In this instructional exercise, you will find out about stl unordered set compartment in c++ and different capacities relevant on it.
Unordered Set goes under unordered holders. As we talked about in prologue to stl article, unordered compartments inside executed with hash tables.
Everything determined by hash work, to guide to the hash table. The principle advantage is in the event that we have powerful hash work we can discover components in O (1) time.
By and large, it can go to straight time. Essentially we can say it depends on the kind of hash work utilized.
So we can say these are quickest among all holders.
As the name says that in unordered_set the request isn’t characterized. The unordered set doesn’t permit copies.
C++ STL Unordered Set – std::unordered_set
Valuable iterators to deal with this unordered set:
start(): returns iterator to the start
end(): returns iterator as far as possible of the rundown
cbegin(): returns steady iterator to the start
cend(): returns steady iterator as far as possible.
First, we have to incorporate unordered_set header record. Which is #include
Embeddings component into unordered_set:
There are different approaches to embed components into unordered_set.
Technique 1: Insert straightforwardly bypassing component.
UnOrdSet.insert(element);
Technique 2: Using iterator. This profits iterator at embedded position.
UnOrdSet.insert ( iterator, esteem)
Strategy 3: Copying from another compartment.
Model program to embed into unordered_set:
#include<iostream>
#include <unordered_set>
using namespace std;
int main(){
unordered_set<int> s1; // declaring unordered_set
unordered_set<int> :: iterator it; // iterator for unordered_set
for(int i=0;i<5;i++){
s1.insert(i*10); // inserting using Method1
}
it= s1.begin();
s1.insert(it,99);
int ary[]= { 23, 34, 45, 56};
s1.insert(ary, ary+4); // Inserting using method3
//checking by printing
for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
// We can observe that output will be print in sorted order. That is the property of Unordered_set
return 0;
}
Output
56 45 34 23 40 99 0 10 20 30
Modifiers: The capacities which impact size/information of that holder
eradicate(): We can delete a component by determining worth or indicating iterator.
swap(): swaps components of Unordered_set1 to Unorderedset2 and Unorderedset2 to Unordered_set1.
clear(): evacuates all components in the rundown. It results rundown of size 0.
Model program to appear above capacities:
#include<iostream>
#include<unordered_set>
using namespace std;
int main(){
unordered_set<int> s1;
unordered_set<int> :: iterator it;
for(int i=0; i<5; i++) s1.insert(i+10);
s1.erase(12); // deleting element 12
cout << "elements after deleting 12 -->";
for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
cout << endl;
unordered_set<int> s2;
for(int i=0;i<4;i++) s2.insert(i);
cout << "unordered_set 1 elements before swapping -->";
for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
cout << endl;
cout << "unordered_set 2 elements before swapping -->";
for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
cout << endl;
s1.swap(s2); // swapping operation
cout << "unordered_set 1 elements after swapping -->";
for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
cout << endl;
cout << "unordered_set 2 elements after swapping -->";
for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
cout << endl;
s1.clear(); // clearing list 1
cout << "Performing clear() operation on unordered_set1......" << endl;
s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl; //
// ternary operation which resutls list is empty or not
return 0;
}
Output
elements after deleting 12 –>14 10 11 13
unordered_set 1 elements before swapping –>14 10 11 13
unordered_set 2 elements before swapping –>3 2 1 0
unordered_set 1 elements after swapping –>3 2 1 0
unordered_set 2 elements after swapping –>14 10 11 13
Performing clear() operation on unordered_set1……
Unordered_set is empty
Data recovering capacities:
void(): restores a Boolean worth whether Unordered_set is unfilled or not.
size(): restores the size of the Unordered_set.
max_size(): restores the most extreme size an Unordered_set can have.
Also, some more activities are:
discover(): It returns iterator to the component.
count(x): Returns how often components “x” present in Unordered_set.
Model program to appear above capacities:
#include<iostream>
#include<unordered_set>
using namespace std;
int main(){
unordered_set<int> s1;
unordered_set<int> :: iterator it;
for(int i=0; i<5; i++) s1.insert(i+10);
s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl;
cout << "size of the unordered_set is " << s1.size() << endl;
cout << "maximum size of the unordered_set is " << s1.max_size() << endl;
cout << "finding elemnt 12 in unordered_set" << endl;
it= s1.find(12);
cout << *it << endl;
s1.insert(12);
if(s1.count(22)) cout << "number 22 is in the unordered_set " << endl;
else cout << "22 is not in the unordered_set";
return 0;
}
Output
unordered_set is not empty
size of the unordered_set is 5
maximum size of the unordered_set is 1152921504606846975
finding elemnt 12 in unordered_set
12
22 is not in the unordered_set