In this article we will discuss how to insert a key value pair in std::map.
Map internally store elements in a pair of key value i.e.
std::pair<key Type, Value Type>
So, to add an element in map we can use one of its member function insert() i.e.
pair<iterator,bool> insert (const value_type& element);
It accepts an object of key value pair and returns an pair of map iterator and bool.
In the returned pair i.e.
pair<iterator,bool>
bool represents the result of insertion and iterator represents the position of newly added element in map.
Frequently Asked:
If insertion in map is successful then,
bool —> true
Iterator —-> Points to Position of newly added element.
If insertion in map failed then,
bool —> false
Iterator —-> Pointing to the passed pair
Let’s see an example,
Suppose we have a map of string & int as key value pair i.e.
std::map<std::string, int> mapOfWordCount;
To insert an element in it we will create a pair of string & int i.e.
std::pair<std::string, int>("first", 1)
insert() function will return a pair of map iterator & bool i.e.
std::pair<std::map<std::string, int>::iterator, bool > result; result = mapOfWordCount.insert(std::pair<std::string, int>("first", 1));
Now, to check if insertion we will first check the bool in result pair i.e.
if(result.second == false) { std::cout<<"Failed to add . duplicate key :: "<<result.first->first<<std::endl; } else { std::cout<<"Successful in Adding , key :: "<<result.first->first<<std::endl; }
Complete example is as follows,
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> void testResult( std::pair<std::map<std::string, int>::iterator, bool> & result) { // Check if Insertion was successful if (result.second == false) { // Insertion Failed std::cout << "Failed to add . duplicate key :: " << result.first->first << std::endl; } else { // Insertion was successful std::cout << "Successful in Adding , key :: " << result.first->first << std::endl; } } int main() { // Map of string and int std::map<std::string, int> mapOfWordCount; // Pair of Map Iterator and bool std::pair<std::map<std::string, int>::iterator, bool> result; // Insert Element in map result = mapOfWordCount.insert(std::pair<std::string, int>("first", 1)); // Test its result testResult(result); // Insert Element in map result = mapOfWordCount.insert(std::pair<std::string, int>("second", 2)); // Test its result testResult(result); // Insert Element in map result = mapOfWordCount.insert(std::pair<std::string, int>("third", 3)); // Test its result testResult(result); // Try to add duplicate element result = mapOfWordCount.insert(std::pair<std::string, int>("third", 4)); // Test its result testResult(result); // Create a map iterator and point to beginning of map std::map<std::string, int>::iterator it = mapOfWordCount.begin(); std::cout << "*****************************" << std::endl; // Iterate over a map using std::for_each and Lambda function std::for_each(mapOfWordCount.begin(), mapOfWordCount.end(), [](std::pair<std::string, int> element) { // Accessing KEY from element std::string word = element.first; // Accessing VALUE from element. int count = element.second; std::cout<<word<<" :: "<<count<<std::endl; }); return 0; }
Output:
Successful in Adding , key :: first Successful in Adding , key :: second Successful in Adding , key :: third Failed to add . duplicate key :: third ***************************** first :: 1 second :: 2 third :: 3
Pointers in C/C++ [Full Course]
To compile the above example in linux use following command,
g++ -std=c++11 example.cpp
Great Article. As part of code optimization can be skip these additional steps as below:
// Pair of Map Iterator and bool
std::pair<std::map::iterator, bool> result;
// Insert Element in map
result = mapOfWordCount.insert(std::pair(“first”, 1));
// Test its result
testResult(result);
and replace it with one line code:
testResult(mapOfWordCount.insert(std::pair(“first”, 1)));
Please let me know if i am missing something.