PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to obtain iterator to beginning of inserted elements in list
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to obtain iterator to beginning of inserted elements in list

Réponse
 
LinkBack Outils de la discussion
Vieux 07/12/2007, 15h17   #1
JurgenvonOerthel@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 07/12/2007, 15h30   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to obtain iterator to beginning of inserted elements in list

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


  Réponse avec citation
Vieux 07/12/2007, 15h38   #3
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to obtain iterator to beginning of inserted elements in list

On Dec 7, 8:17 pm, JurgenvonOert...@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);
> 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
}
  Réponse avec citation
Vieux 07/12/2007, 15h40   #4
JurgenvonOerthel@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to obtain iterator to beginning of inserted elements in list

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11370 seconds with 12 queries