views:

613

answers:

5

I have two maps:

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

I have populated map1, now I want to copy the map1 contents into map2. So I simply did:

I have some operation for that the map1 fills with

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

And now I have to fill map2 with this content. And agian the map1 fills with info

 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 these contents to map2. How do I do this?

+1  A: 

It looks like that's the right way to do it according to the documentation: http://www.cplusplus.com/reference/stl/map/operator=/

Ray Hidayat
+1  A: 

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
I'd like to add:std::copy(map1.begin(), map1.end(), insert_iterator(map2, map2.begin()) is another way
Ray Hidayat
I was expecting this to be the answer at first shot
Uday
+4  A: 

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
map1[1] = 1;what does it indiates can u pz tell me....
Cute
Its just an example..it means the key val : 1 maps to value 1 :-)
Naveen
+11  A: 
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
Remember the space between the two `> >` :)
GMan
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. :)
Nick Lewis
good info - thanks!
AdamC
In C++1x space between >> is not necessary.
Kirill V. Lyadvinsky
A: 

I think you want this:

mapb.insert(mapa.begin(), mapa.end());

I believe that will just skip any keys that already exist in the target. If you want to overwrite values for duplicate keys you would have to iterate over the items, insert each one, test the result pair and replace the value.

For the "three map" case, you would need to:

mapc.insert(mapa.begin(), mapa.end());
mapc.insert(mapb.begin(), mapb.end());
Tim Sylvester