In this instructional exercise you will find out about stl unordered guide compartment, for example, std::unordered_map and all capacities appropriate on it.
By its name, we can say that it goes under Associative compartments with Unordered property.
We realize that any unordered compartment inside executed with hash tables. So same has hashing idea in most pessimistic scenario any activity takes O(n) time and on average and best case it takes O(1) time.
We can say it change dependent on sort of hash work we utilized. With correlation with map holders, these work proficiently to discover an incentive by utilizing key.
What’s more, one fundamental distinction is that guide components are structure however unordered guide components are not all together.
Since it depends on key-esteem pair idea all keys must be exceptional.
C++ STL Unordered Map – std::unordered_map
Iterators that can be material on Unordered guide:
start(): returns iterator to the start
end(): returns iterator as far as possible of the compartment.
cbegin(): Returns consistent iterator to the start.
cend(): Returns consistent iterator as far as possible.
To work with an unordered guide we can independently incorporate unordered_map header document. That is:
#include <unordered_map>
Capacities appropriate on the unordered guide:
Embeddings and printing:
Here we can embed utilizing record administrator []. The fundamental bit of leeway of this key-esteem kind of compartments is to store application data.
Model:
Andhra – Amaravathi
Telangana – Hyderabad
Odisha – Bhubaneswar
Like this, we can store this kind of data. Here state names are keys and their capitals are values.
Or then again we can contrast these things and word references in python. For just understanding and to show all capacities on these unordered guide here I am utilizing keys 0, 1, 2, . . . n. Furthermore, values are client decision. However, recollect both key and esteem can be various information types.
Technique 1: We can embed utilizing dependent on the key.
Ex: unMap[key] = esteem;
Strategy 2: Using embed() work.
Utilizing this we can legitimately incorporate key and worth pair at once. To state that those are pair, we should utilize make_pair work.
Since holder putting away the key – esteem sets, we use a mix of iterators and pointers (“first” for key and “second” for esteem).
Underneath model code clarifies these obviously:
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
int value;
unordered_map <int, int> unMap; // Declearing
unordered_map <int, int> :: iterator it;
for (int i=0; i<5; i++) {
cout << "Enter value for key " << i << endl;
cin >> value;
unMap [i]= value; // inserting using key
}
// inserting using .insert function
unMap.insert(make_pair(10,100));
cout << "(Key , Value)" << endl;
for (it= unMap.begin(); it!= unMap.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
return 0;
}
Output
Enter value for key 0
10
Enter value for key 1
20
Enter value for key 2
30
Enter value for key 3
40
Enter value for key 4
50
(Key , Value)
(10 , 100)
(4 , 50)
(0 , 10)
(1 , 20)
(2 , 30)
(3 , 40)
Other change works on unordered map:
delete(): We can expel utilizing key. At that point key and comparing esteem both will be erased. What’s more, by guiding iterator toward some pair, we can erase that.
swap(): swaps components of Unordered map1 to Unordered map2 and the other way around. Swapping should be possible effectively even the two compartments have various sizes.
clear(): evacuates all components in the Unordered guide. It results in compartment size to 0.
Model program to appear above work:
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
unordered_map <int, int> unMap;
unordered_map <int, int> :: iterator it;
for (int i=0; i<5; i++) {
unMap [i]= i+10;
}
cout << "key - value pairs in unordered map are" << endl;
cout << "(Key , Value)" << endl;
for (it= unMap.begin(); it!= unMap.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Erasing pair whcih has key value 3" << endl;
unMap.erase(3);
it= unMap.begin();
cout << "Erasing first pair" << endl;
unMap.erase(it);
cout << "Remaining pairs are" << endl;
cout << "(Key , Value)" << endl;
for (it= unMap.begin(); it!= unMap.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
// creating new unordered map
unordered_map <int, int> unMap2;
for(int i=0; i<5; i++) {
unMap2[i]= i+100;
}
cout << "New unordered map elements are " << endl;
cout << "(Key , Value)" << endl;
for (it= unMap2.begin(); it!= unMap2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Performing swap operation......." << endl;
unMap.swap(unMap2);
cout << "After swap operation second unordered map is " << endl;
cout << "(Key , Value)" << endl;
for (it= unMap2.begin(); it!= unMap2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "After swap operation unMap1 is " << endl;
cout << "(Key , Value)" << endl;
for (it= unMap.begin(); it!= unMap.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
// clear operation
cout << "Applying clear operation on unMap2" << endl;
unMap2.clear();
cout << "Now unMap2 is " << endl;
cout << "(Key , Value)" << endl;
for (it= unMap2.begin(); it!= unMap2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
return 0;
}
Output
key – value pairs in unordered map are
(Key , Value)
(4 , 14)
(0 , 10)
(1 , 11)
(2 , 12)
(3 , 13)
Erasing pair whcih has key value 3
Erasing first pair
Remaining pairs are
(Key , Value)
(0 , 10)
(1 , 11)
(2 , 12)
New unordered map elements are
(Key , Value)
(4 , 104)
(0 , 100)
(1 , 101)
(2 , 102)
(3 , 103)
Performing swap operation…….
After swap operation second unordered map is
(Key , Value)
(0 , 10)
(1 , 11)
(2 , 12)
After swap operation unMap1 is
(Key , Value)
(4 , 104)
(0 , 100)
(1 , 101)
(2 , 102)
(3 , 103)
Applying clear operation on unMap2
Now unMap2 is
(Key , Value)
Capacities identified with limit:
void(): restores a Boolean worth whether unordered_map is unfilled or not.
size(): returns present size of the holder.
max_size(): restores the greatest size a guide can have.
#include<iostream>
#include <unordered_map>
using namespace std;
int main(){
unordered_map <int, int> unMap;
unordered_map <int, int> :: iterator it;
for (int i=0; i<5; i++) {
unMap [i]= i+10;
}
cout << "Size of the unordered map is " << unMap.size() << endl;
cout << "Maximum Size of the unordered map is " << unMap.max_size() << endl;
bool check= unMap.empty();
if (check)
cout << "Unordered map is empty" << endl;
else
cout << "Unordered map is not empty" << endl;
return 0;
}
Output
Size of the unordered map is 5
Maximum Size of the unordered map is 1152921504606846975
Unordered map is not empty
Remark beneath on the off chance that you have any inquiries identified with stl unordered guide.