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.


bool represents the result of insertion and iterator represents the position of newly added element in map.

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;
   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

	// Insert Element in map
	result = mapOfWordCount.insert(std::pair<std::string, int>("second", 2));
	// Test its result

	// Insert Element in map
	result = mapOfWordCount.insert(std::pair<std::string, int>("third", 3));
	// Test its result

	// Try to add duplicate element
	result = mapOfWordCount.insert(std::pair<std::string, int>("third", 4));
	// Test its 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;

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


To compile the above example in linux use following command,

g++ -std=c++11 example.cpp

Join a list of 2000+ Programmers for latest Tips & Tutorials