C++ std::map::insert – Simple program example to add key value pair in a map. The map data structure stores key value pair.
Here is how we declare a map contain int as key and string as value.
std::map<int, std::string> m;
Using the variable m, we call the insert() function.
C++ map::insert Syntax
std::pair <iterator,bool> insert( const value_type& value )
Insert function accepts a pair of key value e.g. std::pair<key data type,value data type>. Below is how we call insert function with concrete values.
m.insert ( std::pair<int,string>(1,”Peter”) );
As you can see in the declaration insert()function returns pair of iterator and bool values. Bool values says if insertion pass or fail.
We will see an example of returning insert() function. But, before that, lets see the example of adding key and value pair in the std::map.
C++ map::insert example
This program will add key value pair into a map. Key is an int data type and value is of std::string type.
1,” Peter “
2,” Lisa “
3,” Danish “
4,” Linda “
It will print the output using a map::iterator
/*-------------------------------------------------------
* C++ Example - std::map::insert - adding key value pair
*
*/
#include <map> // Include map
#include <iostream>
using namespace std;
int main(){
/* Create a hash map - int as key and string as value */
std::map<int, std::string> m;
/* Insert some elements ( key value pair) in the hash map */
m.insert ( std::pair<int,string>(1,"Peter") );
m.insert ( std::pair<int,string>(2,"Lisa") );
m.insert ( std::pair<int,string>(3,"Danish") );
m.insert ( std::pair<int,string>(4,"Linda") );
/* Print the elements from hash map */
std::cout << "map Contains:\n";
std::map<int,string>::iterator itr;
for( itr = m.begin(); itr != m.end(); ++itr){
cout << "Key => " << itr->first << ", Value => " << itr->second.c_str() << endl;
}
return 0;
}
Output:
map Contains:
Key => 1, Value => Peter
Key => 2, Value => Lisa
Key => 3, Value => Danish
Key => 4, Value => Linda
C++ map::insert function return example
In this program example, we will see the test example for insert() function return value.
We will populate the map with key value pair as in above example. We will try to add the key value which is already present in the map. and check the return result.
Secondly, we will add new pair and check the return result.
map list:
1,” Peter “
2,” Lisa “
3,” Danish “
Add an existing pair like this.
m.insert ( std::pair<int,string>(3,”Danish”) );
Add a new pair like this
m.insert ( std::pair<int,string>(4,”Linda”) );
Code Example:
/*-------------------------------------------------------
* C++ Example - Test return value of insert() function
*
*/
#include <map> // Include map
#include <iostream>
using namespace std;
int main(){
/* Create a hash map - int as key and string as value */
std::map<int, std::string> m;
/* Insert some elements ( key value pair) in the hash map */
m.insert ( std::pair<int,string>(1,"Peter") );
m.insert ( std::pair<int,string>(2,"Lisa") );
m.insert ( std::pair<int,string>(3,"Danish") );
/*-----------------------------------------------
* Test insert() function return value when value is
* already present.
*/
std::pair<std::map<int,std::string>::iterator,bool> returnValue;
returnValue = m.insert ( std::pair<int,string>(3,"Danish") );
if (returnValue.second == false) {
cout << returnValue.first->second.c_str() << " is already in map" <<endl;
}
/*-----------------------------------------------
* Test insert() function return value when value is
* not present.
*/
returnValue = m.insert ( std::pair<int,string>(4,"Linda") );
if (returnValue.second == false) {
cout << returnValue.first->second.c_str() << " is already in map" <<endl;
}else{
cout << "Added new key value :" << "Key => " << returnValue.first->first << ", Value => " << returnValue.first->second.c_str() << endl;
}
return 0;
}
Output:
Danish is already in map
Added new key value: Key => 4, Value => Linda