In this article we will discuss the different ways to insert elements in an unordered_map.
Unordered_map provides different overloaded versions of insert() member function to insert key value pairs in an unordered_map. Lets discuss them one by one.
Inserting multiple elements in unordered_map through an initializer_list
unordered_map provides an overloaded version of insert that accepts an initializer_list i.e.
void insert ( initializer_list<value_type> il );
It inserts the multiple key value pair in the map i.e.
Frequently Asked:
// Creating an empty unordered_map std::unordered_map<std::string, int> wordMap; // Inserting elements through an initializer_list wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );
With the we can insert multiple elements in map. But, as map contains can contain only unique keys, so what if we add elements with duplicate key. This version of insert() function returns void , so there is no way to know which elements are added and which are not. Therefore, for such scenarios we need different overloaded version of insert() i.e.
Inserting a single element in an unordered_map and check result
unordered_map provides an overloaded version of insert that accepts an std::pair of key –Â value and inserts the element in map i.e.
pair<iterator,bool> insert ( const value_type& val );
It returns a pair of Iterator and bool. bool variable in pair will be true if element is inserted successfully else it will be false. If element is inserted successfully then iterator in pair will point to the newly inserted element in map.
Let’s see an example,
Best Resources to Learn C++:
typedef std::unordered_map<std::string, int>::iterator UOMIterator; // Pair of Map Iterator and bool value std::pair< UOMIterator , bool> result; // Inserting an element through pair result = wordMap.insert(std::make_pair<std::string, int>("Second", 6)); if(result.second == false) std::cout<<"Element 'Second' not inserted again"<<std::endl;
Also, instead of creating pair and then inserting, we can also insert the value_type using same version of insert i.e.
// Inserting an element through value_type result = wordMap.insert({"Fourth", 4});
Complete working example is as follows,
#include <iostream> #include <unordered_map> #include <string> int main() { // Creating an empty unordered_map std::unordered_map<std::string, int> wordMap; // Inserting elements through an initializer_list wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } ); typedef std::unordered_map<std::string, int>::iterator UOMIterator; // Pair of Map Iterator and bool value std::pair< UOMIterator , bool> result; // Inserting an element through pair result = wordMap.insert(std::make_pair<std::string, int>("Second", 6)); if(result.second == false) std::cout<<"Element 'Second' not inserted again"<<std::endl; // Inserting an element through value_type result = wordMap.insert({"Fourth", 4}); if(result.second == false) { std::cout<<"Element 'Fourth' not inserted again"<<std::endl; } else { // Element inserted sucessfully, so first value in pair // is the iterator of newly inserted element std::cout<<"Element Inserted : "; std::cout<<result.first->first<<"::"<<result.first->second<<std::endl; } for (std::pair<std::string, int> element : wordMap) std::cout << element.first << " :: " << element.second << std::endl; return 0; }
Output:
Element 'Second' not inserted again Element Inserted : Fourth::4 Fourth :: 4 Third :: 3 Second :: 2 First :: 1