Vector Assign vs Vector operator=

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Roth

    Vector Assign vs Vector operator=

    vector<doublev1 (5,1);
    vector<doublev2 ;

    v2 = v1; // 1
    v2.assign(v1.be gin(),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<doublev2 (v1), but I'm giving an example above.

    Thank you c++ users.
  • John Harrison

    #2
    Re: Vector Assign vs Vector operator=

    Chris Roth wrote:
    vector<doublev1 (5,1);
    vector<doublev2 ;
    >
    v2 = v1; // 1
    v2.assign(v1.be gin(),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<doublev2 (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

    Comment

    • red floyd

      #3
      Re: Vector Assign vs Vector operator=

      Chris Roth wrote:
      vector<doublev1 (5,1);
      vector<doublev2 ;
      >
      v2 = v1; // 1
      v2.assign(v1.be gin(),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<doublev2 (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<doublev1 ;
      vector<doublev2 ;

      // fill v1 here.

      vector<double>: :iterator start_iter = some_iterator_i nto_v1;
      vector<double>: :iterator end_iter = some_other_iter ator_into_v1;

      v2.assign(start _iter, end_iter);

      Comment

      • jlongstreet@gmail.com

        #4
        Re: Vector Assign vs Vector operator=

        On Feb 21, 4:40 pm, red floyd <no.s...@here.d udewrote:
        Chris Roth wrote:
        vector<doublev1 (5,1);
        vector<doublev2 ;
        >
        v2 = v1; // 1
        v2.assign(v1.be gin(),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<doublev2 (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<doublev1 ;
        vector<doublev2 ;
        >
        // fill v1 here.
        >
        vector<double>: :iterator start_iter = some_iterator_i nto_v1;
        vector<double>: :iterator end_iter = some_other_iter ator_into_v1;
        >
        v2.assign(start _iter, end_iter);
        Also, using assign allows you to assign across container types:

        vector<intv;
        list<intll;

        // fill ll here

        v.assign(ll.beg in(), ll.end());

        Comment

        • Ron Natalie

          #5
          Re: Vector Assign vs Vector operator=

          red floyd wrote:
          Chris Roth wrote:
          >vector<doublev 1(5,1);
          >vector<doublev 2;
          >>
          >v2 = v1; // 1
          >v2.assign(v1.b egin(),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<doublev2 (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.

          Comment

          Working...