By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,798 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,798 IT Pros & Developers. It's quick & easy.

about vector and iterator

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Tran Tuan Anh" <an***@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
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
Jul 19 '05 #2

P: n/a
Tran Tuan Anh wrote:
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

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 19 '05 #3

P: n/a
Tran Tuan Anh wrote:
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
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #4

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ha**************@newsread4.news.pas.earthlink .net...

"Tran Tuan Anh" <an***@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
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


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

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.