How to obtain iterator to beginning of inserted elements in list



 DEVELOP > c-Plus-Plus > How to obtain iterator to beginning of inserted elements in list

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 07 Dec 2007 09:17:43 AM
Object: How to obtain iterator to beginning of inserted elements in list
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.
.

User: "Victor Bazarov"

Title: Re: How to obtain iterator to beginning of inserted elements in list 07 Dec 2007 09:30:07 AM
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
.
User: ""

Title: Re: How to obtain iterator to beginning of inserted elements in list 07 Dec 2007 09:40:24 AM
On Dec 7, 4:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> 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.
.


User: "Abhishek Padmanabh"

Title: Re: How to obtain iterator to beginning of inserted elements in list 07 Dec 2007 09:38:26 AM
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
}
.


  Page 1 of 1

1

 


Related Articles
how to erase a list member with just an iterator (without the list itself)?
How to convert from std::list::iterator to std::list::iterator?
How the comparison operators are defined for iterator and const_iterator
How to use the iterator from base class in a derived class?
How to resolve ADL(?) issue using std::copy and std::ostream_iterator
How does std::set implement iterator through red black tree?
how can I access data with iterator?
How can I remove an item in STL iterator?
How to test if an iterator is at the end of the container
How to define function which operates on iterator convertible toT*
How to use map::iterator
How do i remove an element in the middle of a Vector? (without iterator usage)
How to use istream_iterator to read input from cin twice?
How to loop in a vector (set, map, etc) from iterator i to iteratorj
How can I erase() using a reverse iterator?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER