unordered_map c++ insert
// 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;
}
// C++ program to illustrate
// unordered_map::insert({key, element})
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
unordered_map<int, int> ump;
// insert elements in random order
ump.insert({ 20, 130 });
ump.insert({ 100, 410 });
ump.insert({ 31, 60 });
// prints the elements
cout << "KEY\tELEMENT\n";
for (auto itr = ump.begin(); itr != ump.end(); itr++) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
// unordered_map::insert
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double>
myrecipe,
mypantry = {{"milk",2.0},{"flour",1.5}};
std::pair<std::string,double> myshopping ("baking powder",0.3);
myrecipe.insert (myshopping); // copy insertion
myrecipe.insert (std::make_pair<std::string,double>("eggs",6.0)); // move insertion
myrecipe.insert (mypantry.begin(), mypantry.end()); // range insertion
myrecipe.insert ( {{"sugar",0.8},{"salt",0.1}} ); // initializer list insertion
std::cout << "myrecipe contains:" << std::endl;
for (auto& x: myrecipe)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
#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;
}