std::list.insert usage

Discussion in 'C++' started by Tim Slattery, Jan 10, 2007.

  1. Tim Slattery

    Tim Slattery Guest

    It would be convenient for my app to store the stuff I'm generating in
    a std::list. I'd like to remember the location of a particular place
    in the list - sort of like sticking my finger into it - and insert an
    entry in that place some time later. There's an "insert" member
    function, but it takes an iterator to designate the place to insert.
    And I can't figure out how to get an iterator that points to the right
    place. For instance:

    std::list<int> mylist;
    std::list<int>::iterator myiter;
    mylist.push_back(1);
    mylist.push_back(2);
    mylist.push_back(3);
    mylist.push_back(4);
    mylist.push_back(5);
    myiter = mylist.end();
    mylist.push_back(6);
    mylist.push_back(7);
    mylist.push_back(8);
    mylist.push_back(9);

    mylist.insert(myiter, 100);

    for (myiter = mylist.begin(); myiter != mylist.end(); myiter++)
    {
    std::cout << *myiter << std::endl;
    }


    The first invocation of "mylist.end" seems to me like it should return
    an iterator pointing to element 6. But the result from this code is:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    100

    "myiter" continued pointing past the end of the list even after more
    elements were added! So, is there a way to remember a location so that
    I can insert something there later?

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, Jan 10, 2007
    #1
    1. Advertising

  2. Tim Slattery

    Ondra Holub Guest

    Tim Slattery napsal:
    > It would be convenient for my app to store the stuff I'm generating in
    > a std::list. I'd like to remember the location of a particular place
    > in the list - sort of like sticking my finger into it - and insert an
    > entry in that place some time later. There's an "insert" member
    > function, but it takes an iterator to designate the place to insert.
    > And I can't figure out how to get an iterator that points to the right
    > place. For instance:
    >
    > std::list<int> mylist;
    > std::list<int>::iterator myiter;
    > mylist.push_back(1);
    > mylist.push_back(2);
    > mylist.push_back(3);
    > mylist.push_back(4);
    > mylist.push_back(5);
    > myiter = mylist.end();
    > mylist.push_back(6);
    > mylist.push_back(7);
    > mylist.push_back(8);
    > mylist.push_back(9);
    >
    > mylist.insert(myiter, 100);
    >
    > for (myiter = mylist.begin(); myiter != mylist.end(); myiter++)
    > {
    > std::cout << *myiter << std::endl;
    > }
    >
    >
    > The first invocation of "mylist.end" seems to me like it should return
    > an iterator pointing to element 6. But the result from this code is:
    >
    > 1
    > 2
    > 3
    > 4
    > 5
    > 6
    > 7
    > 8
    > 9
    > 100
    >
    > "myiter" continued pointing past the end of the list even after more
    > elements were added! So, is there a way to remember a location so that
    > I can insert something there later?
    >
    > --
    > Tim Slattery
    >
    > http://members.cox.net/slatteryt


    end() returns iterator which points behind the last element. Use back()
    - it returns iterator pointing to the last element.
     
    Ondra Holub, Jan 10, 2007
    #2
    1. Advertising

  3. Tim Slattery wrote:
    ....
    >
    > "myiter" continued pointing past the end of the list even after more
    > elements were added! So, is there a way to remember a location so that
    > I can insert something there later?


    Yes - use:
    myiter = mylist.end();
    -- myiter;

    The "end" interator points to the mythical place one past the last
    element of the list. So your push backs are always made between the
    last element and end().
     
    Gianni Mariani, Jan 10, 2007
    #3
  4. Ondra Holub wrote:
    ....
    >
    > end() returns iterator which points behind the last element. Use back()
    > - it returns iterator pointing to the last element.
    >


    back() returns a reference, not an iterator.
     
    Gianni Mariani, Jan 10, 2007
    #4
  5. "Gianni Mariani" <> wrote in message
    news:45a55351$0$22065$...
    > Tim Slattery wrote:
    > ...
    >>
    >> "myiter" continued pointing past the end of the list even after more
    >> elements were added! So, is there a way to remember a location so that
    >> I can insert something there later?

    >
    > Yes - use:
    > myiter = mylist.end();
    > -- myiter;
    >
    > The "end" interator points to the mythical place one past the last element
    > of the list. So your push backs are always made between the last element
    > and end().
    >


    This makes sense as cont.push_back(element) is really defined as:
    cont.insert(cont.end(), element)
    and insert inserts elements right before the iterator. Therefore, end()
    always remains at the end of the list :)

    - Sylvester
     
    Sylvester Hesp, Jan 11, 2007
    #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. Replies:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
  2. Juha Nieminen
    Replies:
    22
    Views:
    1,033
    Kai-Uwe Bux
    Oct 12, 2007
  3. Replies:
    1
    Views:
    454
  4. Replies:
    1
    Views:
    428
    red floyd
    Dec 21, 2008
  5. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,025
    James Kanze
    Dec 22, 2008
Loading...

Share This Page