27

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?

honk
  • 9,137
  • 11
  • 75
  • 83
Cute
  • 13,643
  • 36
  • 96
  • 112
  • 2
    How does it happen that you and user Cute ask very similar questions at almost the same moments??? – sharptooth Jul 01 '09 at 06:28
  • 2
    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. – sharptooth Jul 01 '09 at 06:57
  • (I have merged these two identical questions) – Marc Gravell Jul 01 '09 at 07:15
  • It looks like that's the right way to do it according to the documentation: [http://www.cplusplus.com/reference/stl/map/operator=/](http://www.cplusplus.com/reference/stl/map/operator=/) – Ray Hidayat Jul 01 '09 at 05:41

5 Answers5

65
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!

Nick Lewis
  • 4,150
  • 1
  • 21
  • 22
8

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());
Naveen
  • 74,600
  • 47
  • 176
  • 233
4

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.

Greg
  • 1,304
  • 1
  • 12
  • 8
1

Since C++17 std::map provides a merge() member function. It allows you to extract content from one map and insert it into another map. Example code based on your data could be written as follows:

using myMap = std::map<std::string, std::list<std::string>>;

myMap  map2 = { {"kiran", {"c:\\pf\\kiran.mdf", "c:\\pf\\kiran.ldf"}},
                {"test",  {"c:\\pf\\test.mdf",  "c:\\pf\\test.mdf"}} };

myMap  map1 = { {"temp",  {"c:\\pf\\test.mdff",    "c:\\pf\\test.ldf"}},
                {"model", {"c:\\model\\model.mdf", "c:\\pf\\model.ldf"}} };

map2.merge(map1);

for (auto const &kv : map2) {
    std::cout << kv.first << " ->";
    for (auto const &str : kv.second)
        std::cout << " " << str;
    std::cout << std::endl;
}

Output:

kiran -> c:\pf\kiran.mdf c:\pf\kiran.ldf
model -> c:\model\model.mdf c:\pf\model.ldf
temp -> c:\pf\test.mdff c:\pf\test.ldf
test -> c:\pf\test.mdf c:\pf\test.mdf

Notes:

  • A key can only exist once in a map (i.e. kiran, test, temp, model in your example). If both maps contain the same key, then the corresponding element will not be merged into map2 and remain in map1.
  • If all elements can be merged into map2, then map1 will become empty.
  • The merge() function is quite efficient, because element are neither copied nor moved. Instead, only the internal pointers of the map nodes are redirected.

Code on Coliru

honk
  • 9,137
  • 11
  • 75
  • 83
0

If you want to insert your map as you define it, this is nice:

payload.insert({
            { "key1", "one" },
            { "key2", 2 },
        });
Pellet
  • 2,254
  • 1
  • 28
  • 20