C++ map insert – C++ Map Insert Example

C++ map insert: In the previous article, we have discussed about Different ways to iterate over a set in C++. Let us learn Map Insert in C++ Program.

C++ Map Insert Example

We are going to see how we can use the std::map in C++.

The std::map stores data in the form of key and values. If we want to add an element into the map we can use the insert( ) function.

Syntax- pair<iterator,bool> insert (const value_type& element);

It will accept the key and the value and it returns a map iterator with a boolean in the following form-

pair<map_iterator,boolean>

The boolean value represents the result of the operation i.e. if it is

  1. Boolean == true –  The insertion operation was successful and the iterator holds the location of the newly added element.
  2. Boolean == false – The insertion failed and the iterator contains the location of the pair passed.

Let’s see by an example

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
void checkRes
(
    std::pair<std::map<std::string, int>::iterator, bool> &res)
{
    // Check if Insertion was successful
    if (res.second == false)
    {
        // Insertion Failed
        std::cout << "Unable to insert . duplicate key :: " << res.first->first
                  << std::endl;
    }
    else
    {
        // Insertion was successful
        std::cout << "Successfully added , key :: " << res.first->first
                  << std::endl;
    }
}
int main()
{
    // map of string and int as key-value
    std::map<std::string, int> WordMap;
    // Pair of the map iterator and boolean
    std::pair<std::map<std::string, int>::iterator, bool> res;
    // Insert an element in the map
    res = WordMap.insert(std::pair<std::string, int>("one", 1));
    // Check the result of insertion
    checkRes(res);
    // Inserting an element in the map
    res = WordMap.insert(std::pair<std::string, int>("two", 2));
    // Check the result of insertion
    checkRes(res);
    // Inserting an element in the map
    res = WordMap.insert(std::pair<std::string, int>("three", 3));
    // Check the result of insertion
    checkRes(res);
    // Inserting an element in the map
    res = WordMap.insert(std::pair<std::string, int>("four", 4));
    // Check the result of insertion
    checkRes(res);
    // Inserting an element in the map
    res = WordMap.insert(std::pair<std::string, int>("five", 5));
    // Check the result of insertion
    checkRes(res);

    //A map iterator that points to the beginning of the map
    std::map<std::string, int>::iterator it = WordMap.begin();
    std::cout << "____________________________" << std::endl;
    //Iterating using for_each loop
    std::for_each(WordMap.begin(), WordMap.end(),
                  [](std::pair<std::string, int> elem)
                  {
                      // Get the key
                      std::string word = elem.first;
                      // getting the value associated with the key
                      int count = elem.second;
                      std::cout << word << " :: " << count << std::endl;
                  });

    return 0;
}
Output :

Successfully added , key :: one
Successfully added , key :: two
Successfully added , key :: three
Successfully added , key :: four
Successfully added , key :: five
____________________________
five :: 5
four :: 4
one :: 1
three :: 3
two :: 2