assigning one vector to another

This is a discussion on assigning one vector to another within the C++ Programming forums, part of the General Programming Boards category; If I have a vector and want to assign its contents to another vector (ie. vector2 = vector1), do I ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    assigning one vector to another

    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?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    vector2 = vector1 should work fine.

    If you don't need vector1 any more, consider using swap() instead, to avoid the unnecessary copy.

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Ability Scores
    By SteelySam in forum C++ Programming
    Replies: 1
    Last Post: 07-10-2008, 03:16 PM
  2. silence warning when assigning pointers
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 12:18 PM
  3. Confusion with assigning values to structs.
    By Tronic in forum C++ Programming
    Replies: 9
    Last Post: 03-22-2004, 08:01 AM
  4. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM
  5. Assigning the value of a const array to a normal array
    By Accident Prone in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2003, 11:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21