xxxxxxxxxx
unordered_map<string, string>New_map;
// Adding key-value pairs using
// Initializer list
New_map = {{"Ground", "Grass"},
{"Floor", "Cement"},
{"Table", "Wood"}};
//Source www.geeksforgeeks.org
xxxxxxxxxx
unordered_map<int, string> m =
{
{1, "one"},
{2, "two"},
{3, "three"}
};
C++Copy
xxxxxxxxxx
unordered_map vs map :
map (like set) is an ordered sequence of unique keys whereas in unordered_map key can be stored in any order, so unordered.
The map is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal).
The time complexity of map operations is O(log n) while for unordered_map, it is O(1) on average.
at(): This function in C++ unordered_map returns the reference to the value with the element as key k.
begin(): Returns an iterator pointing to the first element in the container in the unordered_map container
end(): Returns an iterator pointing to the position past the last element in the container in the unordered_map container
bucket(): Returns the bucket number where the element with the key k is located in the map.
bucket_count:bucket_count is used to count the total no. of buckets in the unordered_map. No parameter is required to pass into this function.
bucket_size: Returns the number of elements in each bucket of the unordered_map.
count(): Count the number of elements present in an unordered_map with a given key.
equal_range: Return the bounds of a range that includes all the elements in the container with a key that compares equal to k.
find(): Returns iterator to element.
empty(): checks whether container is empty in the unordered_map container.
erase(): erase elements in the container in the unordered_map container.
xxxxxxxxxx
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
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;
}
xxxxxxxxxx
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
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;
}
xxxxxxxxxx
// 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 int 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;
}
xxxxxxxxxx
// C++ program to demonstrate
// Initialization, indexing,
// and iteration
#include <iostream>
#include <unordered_map>
using namespace std;
// Driver code
int main()
{
// Declaring umap to be of
// <string, double> type key
// will be of string type and
// mapped value will be of double type
unordered_map<string, double> umap;
// inserting values by using [] operator
umap["PI"] = 3.14;
umap["root2"] = 1.414;
umap["root3"] = 1.732;
umap["log10"] = 2.302;
umap["loge"] = 1.0;
// inserting value by insert function
umap.insert(make_pair("e", 2.718));
string key = "PI";
// If key not found in map iterator
// to end is returned
if (umap.find(key) == umap.end())
cout << key << " not found\n\n";
// If key found then iterator to that
// key is returned
else
cout << "Found " << key << "\n\n";
key = "lambda";
if (umap.find(key) == umap.end())
cout << key << " not found\n";
else
cout << "Found " << key << endl;
// iterating over all value of umap
unordered_map<string, double>::iterator itr;
cout << "\nAll Elements : \n";
for (itr = umap.begin();
itr != umap.end(); itr++)
{
// itr works as a pointer to
// pair<string, double> type
// itr->first stores the key part and
// itr->second stores the value part
cout << itr->first << " " <<
itr->second << endl;
}
}