Home
Aptitude
Categories


Home » C++ STL

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.

1) Using insert() function

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



2) 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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.