If I have a vector and want to assign its contents to another vector (ie. vector2 = vector1), do I have to loop and push_back or is there a simplier way?
Printable View
If I have a vector and want to assign its contents to another vector (ie. vector2 = vector1), do I have to loop and push_back or is there a simplier way?
vector2 = vector1 should work fine.
If you don't need vector1 any more, consider using swap() instead, to avoid the unnecessary copy.
yes the assignment operator will work but also remember it's a shallow copy, so if you delete any pointers that you added to vector1, they will be deleted for vector2 as well.