Vector Assign vs Vector operator=

Discussion in 'C++' started by Chris Roth, Feb 21, 2007.

  1. Chris Roth

    Chris Roth Guest

    vector<double> v1(5,1);
    vector<double> v2;

    v2 = v1; // 1
    v2.assign(v1.begin(),v1.end()); // 2

    Are 1 and 2 the same, or are their subtle differences between them.
    Which is preferable, if either? And yes, I know I could use the
    construction vector<double> v2(v1), but I'm giving an example above.

    Thank you c++ users.
     
    Chris Roth, Feb 21, 2007
    #1
    1. Advertising

  2. Chris Roth wrote:
    > vector<double> v1(5,1);
    > vector<double> v2;
    >
    > v2 = v1; // 1
    > v2.assign(v1.begin(),v1.end()); // 2
    >
    > Are 1 and 2 the same, or are their subtle differences between them.
    > Which is preferable, if either? And yes, I know I could use the
    > construction vector<double> v2(v1), but I'm giving an example above.
    >
    > Thank you c++ users.


    No observable difference between them, but 1 is clearly better since it
    is clearer to any reader of the code. With 2 you have to check the
    arguments to understand the meaning.

    john
     
    John Harrison, Feb 21, 2007
    #2
    1. Advertising

  3. Chris Roth

    red floyd Guest

    Chris Roth wrote:
    > vector<double> v1(5,1);
    > vector<double> v2;
    >
    > v2 = v1; // 1
    > v2.assign(v1.begin(),v1.end()); // 2
    >
    > Are 1 and 2 the same, or are their subtle differences between them.
    > Which is preferable, if either? And yes, I know I could use the
    > construction vector<double> v2(v1), but I'm giving an example above.


    As John said, there's probably no detectable difference. The latter
    construct (assign) is more useful when you want to copy a subvector.

    e.g.:

    vector<double> v1;
    vector<double> v2;

    // fill v1 here.

    vector<double>::iterator start_iter = some_iterator_into_v1;
    vector<double>::iterator end_iter = some_other_iterator_into_v1;

    v2.assign(start_iter, end_iter);
     
    red floyd, Feb 21, 2007
    #3
  4. Chris Roth

    Guest

    On Feb 21, 4:40 pm, red floyd <> wrote:
    > Chris Roth wrote:
    > > vector<double> v1(5,1);
    > > vector<double> v2;

    >
    > > v2 = v1; // 1
    > > v2.assign(v1.begin(),v1.end()); // 2

    >
    > > Are 1 and 2 the same, or are their subtle differences between them.
    > > Which is preferable, if either? And yes, I know I could use the
    > > construction vector<double> v2(v1), but I'm giving an example above.

    >
    > As John said, there's probably no detectable difference. The latter
    > construct (assign) is more useful when you want to copy a subvector.
    >
    > e.g.:
    >
    > vector<double> v1;
    > vector<double> v2;
    >
    > // fill v1 here.
    >
    > vector<double>::iterator start_iter = some_iterator_into_v1;
    > vector<double>::iterator end_iter = some_other_iterator_into_v1;
    >
    > v2.assign(start_iter, end_iter);


    Also, using assign allows you to assign across container types:

    vector<int> v;
    list<int> ll;

    // fill ll here

    v.assign(ll.begin(), ll.end());
     
    , Feb 21, 2007
    #4
  5. Chris Roth

    Ron Natalie Guest

    red floyd wrote:
    > Chris Roth wrote:
    >> vector<double> v1(5,1);
    >> vector<double> v2;
    >>
    >> v2 = v1; // 1
    >> v2.assign(v1.begin(),v1.end()); // 2
    >>
    >> Are 1 and 2 the same, or are their subtle differences between them.
    >> Which is preferable, if either? And yes, I know I could use the
    >> construction vector<double> v2(v1), but I'm giving an example above.

    >
    > As John said, there's probably no detectable difference. The latter
    > construct (assign) is more useful when you want to copy a subvector.
    >

    In case 1, v1 must be a vector.
    In case 2, v1 can be anything provided that the iterators returned
    are of a type that's insertable into v2.
     
    Ron Natalie, Feb 22, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rim
    Replies:
    20
    Views:
    715
    Corey Coughlin
    Jul 8, 2003
  2. Replies:
    2
    Views:
    897
    Fredrik Lundh
    Jun 7, 2006
  3. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
  4. Replies:
    2
    Views:
    2,261
    John Harrison
    May 26, 2007
  5. Emile van Sebille

    Re: Error:can't assign to operator

    Emile van Sebille, Aug 1, 2008, in forum: Python
    Replies:
    1
    Views:
    472
Loading...

Share This Page