Connecting Tech Pros Worldwide Help | Site Map

a problem of iterator

  #1  
Old July 4th, 2009, 02:45 PM
Newbie
 
Join Date: Jul 2009
Posts: 16
string str="this IS A example";
for(string::iterator iter=str.begin();
iter!=str.end();++iter)
{
if(isupper(*iter))
{str.erase(iter);
--iter;}
}

cout<<str<<endl;
after "str.erase(iter);" i think iter will be an ivalidated iterator .But the code is correct.Why?Thank you~
  #2  
Old July 4th, 2009, 04:12 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: a problem of iterator


Quote:
Originally Posted by evenstar View Post
string str="this IS A example";
for(string::iterator iter=str.begin();
iter!=str.end();++iter)
{
if(isupper(*iter))
{str.erase(iter);
--iter;}
}

cout<<str<<endl;
after "str.erase(iter);" i think iter will be an ivalidated iterator .But the code is correct.Why?Thank you~
On your implementation it seems to run but on other implementations it can make daemons fly out of your nose. Better make your loop do this:

Expand|Select|Wrap|Line Numbers
  1. for(string::iterator iter=str.begin(); iter!=str.end(); ) 
  2.    if (isupper(*iter))
  3.       iter= str.erase(iter);
  4.    else
  5.       iter++;
  6.  
kind regards,

Jos
  #3  
Old July 5th, 2009, 12:33 AM
Newbie
 
Join Date: Jul 2009
Posts: 16

re: a problem of iterator


I know use iterator like "iter= str.erase(iter);"is a good custom.But i puzzle that use a vector<int>::iterator like
Quote:
vec.erase(iter); //vec is a object of vector
iter--;
will lead a RUNTIME ERROR. Why this kind of error would not happen to object of string .(in Visual Studio 2008)
  #4  
Old July 5th, 2009, 08:48 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: a problem of iterator


Quote:
Originally Posted by evenstar View Post
I know use iterator like "iter= str.erase(iter);"is a good custom.But i puzzle that use a vector<int>::iterator like

will lead a RUNTIME ERROR. Why this kind of error would not happen to object of string .(in Visual Studio 2008)
That can also happen with the string class, it depends on its implementation; suppose the string class reallocates its buffer after it had become less than half full; the buffer probably ends up somewhere else in memory leaving any pointers to the old buffer (kept by the iterator) invalid.

Simply assume that all iterators become invalid after an erase() operation and use the iterator returned by that erase() operation instead.

kind regards,

Jos
  #5  
Old July 5th, 2009, 10:53 AM
Newbie
 
Join Date: Jul 2009
Posts: 16

re: a problem of iterator


I see,and appreciate your help very much.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementation of iterator in Composite Pattern mathieu answers 1 January 18th, 2008 07:45 PM
operator * of iterator George2 answers 5 December 4th, 2007 01:35 PM
Implicit conversion of iterator to reverse_iterator bb answers 3 April 30th, 2007 07:35 PM
A problem of T * const & miaohua1982@gmail.com answers 9 November 30th, 2006 12:55 PM