about vector and iterator

Discussion in 'C++' started by Tran Tuan Anh, Nov 5, 2003.

  1. 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
    Tran Tuan Anh, Nov 5, 2003
    #1
    1. Advertising

  2. Tran Tuan Anh

    Mike Wahler Guest

    "Tran Tuan Anh" <> wrote in message
    news:...
    > 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
    Mike Wahler, Nov 5, 2003
    #2
    1. Advertising

  3. Tran Tuan Anh

    David Rubin Guest

    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."
    David Rubin, Nov 5, 2003
    #3
  4. Tran Tuan Anh

    Artie Gold Guest

    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.
    Artie Gold, Nov 5, 2003
    #4
  5. Tran Tuan Anh

    Mike Wahler Guest

    Re: (cont.) about vector and iterator

    "Mike Wahler" <> wrote in message
    news:Ha%pb.306$...
    >
    > "Tran Tuan Anh" <> wrote in message
    > news:...
    > > 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
    Mike Wahler, Nov 5, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,768
  2. Replies:
    8
    Views:
    1,911
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    557
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,573
    Francesco S. Carta
    Sep 7, 2010
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    354
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page