how to iterate over unordered_map c++
// C++ program to demonstrate functionality of unordered_map
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// Declaring umap to be of <string, int> type
// key will be of string type and mapped value will
// be of double type
unordered_map<string, int> umap;
// inserting values by using [] operator
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
// Traversing an unordered map
for (auto x : umap)
cout << x.first << " " << x.second << endl;
}
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <unordered_map>
using namespace std;
int main() {
map<char, int> M; //based on balanced binary tree takes O(logn) access time
unordered_map<char, int> U; //uses hashing and accessing elements takes O(1)
//U.add(key,value);
//U.erase(key,value);
//map each letter to their occurance
string s = "Sumant Tirkey";
for (char c : s) {
M[c]++;
}
for (char c : s){
U[c]++;
}
return 0;
}
// C++ program makes a map to iterate
// elements in reverse order with simpler
// syntax
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating & Initializing a map of String & Ints
map<int, string> mymap;
// Inserting the elements one by one
mymap.insert(make_pair(10, "geeks"));
mymap.insert(make_pair(20, "practice"));
mymap.insert(make_pair(5, "contribute"));
// rbegin() returns to the last value of map
for (auto it = mymap.rbegin(); it != mymap.rend(); it++) {
cout << "(" << it->first << ", "
<< it->second << ")" << endl;
}
return 0;
}