Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Vector Assign vs Vector operator=

Reply
Thread Tools

Vector Assign vs Vector operator=

 
 
Chris Roth
Guest
Posts: n/a
 
      02-21-2007
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-21-2007
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      02-21-2007
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);

 
Reply With Quote
 
jlongstreet@gmail.com
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 4:40 pm, red floyd <(E-Mail Removed)> 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());

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      02-22-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
std::vector::assign scooter C++ 5 09-19-2003 08:51 AM



Advertisments