How to Copy One Map into Another Using Std::Copy

Copy std::map data to another map

Copying one map to another can be done with operator = or the copy constructor.

E.g

map<X, Y> mp1; 
//fill mp1 with data
map<X, Y> mp2(mp1); //mp2 is a copy of mp1 (via copy-construction)
map<X, Y> mp3;
mp3 = mp2; // mp3 is also a copy of mp2 (via copy-assignment)

How can I copy one map into another using std::copy?

You can use GMan's answer --- but the question is, why do you want to use std::copy? You should use the member function std::map<k, v>::insert instead.

m2.insert(m1.begin(), m1.end());

How to transfer keys and values ​from map to another map?

Actually, when you assign map2 to map1. It copies map2 into map1 replacing the old contents.

Therefore after the assignment, the old values are lost and map1 is now an exact copy of map2.

If you want to only add new elements, you can use the insert() function instead. This may look as follows:

#include <map>

int main()
{
std::map<int, int> map1 {{1 ,1001}, {2, 1002}, {3, 1003}};
std::map<int, int> map2 {{10, 1010}, {11, 1011}, {12, 1012}};

map1.insert(map2.cbegin(), map2.cend());

for(const auto & e : map1)
{
std::cout << e.first << ": " << e.second << std::endl;
}

return 0;
}

This way, map2 is inserted into map1 without erasing the old contents. You just have to know that if you try to insert an already existing key, it will not be inserted (as every elements are unique in std::map).


EDIT:

If you want to replace the already existing values too, you can simply use the operator[]. It may look as:

for(const auto & e : map2)
{
// If the key exists, change only the value, add the {key, value} otherwise
map1[e.first] = e.second;
}

copy a map into another map

Use the map's copy constructor:

map<prmNode,vector<prmEdge> > nodo2archi;
map<prmNode,vector<prmEdge> > acopy( nodo2archi ) ;

This code, which copies a map, prints the same size (1) for each.

#include <map>
#include <iostream>
using namespace std;

typedef map <int, int> MapType;

int main() {
MapType m1;
m1.insert( make_pair( 1, 1 ) );
cout << m1.size() << endl;
MapType m2( m1 );
cout << m2.size() << endl;
}

If your own code really doesn't copy, then I would suspect bugs in the copy constructors or the comparison functions for the contained types are screwing up memory somehow.

Is it possible to use STL copy function with map

You could use std::transform instead:

template <typename T, typename U>
const U &extract_second(const std::pair<T,U> &p)
{
return p.second;
}

std::transform(test.begin(), test.end(), test_arr, extract_second<int,double>);

And as @Andre points out in a comment below, if you want a slightly more verbose overhead, you can avoid having to explicitly state the template arguments via a functor:

struct extract_second
{
template <typename T, typename U>
const U operator() (const std::pair<T,U> &p) const
{
return p.second;
}
};

std::transform(test.begin(), test.end(), test_arr, extract_second());

I'm sure there's a less-verbose solution using Boost binders, but I can't remember the syntax off the top of my head.

C++ How to make a shallow copy constructor for a map

To be honest, I have the feeling the actual problem is that you do not fully understand the difference between a shallow copy and deep copy in c++.

In very simple terms: shallow copy = copy the pointer, deep copy = copy what the pointer points to.

Your code is a variation of deep copy by taking what the pointer points to and create a new instance from that (assuming it would compile).

I will give you a simple example and leave the rest to you:

#include <iostream>

struct foo {
int * some_pointer;
void deep_copy( const foo& other) {
// copy the value
*some_pointer = *(other.some_pointer);
}
void shallow_copy( const foo& other) {
// make some_pointer point to the same as other.some_pointer
some_pointer = other.some_pointer;
}
};

int main() {
int x = 0;
int y = 42;
foo f{&x};
foo g{&y};
f.deep_copy(g);
y = 3;
std::cout << *f.some_pointer << "\n";
std::cout << *g.some_pointer << "\n";
f.shallow_copy(g);
y = 5;
std::cout << *f.some_pointer << "\n";
std::cout << *g.some_pointer << "\n";
}

This prints:

42
3
5
5

Because first f.deep_copy(g); copies the value and afterwards changing the value of y (which initially was bound to g) has no effect on f.

On the other hand, after f.shallow_copy(g); both f.some_pointer and g.some_pointer both point to y, hence modifiying y is reflected on both, f and g.



Related Topics



Leave a reply



Submit