473,503 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about vector and iterator

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
4 3057

"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1370
by: James W. Walker | last post by:
I can't understand why I'm getting an error message from code like this... #include <vector> template <class T> struct Foo { typedef int (T::*Method)( int i ); typedef int (*Func)( int i );
15
6555
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
3
2898
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
9
5180
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
5
2555
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
4
5659
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
3
1555
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
7
5746
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
3
2862
by: chsalvia | last post by:
I have a question about the design of STL vector. One thing I wonder was why the STL designers chose to have the insert() and erase() functions take an iterator as the first argument, rather than...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.