+ Reply to Thread
Results 1 to 2 of 2

Thread: How to merge two map in C++?

  1. #1

    How to merge two map in C++?

    I have following code:

    Code:
    void MyMethod()
    {
    	std::map<int, int> map1 ;
    	std::map<int, int> map2 ;
    
    	map1[1] = 1 ;
    	map1[2] = 2 ;
    	map1[3] = 3 ;
    
    	map2[4] = 4 ;
    	map2[5] = 5 ;
    	map2[6] = 6 ;
    
    	//how to merge this two maps?
    }
    How to merge two maps?

    Is there direct way or do I need to iterate through the elements of one map and insert them to another one?

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Use map::insert method to achieve this.

    Example:
    Code:
    void MyMethod()
    {
    	std::map<int, int> map1 ;
    	std::map<int, int> map2 ;
    
    	map1[1] = 1 ;
    	map1[2] = 2 ;
    	map1[3] = 3 ;
    
    	map2[4] = 4 ;
    	map2[5] = 5 ;
    	map2[6] = 6 ;
    
          //this will merge two maps!
           map1.insert(map2.begin(), map2.end()) ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts