about vector and iterator

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
 
M

Mike Wahler

Tran Tuan Anh said:
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.
Yes.

This is very strange to me..

Get used to it. :) This is by design.
Hence now it is very hard to track
which object is my original object???

Your 'original' objects are still 't1', 't2', and 't3'.
Is there anyway to make vector contains the same objects I push in?

No, but you could store pointers to them. But what
exactly is your purpose for this?
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?

That's the way standard library containers work, by design.
Hope someone can clear my doubts!
I really got lost..

What specific problem are you trying to solve that a vector
does not do for you?

-Mike
 
D

David Rubin

Tran said:
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. [snip]
Is there anyway to make vector contains the same objects I push in?

vector<Temp*> v; // holds pointers to Temps
Temp v1;

t1.x = 1;
v.push_back(&t1);
t1.x = 5; // changes value in v

/david
 
A

Artie Gold

Tran said:
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.

Right. This is how containers such as std::vector work -- which is
also why objects that you place in containers need to be copyable.
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?

You could make it a std::vector<Temp *> and pass in pointers to the
desired objects. In some cases, this is the way to go -- but it also
puts the burden of memory management on the client code.
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?
Design decision. One of the great advantages of std::vector, std::list,
etc. is the fact that the programmer need not be concerned with
explicit
memory management.

HTH,
--ag
 
M

Mike Wahler

Mike Wahler said:
Get used to it. :) This is by design.


Your 'original' objects are still 't1', 't2', and 't3'.


No, but you could store pointers to them. But what
exactly is your purpose for this?


That's the way standard library containers work, by design.


What specific problem are you trying to solve that a vector
does not do for you?

-Mike

I'm speculating that perhaps you simply didn't want your
'original' objects left around 'cluttering' things up.

You can store those same object instances without leaving
any 'residue', by using a constructor to create 'temporary'
objects, which will be automatically destroyed after they're
copied into the vector:

#include <iostream>
using std::cout;

#include <ostream>
using std::endl;

#include <vector>
using std::vector;

class Temp
{
public:
Temp(int arg) : x(arg) {} // constructor
int x;
};

int main() {
vector<Temp> v;
vector<Temp>::iterator it;

v.push_back(Temp(1));
// the argument to 'push_back()' creates an (unnamed)
// temporary type 'Temp' object with member 'x' set
// to 1. After 'push_back()' returns, this unnamed
// temporary object is destroyed, (but a copy of it
// remains in the vector)

v.push_back(Temp(2));
v.push_back(Temp(3));

for (it = v.begin(); it != v.end(); it++) {
cout << (*it).x << endl;
}
}


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top