Best way to insert value into a map in C++

Hello Learners!! In this tutorial, we will learn ways to insert the value into a map in C++ and discuss the best way to do that. Before starting let’s understand what are Maps in STL.

Maps in STL

A Maps in STL (Standard Template Library) can be considered as a container to store key-value pairs. So just directly jump into the Methods of insertion in Maps.

What is Insertion??

Insertion is a technique using which we can insert our key-value pairs or items inside our map container. There are three ways using which you can insert easily. Let’s discuss each method Briefly.

Different ways to insert values into a map in C++

Insertion in Maps can be done in 3 ways:-

      1) Using Insert function:- One of the easiest ways to insert a value into a map is by using the built-in function insert().

    Let’s understand how to insert a value using the above function.

  •  Firstly you need to include a header file Map if you want to use maps in C++.
  •   The next step is the creation of a map where the key and value both are Floats and the name of the map is map1.
  •   Now simply you just have to use the built-in function insert(). Remember Maps always store unique elements as key-value pairs. If accidentally or intensionally you insert the same key multiple times it will store it only once.
  • After that we will use Loop for our map elements. Inside our loop, we are using Two methods The first one is begin() and the next is the end(). The former one points to the beginning while the latter will point to the end.
  •  The name of the iterator is iter and we are using  ( * ) to get the element where our pointer is. additionally, we are using .first to get the first value of the pair and .second to get the second value of the pair.

 Now let’s see our code:-

#include <iostream>
#include <map>
using namespace std;
int main()
{
    map<float, float> map1;

    map1.insert({ 56.4, 67.2 });
    map1.insert({ 34.3, 76.45 });
    map1.insert({ 2, 99.4 });
    map1.insert({ 2, 76.3 });

    map <float,float> :: iterator iter;
   //printing our key value pairs
    cout << "KEY & VALUE"<<endl;
    for (iter = map1.begin(); iter != map1.end(); iter++)
    {
      cout<<(*iter).first<<"    "<<(*iter).second<<"\n";
    }
    return 0;
}

 

2) Using Emplace Function:- Another way to insert a value into a map is by using Emplace function. This is also a built-in function in STL.

Let’s understand how to insert a value using the above function:-

  •  Firstly you need to include a header file Map if you want to use maps in C++.
  •   The next step is the creation of a map where the keys are of data-type int and values are of data-type Float and we call it map2.
  •   Now simply you just have to use the built-in function emplace(). Maps always arrange these key-value pairs according to the key elements.
  •  at last, we will print our key-elements pairs with the help of a loop.

 Now let’s see our code:-

#include <iostream>
#include <map>
using namespace std;
int main()
{

    map<int, float> map2;

    map2.emplace(3, 78.4);
    map2.emplace(1, 34.4);
    map2.emplace(2, 66.2);
    map2.emplace(1, 23.4);
    map2.emplace(8, 52.3);

    map <int,float> :: iterator iter;
    
    //printing key-value pairs
    cout << "KEY & VALUE"<<endl;
    for (iter = map2.begin(); iter != map2.end(); iter++)
    {
      cout<<(*iter).first<<"    "<<(*iter).second<<"\n";
    }

    return 0;
}

3) With the help of Operator:- In addition to the above-discussed methods, Isn’t it interesting we can also insert or assign a value into a map using the” []” operator.

Let’s understand how to insert a value using the above function:-

  •  Firstly you need to include a header file Map if you want to use maps in C++.
  • The next step is the creation of a map where the keys are of data-type char and values are of data-type float and we name it as map3.
  •   Now simply you just have to use the operator “[]” to assign a value to your key.
  • Lastly, you can print the value corresponding to keys using the cout function.

 Now let’s see our code:-

#include <iostream>
#include <map>
using namespace std;
int main()
{
     map<char, float> map3;

    
    map3['x'] = 54.3;
    map3['y'] = 23.2;
    map3['z'] = 88.56;

 
    cout << "The  value assigned to key x is : ";
    cout << map3['x'] << endl;

    cout << "The  value assigned to key x is : ";
    cout << map3['y'] << endl;

    cout << "The  value assigned to key x is : ";
    cout << map3['z'] << endl;



    return 0;

}

 

The best way for insertion into the map in C++

The above discussion about various ways of insertion is essential to understand what’s the best way to do the same?

Let me tell you frankly every method has its advantage. Most people use insert(), which is the built-in function for insertion. In this, the elements are automatically sorted w.r.t the key.

Now talking about emplace function, It is almost similar to insert() but the only variation is that it increases the size of the map by 1. Also, it prevents an unnecessary copy of the object.

Last but not the least,” operator[]“, also increases the size of the map by 1. An interesting thing you need to know is that this “[]” operator always assigns a Null value even if a value is not mapped to our key.

so here we come to the end of our discussion to summarize I would say every method has its benefit it depends on the user and on the situation where he/she want to use it.

So, this is all about Insertion into a Map in C++.

Leave a Reply

Your email address will not be published.