Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy content of one STL container to another

Tags:

c++

How could I create a copy of an unordered_map from an existing one? Could I use assignment operator or I need to iterate it one by one? Also, I am using shared_ptr as the value in the map. Did I need to take extra care because it is shared_ptr?

typedef unordered_map<string, shared_ptr<classA>>MAP1;
MAP1 map1;
map1["abc"] = make_shared<classA>();
MAP2 map2 = map1; ?? //can I use assignment operator??

Thanks.

like image 688
Leslieg Avatar asked Sep 06 '25 03:09

Leslieg


1 Answers

Yes you can.

Regarding shared_ptr, if you want the copied pointers to point to the same values then no. If you want two separate copy (deep copy), then you need to do the copying yourself.

like image 90
Dat Chu Avatar answered Sep 07 '25 22:09

Dat Chu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!