How does this work?...assignment using vectors...

This is a discussion on How does this work?...assignment using vectors... within the C++ Programming forums, part of the General Programming Boards category; ..." A vector is a single object that can be assigned. For example: Code: void f(vector<Entry>& v) { vector<Entry> v2 ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    How does this work?...assignment using vectors...

    ..." A vector is a single object that can be assigned. For example:

    Code:
    void f(vector<Entry>& v)
    {
    	vector<Entry> v2 = phone_book;
    	v = v2;
    	//...
    }
    ... " Assigning a vector involves copying its elements. Thus, after the initialization and assignment in
    f(), v and v2 each holds a seperate copy of every Entry in the phone book. When a vector holds many
    elements, such innocent-looking assignments and initialization can be prohibihtively expensive. Where
    copying is indesirable, refrences or pointers should be used."

  2. #2
    Unregistered
    Guest
    a vector is a class that extends the native array container to include things like self expansion, etc. With arrays, you cannot assign one to another, but with vectors you can, because the assignment operator has been overloaded to implement it. The implementation is probably something like:

    find length of source vector.
    make sure there is adequate length in destination vector
    use a loop to assign each element of source vector into destination vector

    This is pretty much how you would copy one array into another, except it is all written out for you already in the vector class' assignment operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. Vectors work when they want to.
    By bobbelPoP in forum C++ Programming
    Replies: 40
    Last Post: 07-30-2008, 02:00 PM
  4. assignment operator for static array inside a class
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2008, 11:11 AM
  5. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM

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