Unordered Set in STL | C++

/Unordered Set in STL | C++

An unordered_set is implemented using a hash table where keys are hashed into indices of a hash table so that the insertion is always randomized. All operations on the unordered_set takes constant time O(1) on an average which can go up to linear time O(n) in worst case which depends on the internally used hash function, but practically they perform very well and generally provide a constant time lookup operation.
The unordered_set can contain key of any type – predefined or user-defined data structure but when we define key of type user define the type, we need to specify our comparison function according to which keys will be compared.

Important Methods

  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator pointing to the first element in the unordered_set container.
  • void clear() noexcept;
    All the elements in the unordered_set container are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.
  • size_type count ( const key_type& k ) const;
    Searches the container for elements with a value of k and returns the number of elements found. Because unordered_set containers do not allow for duplicate values, this means that the function actually returns 1 if an element with that value exists in the container, and zero otherwise.
  • bool empty() const noexcept;
    Returns a bool value indicating whether the unordered_set container is empty, i.e. whether its size is 0.
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator pointing to the past-the-end element in the unordered_set container (1) or in one of its buckets (2).
  • (1) iterator erase ( const_iterator position );
    (2) size_type erase ( const key_type& k );
    (3) iterator erase ( const_iterator first, const_iterator last );
    Removes from the unordered_set container either a single element or a range of elements ([first,last)).
  • size_type size() const noexcept;
    Returns the number of elements in the unordered_set container.

 

Example

#include<iostream>
#include <unordered_set>
 
using namespace std;
 
int main(){
  // declaring unordered_set
  unordered_set<int> s1; 
  
  // iterator for unordered_set
  unordered_set<int> :: iterator it; 
  
  // inserting
  for(int i=0;i<5;i++){
    s1.insert(i*10);
  }
  
  // Inserting using Iterator
  it = s1.begin();
  s1.insert(it,99);
  
  // Inserting array
  int ary[]= { 23, 34, 45, 56};
  s1.insert(ary, ary+4);
  
  // Finding elemnt 23
  it = s1.find(23);
  cout << *it;
  
  // Erasing by iterator
  s1.erase(s1.begin());
  // Erasing by key  
  s1.erase(34);                         
  // Erasing by range
  s1.erase(s1.find(45), s1.end(56) ); 
  
  // Size of the set
  cout << s1.size();
   
  // Printing the set
  for(it= s1.begin(); it!=s1.end(); it++){
    cout << *it;
  }
  
  // clearing list 1
  s1.clear();
  
  // Checking for Empty
  cout << s1.empty()
  
  return 0;
}

 

September 18th, 2019|Categories: Programming|Tags: |
avatar
  Subscribe  
Notify of