T
Tran Tuan Anh
Dear all,
I am new in C++, and now get confused about a lot of things.
I wrote this simple code to test the vector.
class Temp { public: int x; };
int main() {
vector<Temp> v;
vector<Temp>::iterator it;
Temp t1, t2, t3;
t1.x = 1; t2.x = 2; t3.x = 3;
v.push_back(t1); v.push_back(t2); v.push_back(t3);
t1.x = 5; t2.x = 5; t3.x = 5;
for (it = v.begin(); it != v.end(); it++) {
cout << (*it).x << endl;
}
}
And it prints out "1 2 3". It means when I push_back() an object.
The vector copies the object I push in, hence vector does not contain
t1, t2, and t3 but copies of them.
This is very strange to me.. Hence now it is very hard to track
which object is my original object???
Is there anyway to make vector contains the same objects I push in?
However when I look at the reference, push_back() is:
void push_back(const T& x);
It means the values are passed by reference not by value, how come
in reality it does duplicate the object?
Hope someone can clear my doubts!
I really got lost..
Tuan Anh
I am new in C++, and now get confused about a lot of things.
I wrote this simple code to test the vector.
class Temp { public: int x; };
int main() {
vector<Temp> v;
vector<Temp>::iterator it;
Temp t1, t2, t3;
t1.x = 1; t2.x = 2; t3.x = 3;
v.push_back(t1); v.push_back(t2); v.push_back(t3);
t1.x = 5; t2.x = 5; t3.x = 5;
for (it = v.begin(); it != v.end(); it++) {
cout << (*it).x << endl;
}
}
And it prints out "1 2 3". It means when I push_back() an object.
The vector copies the object I push in, hence vector does not contain
t1, t2, and t3 but copies of them.
This is very strange to me.. Hence now it is very hard to track
which object is my original object???
Is there anyway to make vector contains the same objects I push in?
However when I look at the reference, push_back() is:
void push_back(const T& x);
It means the values are passed by reference not by value, how come
in reality it does duplicate the object?
Hope someone can clear my doubts!
I really got lost..
Tuan Anh