Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to obtain iterator to beginning of inserted elements in list

Reply
Thread Tools

How to obtain iterator to beginning of inserted elements in list

 
 
JurgenvonOerthel@hotmail.com
Guest
Posts: n/a
 
      12-07-2007
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 8:17 pm, (E-Mail Removed) 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
}
 
Reply With Quote
 
JurgenvonOerthel@hotmail.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 4:30 pm, "Victor Bazarov" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
will an iterator to a map becomes invalid when an element is inserted into the map wolverine C++ 3 07-31-2006 12:24 PM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM



Advertisments