473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Vector Assign vs Vector operator=

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

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<doublev2(v1), but I'm giving an example above.

Thank you c++ users.
Feb 21 '07 #1
4 6881
Chris Roth wrote:
vector<doublev1(5,1);
vector<doublev2;

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<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
Feb 21 '07 #2
Chris Roth wrote:
vector<doublev1(5,1);
vector<doublev2;

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<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_into_v1;
vector<double>::iterator end_iter = some_other_iterator_into_v1;

v2.assign(start_iter, end_iter);

Feb 21 '07 #3
On Feb 21, 4:40 pm, red floyd <no.s...@here.dudewrote:
Chris Roth wrote:
vector<doublev1(5,1);
vector<doublev2;
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<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_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<intv;
list<intll;

// fill ll here

v.assign(ll.begin(), ll.end());

Feb 21 '07 #4
red floyd wrote:
Chris Roth wrote:
>vector<doublev1(5,1);
vector<doublev2;

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<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.
Feb 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: scooter | last post by:
what happens memory wise with this scenerio: std::vector<int> vecInt(20, 999); VecInt.assign(20, 0); do all the 999 values get overwritten or does the vector reassign the memory lso?
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
1
by: robk | last post by:
Hi, Could someone know what is wrong with my code. First of all what I'm trying to do. I have (as can be seen) declared typedef's of vector STL. I want each value in vector which is part of...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
6
by: hannibal200480 | last post by:
Hi everyone, Here about what it is. vector<vector<Point3D> > pVector0; pVector0 is a vector of vector of 3D points. pVector0 is a vector of 3D points.
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
3
by: Holgerson | last post by:
Hi everybody, I wonder if someone knows how to implement an operator* into NRVec and/ or NRMat, respectively, that can perform these kind of operations: Matrix*Vector and Vector*Matrix. Thanks a...
19
by: Ramon F Herrera | last post by:
Newbie alert: I come from the C side, struggling to learn C++. I need a two-dimensional data structure. I first tried a regular vector and added the 2nd dimension with my own structs and...
1
by: Emile van Sebille | last post by:
Prasad, Mrunalini wrote: Well, what are you trying to do here? SyntaxError:can't assign to operator Emile
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.