JurgenvonOerthel@hotmail.com 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