×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Insert elements in map | C++ STL

C++ STL map::insert() function with Example: Here, we are going to learn how to insert elements in map in C++ STL?
Submitted by Vivek Kothari, on December 03, 2018

Maps are associative containers. Maps store elements in form of a pair of key value and there mapped value and the key value are stored in a sorted manner. The time taken to insert an element in the map is generally O(logn). There are various ways to insert elements in a map, some methods are discussed in this article.

Using insert() function

The std::map::insert() function is built in function in C++ STL and used for inserting the elements in map. We can use this function in following ways:

  • insert(pair): to insert a pair in map.
  • insert(pos, pair): to insert a pair at specified position in a map.
  • insert(beg, end): it is used to copy contents of one map to other.

Example

#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{
    map< char, int > myMap; 
      
    // declaring iterators 
    map<char, int>::iterator it ; 
    map<char, int>::iterator it1;
 
 	// example of insert(pair)
    myMap.insert( pair<char, int>('P', 100) ); 
      
    // printing elements of map after insertion 
    cout << "The elements of map after insertion : \n"; 
      
    for (it1 = myMap.begin(); it1!=myMap.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
      
    it = myMap.begin(); 
      
    // example of insert(pos, pair) 
    // inserting map pair by specifying position
    myMap.insert(it, pair<char, int>('Q', 101) ); 
      
    cout << endl ; 
      
    //elements in map after insertion
    cout << "The elements of map after insertion using insert(pos, pair) : \n"; 
      
    for (it1 = myMap.begin(); it1!=myMap.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
      
    // initializing another map  
    map<char, int> myMap2; 
      
    // using insert(beg_iter, end_iter) to copy all elements 
    myMap2.insert(myMap.begin(), myMap.end()); 
      
    cout << endl ; 
      
    // printing elements of map
    cout << "The elements of new map after using insert(beg_iter, end_iter) : \n"; 
      
    for (it1 = myMap2.begin(); it1!=myMap2.end(); ++it1) 
        cout << it1->first << "  :  " << it1->second << endl; 
     
	return 0;  
} 

Output

The elements of map after insertion :
P  :  100

The elements of map after insertion using insert(pos, pair) :
P  :  100
Q  :  101

The elements of new map after using insert(beg_iter, end_iter) :
P  :  100
Q  :  101

Using [] Operator

Using [] operator, we can directly insert an element into a map. It returns the pointer to the newly constructed element and size of map is always increased by 1.

Example

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main ()
{
  map<char,string> mymap;

  // inserting elements
  mymap['i']="include";
  mymap['h']="help";
  
  //printng the elements of map
  cout << "mymap['i'] is " << mymap['i'] << endl;
  cout << "mymap['h'] is " << mymap['h'] << endl;

  cout << "now mymap contains " << mymap.size() << " elements."<<endl;

  return 0;
}

Output

mymap['i'] is include
mymap['h'] is help
now mymap contains 2 elements.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.