How to obtain iterator to beginning of inserted elements in list

Discussion in 'C++' started by JurgenvonOerthel@hotmail.com, Dec 7, 2007.

  1. Guest

    I want to replace one element in a list<string> by a list<string> and
    I need to obtain an iterator to the first element in the inserted
    list.
    In code:

    void replace_element_by_list(list<string> &my_list,
    list<string>::iterator
    &iter_to_remove,
    const list<string>
    &list_to_insert) {
    list<string>::iterator next_iter = my_list.erase(iter_to_remove);
    my_list.insert(next_iter, list_to_insert.begin(),
    list_to_insert.end());
    // Does 'iter_to_remove' point to the first element of the inserted
    list, or is it invalid?
    }

    When I try this code I find that 'iter_to_remove' indeed points to the
    first element of the inserted list. However, I'm wondering whether
    that is guaranteed.
     
    , Dec 7, 2007
    #1
    1. Advertising

  2. wrote:
    > I want to replace one element in a list<string> by a list<string> and
    > I need to obtain an iterator to the first element in the inserted
    > list.
    > In code:
    >
    > void replace_element_by_list(list<string> &my_list,
    > list<string>::iterator
    > &iter_to_remove,
    > const list<string>
    > &list_to_insert) {
    > list<string>::iterator next_iter = my_list.erase(iter_to_remove);


    Add
    list<string>::iterator prev_iter = next_iter;
    if (prev_iter == my_list.begin())
    prev_iter = my_list.end();
    else
    --prev_iter;

    > my_list.insert(next_iter, list_to_insert.begin(),
    > list_to_insert.end());


    What does 'insert' return?

    > // Does 'iter_to_remove' point to the first element of the inserted
    > list, or is it invalid?


    'iter_to_remove' is invalid. However, you can return 'prev_iter' here.
    The catch, of course, is that if you're removing the very first element
    in the list, you'll get 'end()'.

    > }
    >
    > When I try this code I find that 'iter_to_remove' indeed points to the
    > first element of the inserted list. However, I'm wondering whether
    > that is guaranteed.


    Nope.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 7, 2007
    #2
    1. Advertising

  3. On Dec 7, 8:17 pm, wrote:
    > I want to replace one element in a list<string> by a list<string> and
    > I need to obtain an iterator to the first element in the inserted
    > list.
    > In code:
    >
    > void replace_element_by_list(list<string> &my_list,
    > list<string>::iterator
    > &iter_to_remove,
    > const list<string>
    > &list_to_insert) {
    > list<string>::iterator next_iter = my_list.erase(iter_to_remove);
    > my_list.insert(next_iter, list_to_insert.begin(),
    > list_to_insert.end());
    > // Does 'iter_to_remove' point to the first element of the inserted
    > list, or is it invalid?
    >
    > }
    >
    > When I try this code I find that 'iter_to_remove' indeed points to the
    > first element of the inserted list. However, I'm wondering whether
    > that is guaranteed.


    This is not guaranteed. It is invalid. You could, however, get the
    iterator to the first element of the inserted range as below:

    void replace_element_by_list(list<string> &my_list,
    list<string>::iterator
    &iter_to_remove,
    const list<string>
    &list_to_insert) {
    list<string>::iterator next_iter = my_list.erase(iter_to_remove);
    list<string>::iterator prev_to_next_iter = --next_iter;
    my_list.insert(next_iter, list_to_insert.begin(),
    list_to_insert.end());
    // Does 'iter_to_remove' point to the first element of the inserted
    list, or is it invalid?
    iter_to_remove = ++prev_to_next_iter;
    //iter_to_remove is guaranteed to point to the first element of the
    //inserted list/range
    }
     
    Abhishek Padmanabh, Dec 7, 2007
    #3
  4. Guest

    On Dec 7, 4:30 pm, "Victor Bazarov" <> wrote:
    > Add
    > list<string>::iterator prev_iter = next_iter;
    > if (prev_iter == my_list.begin())
    > prev_iter = my_list.end();
    > else
    > --prev_iter;

    I was afraid I would have to do something ugly like this.

    > What does 'insert' return?

    That's my point: it returns void. I think it should return an iterator
    to the first inserted element. That's what 'insert' of a single
    element does.
     
    , Dec 7, 2007
    #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. David
    Replies:
    3
    Views:
    1,768
    Peter van Merkerk
    Oct 5, 2004
  2. Replies:
    6
    Views:
    665
    Jim Langston
    Oct 30, 2005
  3. Adam Hartshorne
    Replies:
    2
    Views:
    381
    Nitin Motgi
    Jan 27, 2006
  4. wolverine
    Replies:
    3
    Views:
    1,090
    Chris
    Jul 31, 2006
  5. Jesse B.
    Replies:
    9
    Views:
    244
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page