unordered map code example
Example 1: unordered_map of pair and int
struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
unordered_map<pair<int,int>,int,HASH>mp;
int foo(unordered_map<pair<int,int>,int,HASH> &mp);
Example 2: hashmap in cpp
#include <unordered_map>
#include <iostream>
int main()
{
std::unordered_map<std::string, int> age;
age["Michael"] = 16;
age.insert(std::pair<std::string, int>{"Bill", 25});
age.insert({"Chris", 30});
age["Michael"] = 18;
age.at("Chris") = 27;
std::string query;
query = "Eric";
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
query = "Michael";
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
age.erase(query);
if (age.find(query) == age.end())
{
std::cout << query << " is not in the dictionary!" << std::endl;
}
for (const std::pair<std::string, int>& tup : age)
{
std::cout << "Name: " << tup.first << std::endl;
std::cout << "Age: " << tup.second << std::endl;
}
}
Example 3: header file for unordered_map in c++
#include<unordered_map>
Example 4: unordered_map c++
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <unordered_map>
using namespace std;
int main() {
map<char, int> M;
unordered_map<char, int> U;
string s = "Sumant Tirkey";
for (char c : s) {
M[c]++;
}
for (char c : s){
U[c]++;
}
return 0;
}