473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Feb 21 '07 #1
4 6917
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
Feb 21 '07 #2
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);

Feb 21 '07 #3
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());

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

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

Similar topics

5
7380
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
2317
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 doubles using new? And in cleaning up, manually delete each of these vectors of doubles? Thanks,
1
1667
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 t_set of vectors to be part of new vector for future use. But I want to reuse that variable vector by clearing or erasing the values form it.
5
10559
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 the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I...
6
4208
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
3969
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 large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the...
3
2145
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 lot, Holger
19
1511
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 pointers. I couldn't make it work. Too many illegal assignments. My next attempt was to create a vector of vector (or list of vector, etc.). The compiler...
1
5688
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
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.