C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vector erase

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Daniel Schüle
Guest





PostPosted: Tue Apr 26, 2005 9:26 pm    Post subject: vector erase Reply with quote



Hallo NG

#include nötige header

int main()
{
int * pi = new int[1024];
vector<int> v(pi, pi+1024);
/*
int cnt = 0;
while(v.empty() == false)
v.erase(v.begin()), cnt++;
cout << cnt << endl; // 1024
*/

vector for(; i != n; ++i)
v.erase(i);

delete [] pi;
return 0;
}

das Program bricht mit SEGFAULT ab
ersetzt man die for Schleife mit dem auskommentierten Kode,
so macht die while Schleife keine Probleme

erase nimmt in beiden Fällen einen iterator

Woran scheitert das Löschen mit der for Schleife?

MfG

Daniel

ps. Ich weiss es gibt clear, es geht nur um erase Methode
Compileraufruf mit
g++ -o erase erase.cpp -Wall -O2 -fomit-frame-pointer -fno-exceptions

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Falk Tannhäuser
Guest





PostPosted: Tue Apr 26, 2005 10:21 pm    Post subject: Re: vector erase Reply with quote



Daniel Schüle wrote:
[...]
Quote:
vector<int>::iterator i = v.begin(), n = v.end();
for(; i != n; ++i)
v.erase(i);

Bereits der erste erase()-Aufruf macht nach § 23.2.4.3/3 alle Iteratoren
nach dem gelöschten Element ungültig, insbesondere also n. Daher "Nicht
Definiertes Verhalten".

MfG
Falk

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Daniel Schüle
Guest





PostPosted: Tue Apr 26, 2005 10:37 pm    Post subject: Re: vector erase Reply with quote



Quote:
Bereits der erste erase()-Aufruf macht nach § 23.2.4.3/3 alle Iteratoren
nach dem gelöschten Element ungültig, insbesondere also n. Daher "Nicht
Definiertes Verhalten".

vector<int>::iterator i = v.begin(), n = v.end();
while( i != n )
v.erase(--n);

scheint somit Standardkonform zu sein

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Thomas Maeder
Guest





PostPosted: Wed Apr 27, 2005 6:10 am    Post subject: Re: vector erase Reply with quote

Daniel Schüle <uval (AT) rz (DOT) uni-karlsruhe.de> writes:

Quote:
Bereits der erste erase()-Aufruf macht nach § 23.2.4.3/3 alle
Iteratoren nach dem gelöschten Element ungültig, insbesondere also
n. Daher "Nicht Definiertes Verhalten".

vector<int>::iterator i = v.begin(), n = v.end();
while( i != n )
v.erase(--n);

Ja. Oder

v.erase(v.begin(),v.end());

oder

v.clear();

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Torsten Robitzki
Guest





PostPosted: Mon May 02, 2005 7:09 am    Post subject: Re: vector erase Reply with quote

Quote:
scheint somit Standardkonform zu sein


Nein, da n bzw. end() hinter n-1 liegt.

Was natürlich Blödsinn ist Sad War wohl etwas früh letzte Woche.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.