Deep copy vector <Point> myArr

To make a deep copy of myArr ,

 vector <Point> myArr; 

where Point is a class with 2 ints as members,

Do I need to do something? or ok with

 vector <Point> otherArr = myArr; 

I need to delete multiple points in otherArr , but at the same time I need all the points in myArr for later use.

early

+4
source share
3 answers

See Shallow and Deep Copy and Effective C ++

Point does not need a deep copy. As a rule of thumb, a deep copy is required when the class has pointer elements. The Point class has only two int members, so it does not require much effort for a "deep copy", a normal or "shallow copy" will work fine. In fact, it is not required to write a copy constructor for Point , by default or synthesized, provided by the compiler, it will be very good.

As for your second question, after the line

 vector< Point > otherArr = myArr; 
Performed

otherArr and myArr are two independent independents . Operations (for example, deleting some elements) performed on one of them do not affect the others in any way.

+7
source

The appointment should be perfect. It ensures that all data is copied to the right. Just make sure the dot is copied.

+1
source

Are you all right. The vector has an overloaded assignment operator and copy constructor that perform deep copy.

+1
source

All Articles