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 > Vector erase dumps core when vector size is 2
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Vector erase dumps core when vector size is 2

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2007, 23h01   #1
Anil
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Vector erase dumps core when vector size is 2

I am facing problem while erasing an elemet from stl vector when its
size is 2. It works fine when SIZE > 2.
Can anybody me in this?? Following is the sample code which i
tried.

#include <iostream>
#include <vector>

using namespace std;
#define SIZE 2
main()
{
vector<int> myVect;
std::vector<int>::iterator iter;

for(int i =0; i<SIZE; i++)
myVect.push_back(i);

cout<< myVect.size() << "\n";
for( iter = myVect.begin(); iter != myVect.end(); iter++)
cout<< *iter << " ";

cout<<"\n";

for( iter = myVect.begin(); iter != myVect.end(); iter++)
{
cout<< *iter << "\n";
if( *iter == 1)
myVect.erase(iter);

}

return 0;
}
  Réponse avec citation
Vieux 05/12/2007, 23h13   #2
Mark P
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Vector erase dumps core when vector size is 2

Anil wrote:
> I am facing problem while erasing an elemet from stl vector when its
> size is 2. It works fine when SIZE > 2.
> Can anybody me in this?? Following is the sample code which i
> tried.
>
> #include <iostream>
> #include <vector>
>
> using namespace std;
> #define SIZE 2
> main()
> {
> vector<int> myVect;
> std::vector<int>::iterator iter;
>
> for(int i =0; i<SIZE; i++)
> myVect.push_back(i);
>
> cout<< myVect.size() << "\n";
> for( iter = myVect.begin(); iter != myVect.end(); iter++)
> cout<< *iter << " ";
>
> cout<<"\n";
>
> for( iter = myVect.begin(); iter != myVect.end(); iter++)
> {
> cout<< *iter << "\n";
> if( *iter == 1)
> myVect.erase(iter);
>
> }
>
> return 0;
> }


Calling erase invalidates any iterators pointing at or after the erased
element. The canonical way to do this is, rather than ++iter in the loop:

iter = myVect.erase(iter);

Read footnote 5 here: http://www.sgi.com/tech/stl/Vector.html#5
Read about erase here: http://www.sgi.com/tech/stl/Sequence.html

-Mark
  Réponse avec citation
Vieux 05/12/2007, 23h16   #3
Howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Vector erase dumps core when vector size is 2


"Anil" <anil.pundoor@gmail.com> wrote in message
news:ca663000-10df-4d97-8c40-4f203820eea9@w40g2000hsb.googlegroups.com...
>I am facing problem while erasing an elemet from stl vector when its
> size is 2. It works fine when SIZE > 2.
> Can anybody me in this?? Following is the sample code which i
> tried.
>
> #include <iostream>
> #include <vector>
>
> using namespace std;
> #define SIZE 2
> main()
> {
> vector<int> myVect;
> std::vector<int>::iterator iter;
>
> for(int i =0; i<SIZE; i++)
> myVect.push_back(i);
>
> cout<< myVect.size() << "\n";
> for( iter = myVect.begin(); iter != myVect.end(); iter++)
> cout<< *iter << " ";
>
> cout<<"\n";
>
> for( iter = myVect.begin(); iter != myVect.end(); iter++)
> {
> cout<< *iter << "\n";
> if( *iter == 1)
> myVect.erase(iter);
>
> }
>
> return 0;
> }


I think that calling erase above causes iter to become invalidated, so it's
illegal to call iter++ on it afterwards, regardless of whether it seems to
work on some vectors. The erase functions returns an iterator to the next
item, so you can set iter to that when calling erase, and only incrementing
when not calling erase. I believe there's also a std algorithm for this
(remove_if or something like that, I forget).

-Howard


  Réponse avec citation
Vieux 17/12/2007, 13h28   #4
Joel Yliluoma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Vector erase dumps core when vector size is 2

On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
> main()
> {


Is there some popular course that mistakenly teaches
declaring the main() function like that? Such line
seems common in questions lately in this group.

The proper minimal way to declare main(), is of course:

int main()

--
Joel Yliluoma - http://iki.fi/bisqwit/
  Réponse avec citation
Vieux 17/12/2007, 23h50   #5
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Vector erase dumps core when vector size is 2

Joel Yliluoma wrote:
> On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
>> main()
>> {

>
> Is there some popular course that mistakenly teaches
> declaring the main() function like that? Such line
> seems common in questions lately in this group.
>


Welcome to India.
  Réponse avec citation
Vieux 18/12/2007, 17h47   #6
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Vector erase dumps core when vector size is 2

Anil wrote:
> I am facing problem while erasing an elemet from stl vector when its
> size is 2. It works fine when SIZE > 2.
> Can anybody me in this?? Following is the sample code which i
> tried.
>
> #include <iostream>
> #include <vector>
>
> using namespace std;
> #define SIZE 2
> main()
> {
> vector<int> myVect;
> std::vector<int>::iterator iter;
>
> for(int i =0; i<SIZE; i++)
> myVect.push_back(i);
>
> cout<< myVect.size() << "\n";
> for( iter = myVect.begin(); iter != myVect.end(); iter++)
> cout<< *iter << " ";
>
> cout<<"\n";
>

for( iter = myVect.begin(); iter != myVect.end(); )
> {
> cout<< *iter << "\n";
> if( *iter == 1)

iter = myVect.erase(iter);
else
++iter;
>
> }
>
> return 0;
> }




--
Jim Langston
tazmaster@rocketmail.com


  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 12h21.


É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,74519 seconds with 14 queries