copying map

M

Minkoo Seo

Hi group!

I'd like know the best way to copy a map. I can copy a list
using copy and back_inserter, but in case of map, I don't
know how.

Given the following:

#include <iostream>
#include <utility>
#include <algorithm>
#include <iterator>
#include <map>

using namespace std;

int main()
{
map<string, int> m;
map<string, int> m2;

m["a"] = 1;
m["b"] = 2;

// error!
copy(m.begin(), m.end(), back_inserter(m2));

return EXIT_SUCCESS;
}

How can I write the copy statement? If it can't be done with
copy, what is the solution that requries the smallest lines of code?

Sincerely,
Minkoo Seo
 
M

Mark P

Minkoo said:
Hi group!

I'd like know the best way to copy a map. I can copy a list
using copy and back_inserter, but in case of map, I don't
know how.

Given the following:

#include <iostream>
#include <utility>
#include <algorithm>
#include <iterator>
#include <map>

using namespace std;

int main()
{
map<string, int> m;
map<string, int> m2;

m["a"] = 1;
m["b"] = 2;

// error!
copy(m.begin(), m.end(), back_inserter(m2));

return EXIT_SUCCESS;
}

How can I write the copy statement? If it can't be done with
copy, what is the solution that requries the smallest lines of code?

Sincerely,
Minkoo Seo

How about:

m2 = m;

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top