how to take a reference of inserted items?

Discussion in 'C++' started by lallous, Oct 13, 2004.

  1. lallous

    lallous Guest

    Hello

    Given this:

    map<int, mystruct> m;
    m[4] = item;
    item.id++;
    m[2] = item;

    How can I get a direct refrence to the location of the just inserted item?
    (which is now managed by the map)

    I would do a find() then get a reference to its "second".

    Same question <list> <vector> how can I get reference directly after
    insertion? is the back() / front() the only way for this case?

    --
    Elias
     
    lallous, Oct 13, 2004
    #1
    1. Advertising

  2. lallous wrote:
    > Given this:
    >
    > map<int, mystruct> m;
    > m[4] = item;
    > item.id++;
    > m[2] = item;
    >
    > How can I get a direct refrence to the location of the just inserted item?
    > (which is now managed by the map)
    >
    > I would do a find() then get a reference to its "second".


    That's not a bad idea.

    > Same question <list> <vector> how can I get reference directly after
    > insertion? is the back() / front() the only way for this case?


    No. What if you inserted in the middle?

    Dereferencing a non-const iterator gives you a reference. So, if you
    use 'find' and then do

    list<T>::iterator myObjIter = mylist.find(someT);
    if (myObjIter != mylist.end()) {
    T & myObjectRef = *myObjIter;
    ... // do something with 'myObjectRef'
    }

    However, keep in mind that references do become invalid, more often in
    a 'vector' than in a 'list', but still.

    Victor
     
    Victor Bazarov, Oct 13, 2004
    #2
    1. Advertising

  3. lallous

    Ron Natalie Guest

    lallous wrote:
    > Hello
    >
    > Given this:
    >
    > map<int, mystruct> m;
    > m[4] = item;
    > item.id++;
    > m[2] = item;
    >

    operator[] returns such a reference.
     
    Ron Natalie, Oct 13, 2004
    #3
  4. lallous wrote:

    > Hello
    >
    > Given this:
    >
    > map<int, mystruct> m;
    > m[4] = item;
    > item.id++;
    > m[2] = item;
    >
    > How can I get a direct refrence to the location of the just
    > inserted item? (which is now managed by the map)
    >
    > I would do a find() then get a reference to its "second".
    >
    > Same question <list> <vector> how can I get reference directly
    > after insertion? is the back() / front() the only way for this
    > case?


    All of them have an insert() method that returns an iterator, so:

    vector<int> v(3, 0);
    list<int> l;
    map<char, int> m;
    int& rv = *v.insert(v.begin() + 1, 35);
    int& rl = *l.insert(l.end(), 17);
    int& rm = m.insert(make_pair('k', 80)).first->second;


    Martin

    --
    Quidquid latine dictum sit, altum viditur.
     
    Martin Eisenberg, Oct 13, 2004
    #4
    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. Gilles Kuhn
    Replies:
    0
    Views:
    456
    Gilles Kuhn
    Sep 15, 2003
  2. Bart Van Hemelen
    Replies:
    2
    Views:
    4,717
    Bart Van Hemelen
    Sep 4, 2006
  3. Larry Bud
    Replies:
    13
    Views:
    7,811
    Larry Bud
    Jan 7, 2008
  4. Ben
    Replies:
    2
    Views:
    954
  5. Steven D'Aprano

    Take the first n items of an iterator

    Steven D'Aprano, Jan 4, 2009, in forum: Python
    Replies:
    2
    Views:
    307
    Steven D'Aprano
    Jan 4, 2009
Loading...

Share This Page