445,655 Members | 962 Online Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

# doubt with stl vector

 P: 6 if I have : vector one(4); vector 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 one; vector 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 Replies

 Expert 100+ P: 1,510 no obvious reason why assignment should not work do you initialise one and two in the case Expand|Select|Wrap|Line Numbers vector one; vector two;   one.reserve(4); two.reserve(4);   if so what with what values and what are the values of the elements of one after the assignment Expand|Select|Wrap|Line Numbers one = two; Feb 24 '07 #2

 P: 6 Yeah I mentioned that in the post that I initialize the elements of one and two with different integer values. say something like one = one = one = one = 0; two = two = two = two = 1; and after we do: one = two the values of one[] are still the same old values i.e. "0" Feb 24 '07 #3

 Expert 100+ P: 1,510 when I run the following program Expand|Select|Wrap|Line Numbers #include  #include  using namespace std; int main() {   vector one(4);   vector two(4);   one.reserve(4);   two.reserve(4);   one = one = one = one = 0;   two = two = two = two = 1;    cout << "\n one " ;    for (int i=0; i< one.size() ; i++) cout << one[i] << " ";    cout << "\n two " ;    for (int i=0; i< two.size() ; i++) cout << two[i]  << " ";    cout << endl<< endl;      one = two;               // ** assign two to one    cout << "\n one " ;    for (int i=0; i< one.size() ; i++) cout << one[i] << " ";    cout << "\n two " ;    for (int i=0; i< two.size() ; i++) cout << two[i]  << " ";    cout << endl<< endl;    system("pause"); }   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 