473,326 Members | 2,438 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,326 software developers and data experts.

doubt with stl vector

if I have :

vector<int> one(4);
vector<int> two(4);

and say we assigned 0s to all the elements of one and 1s to all the element of two. and than we do :

one = two;

in this case the contents of two are copied to contents of one.

But instead if we do like this :

vector<int> one;
vector<int> two;

one.reserve(4);
two.reserve(4);

and than do the same assignments than the contents of two are not copied to the vector one...


why does this happen ?

thanks in advance.
Feb 24 '07 #1
3 1128
horace1
1,510 Expert 1GB
no obvious reason why assignment should not work
do you initialise one and two in the case
Expand|Select|Wrap|Line Numbers
  1. vector<int> one;
  2. vector<int> two;
  3.  
  4. one.reserve(4);
  5. two.reserve(4);
  6.  
if so what with what values and what are the values of the elements of one after the assignment
Expand|Select|Wrap|Line Numbers
  1. one = two;
Feb 24 '07 #2
Yeah I mentioned that in the post that I initialize the elements of one and two with different integer values.

say something like

one[0] = one[1] = one[2] = one[3] = 0;
two[0] = two[1] = two[2] = two[3] = 1;

and after we do:

one = two

the values of one[] are still the same old values i.e. "0"
Feb 24 '07 #3
horace1
1,510 Expert 1GB
when I run the following program
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6.   vector<int> one(4);
  7.   vector<int> two(4);
  8.   one.reserve(4);
  9.   two.reserve(4);
  10.   one[0] = one[1] = one[2] = one[3] = 0;
  11.   two[0] = two[1] = two[2] = two[3] = 1;
  12.    cout << "\n one " ;
  13.    for (int i=0; i< one.size() ; i++) cout << one[i] << " ";
  14.    cout << "\n two " ;
  15.    for (int i=0; i< two.size() ; i++) cout << two[i]  << " ";
  16.    cout << endl<< endl;
  17.  
  18.    one = two;               // ** assign two to one
  19.    cout << "\n one " ;
  20.    for (int i=0; i< one.size() ; i++) cout << one[i] << " ";
  21.    cout << "\n two " ;
  22.    for (int i=0; i< two.size() ; i++) cout << two[i]  << " ";
  23.    cout << endl<< endl;
  24.    system("pause");
  25. }
  26.  
it gives
one 0 0 0 0
two 1 1 1 1

one 1 1 1 1
two 1 1 1 1
Press any key to continue . . .

tried it with gcc and Borlan CBuilder 5 - which compiler are you using?
post your code and we can try it?
Feb 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
1
by: edu.mvk | last post by:
Hi I am using a "vector" in my code and i added "#include <vector>" in my header file. my code has the declaration as: *************************************** vector<Gerneric_Class*var_name;...
2
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
1
by: wolverine | last post by:
Hi, I have read that to erase an element from a vector with reverse_iterator we have to use -- vector.erase( (++reverseItr).base()) -- But assuming i have to delete the first element of the...
6
by: amscompit | last post by:
I have a written the following code. #include<iomanip> #include<fstream> #include<vector> #include<cctype> using namespace std;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.