How can I append the content of one map to another map?

32,017

Solution 1

map<int,int> map1;
map<int,int> map2;
map1.insert(map2.begin(), map2.end());

This will insert into map1 the elements from the beginning to the end of map2. This method is standard to all STL data structure, so you could even do something like

map<int,int> map1;
vector<pair<int,int>> vector1;
vector1.insert(map1.begin(), map1.end());

Furthermore, pointers can also function as iterators!

char str1[] = "Hello world";
string str2;
str2.insert(str1, str1+strlen(str1));

Highly recommend studying the magic of the STL and iterators!

Solution 2

You can use use insert method of the map. For example:

   std::map<int, int> map1;
    std::map<int, int> map2;

    map1[1] = 1;

    map2.insert(map1.begin(), map1.end());
    map1.clear();

    map1[2] =2;
    map2.insert(map1.begin(), map1.end());

Solution 3

You can do this several ways depending on what you want to do:

  1. Use the copy constructor:

    map< string, list < string > > map1;
    // fill in map1
    
    map< string, list < string > > map2(map1);
    
  2. Use the assignment operator as you indicate in the question:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    
    // fill in map1
    
    map2 = map1;
    
  3. Do it all yourself manually:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    
    // fill in map1
    
    for (map< string, list < string > >::iterator i = map1.begin();
         i <= map1.end(); ++i) {
      map2[i.first()] = i.second();
    }
    

It sounds like (1) is what you want.

Share:
32,017

Related videos on Youtube

Cute
Author by

Cute

Updated on July 09, 2022

Comments

  • Cute
    Cute almost 2 years

    I have the following two maps:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    

    I populated map1 with the following content:

    1. kiran; c:\pf\kiran.mdf, c:\pf\kiran.ldf
    2. test;  c:\pf\test.mdf, c:\pf\test.mdf
    

    Then I copied the content of map1 into map2 as follows:

    map2 = map1;
    

    Then I filled map1 again with the following new content:

    1. temp; c:\pf\test.mdf, c:\pf\test.ldf
    2. model; c:\model\model.mdf, c:\pf\model.ldf
    

    Now I have to append this content to map2. I cannot use map2 = map1;, because this will overwrite the existing content in map2. So, how can I do this?

    • sharptooth
      sharptooth almost 15 years
      How does it happen that you and user Cute ask very similar questions at almost the same moments???
    • sharptooth
      sharptooth almost 15 years
      Okay, but what's the point in asking the question twice? One of you could ask the question and then both of you could see the answers. Question duplication just dilutes the community effort and you get less good answers.
  • Ray Hidayat
    Ray Hidayat almost 15 years
    I'd like to add: std::copy(map1.begin(), map1.end(), insert_iterator(map2, map2.begin()) is another way
  • Kiran Kumar
    Kiran Kumar almost 15 years
    Its just an example..it means the key val : 1 maps to value 1 :-)
  • GManNickG
    GManNickG almost 15 years
    Remember the space between the two > > :)
  • Nick Lewis
    Nick Lewis almost 15 years
    Oh yes, certain compilers will complain about nested template parameters (cough GCC), as the >> looks like a stream extraction operator. So you may need a space in the middle. I come from a Windows world, though, and the compiler in Visual Studio does the right thing. :)
  • Kirill V. Lyadvinsky
    Kirill V. Lyadvinsky almost 15 years
    In C++1x space between >> is not necessary.
  • 小文件
    小文件 over 5 years
    use std::make_move_iterator if map2 is not needed.