tommygun
0
Q:

traversing unordered_map

// 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; 
  
} 
4
unordered_map<string, int> umap;

for (auto x : umap) 
	cout << x.first << " " << x.second << endl;
1
unordered_map is an associated container that stores elements formed by combination of key value and a mapped value. 
The key value is used to uniquely identify the element and mapped value is the content associated with the key. 
Both key and value can be of any type predefined or user-defined.

1
#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;
}
0

New to Communities?

Join the community