- Examples

We will discuss the examples of different associative containers for comparative analysis in this lesson.

Example 1 - Unordered Map

Press + to interact
//unorderedMap.cpp
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
int main(){
std::cout << std::endl;
// using the C++ map
std::map<std::string, int> m { {"Dijkstra", 1972}, {"Scott", 1976}, {"Wilkes", 1967}, {"Hamming", 1968} };
for(auto p : m) std::cout << '{' << p.first << ", " << p.second << '}';
std::cout << std::endl;
std::cout << "m[Scott]: " << m["Scott"] << std::endl;
m["Ritchie"] = 1983;
m["Scott"]= 1988;
for(auto p : m) std::cout << '{' << p.first << ", " << p.second << '}';
std::cout << "\n\n";
// using the C++11 unordered_map
std::unordered_map<std::string, int> um { {"Dijkstra", 1972}, {"Scott", 1976}, {"Wilkes", 1967}, {"Hamming", 1968} };
for(auto p: um) std::cout << '{' << p.first << ", " << p.second << '}';
std::cout << std::endl;
std::cout << "um[Scott]: " << um["Scott"] << std::endl;
um["Ritchie"] = 1983;
um["Scott"]= 1988;
for(auto p : um) std::cout << '{' << p.first << ", " << p.second << '}';
std::cout << std::endl;
std::cout << std::endl;
}

Explanation

  • In the example above, we have defined an std::map m and an std::unordered_map um and stored the same data in both containers.

  • Values in std::map are stored depending on the alphabetical value of their associated keys.

  • Values in std::unordered_map are stored depending on the hash values of their associated keys.

  • Values can be accessed using their associated keys.

Example 2 - Unordered Multimap

Press + to interact
// unorderedMapMultimap.cpp
#include <iostream>
#include <map>
#include <unordered_map>
int main(){
std::cout << std::endl;
long long home= 497074123456;
long long mobile= 4916046123356;
// constructor
std::unordered_multimap<std::string, long long> multiMap{{"grimm", home}, {"grimm", mobile}, {"jaud-grimm", home}};
std::unordered_map<std::string, int> uniqMap{{"bin", 1}, {"root", 20}, {"nobody", 65834}, {"rainer", 1000}};
// show the unordered maps
std::cout << "multiMap: ";
for(auto m : multiMap) std::cout << '{' << m.first << ", " << m.second << '}';
std::cout << std::endl;
std::cout << "uniqMap: ";
for(auto u : uniqMap) std::cout << '{' << u.first << ", " << u.second << '}';
std::cout << std::endl;
std::cout << std::endl;
// insert elements
long long work= 4970719754513;
multiMap.insert({"grimm", work});
// will not work
//multiMap["grimm-jaud"]=4916012323356;
uniqMap["lp"]=4;
uniqMap.insert({"sshd", 71});
std::map<std::string, int> myMap{{"ftp", 40}, {"rainer", 999}};
uniqMap.insert(myMap.begin(), myMap.end());
// show the unordered maps
std::cout << "multiMap: ";
for(auto m : multiMap) std::cout << '{' << m.first << ", " << m.second << '}';
std::cout << std::endl;
std::cout << "uniqMap: ";
for(auto u : uniqMap) std::cout << '{' << u.first << ", " << u.second << '}';
std::cout << std::endl;
std::cout << std::endl;
// search for elements
// only grimm
auto iter= multiMap.equal_range("grimm");
std::cout << "grimm: ";
for(auto itVal= iter.first; itVal !=iter.second;++itVal){
std::cout << itVal->second << " ";
}
std::cout << std::endl;
std::cout << "multiMap.count(grimm): " << multiMap.count("grimm") << std::endl;
auto it= uniqMap.find("root");
if ( it != uniqMap.end()){
std::cout << "uniqMap.find(root): " << it->second << std::endl;
std::cout << "uniqMap[root]: " << uniqMap["root"] << std::endl;
}
// will create a new entry
std::cout << "uniqMap[notAvailable]: " << uniqMap["notAvailable"] << std::endl;
std::cout << std::endl;
// remove
int numMulti= multiMap.erase("grimm");
int numUniq= uniqMap.erase("rainer");
std::cout << "Erased " << numMulti << " times grimm from multiMap." << std::endl;
std::cout << "Erased " << numUniq << " times rainer from uniqMap." << std::endl;
// all
multiMap.clear();
uniqMap.clear();
std::cout << std::endl;
std::cout << "multiMap.size(): " << multiMap.size() << std::endl;
std::cout << "uniqMap.size(): " << uniqMap.size() << std::endl;
std::cout << std::endl;
}

Explanation

  • In lines 14-15, we have defined an std::unordered_multimap named multiMap and an std::unordered_map named uniqMap ...

Get hands-on with 1400+ tech skills courses.