Topic: STL and String Iterator


Author: doryzpost@yahoo.com.au (Dory)
Date: 6 Mar 2002 05:15:51 -0500
Raw View
Hi,
  When you run an algorithm such as remove_if you get back an output
iterator to the new logical end of your container.
On MS platform (I donno what would happen on other platforms) most
string iterators are just and unsigned int, the interator isn't.
Therefore after I run the remove_if on a string, I have an new end and
I want to run erase......but no can do.....
There is not a way to use this iterator because it's not a string
iterator....

Any thoughts would be welcome.

   Dory.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 07 Mar 02 18:26:03 GMT
Raw View
Dory wrote:
>
> Hi,
>   When you run an algorithm such as remove_if you get back an output
> iterator to the new logical end of your container.
> On MS platform (I donno what would happen on other platforms) most
> string iterators are just and unsigned int, the interator isn't.

"the iterator isn't" what?

A string iterator must meet the random access iterator requirements. An
unsigned integer can't meet those requirements. In particular, if 'u' is
an unsigned integer, u[n], where n is an iteger, is an illegal
expression, which are required to be legal for random access iterators.
remove_if() requires  ForwardIterator arguments. *u is an illegal
expression, which means it doesn't even meet the forward iterator
requirements.

> Therefore after I run the remove_if on a string, I have an new end and
> I want to run erase......but no can do.....
> There is not a way to use this iterator because it's not a string
> iterator....

If you want to use remove_if() on a string, you must pass it a string
iterator. If you do so, it will return an iterator of the same type. You
can then pass that iterator to the string's erase() function. There's no
problem here.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: werner.salomon@kratzer-automation.de (Werner Salomon)
Date: 07 Mar 02 18:26:50 GMT
Raw View
doryzpost@yahoo.com.au (Dory) wrote in message news:<4b712327.0203051436.7ee8b78d@posting.google.com>...
> Hi,
>   When you run an algorithm such as remove_if you get back an output
> iterator to the new logical end of your container.
> On MS platform (I donno what would happen on other platforms) most
> string iterators are just and unsigned int, ..

Hi Dory,
The string::iterator in the native STL of MS-Dev 6x is a char*.
Have a look in the source-Code (<string> includes <xstring>)
 typedef _A::pointer iterator; // _A is std::allocator< char>
template allocator
 typedef _Ty _FARQ *pointer; // _Ty is char

> .. the interator isn't.
> Therefore after I run the remove_if on a string, I have an new end and
> I want to run erase......but no can do.....
> There is not a way to use this iterator because it's not a string
> iterator....

I don't see Your problem - this code runs perfect
    string tmp = " useX stringX::iteratorX";
    string::iterator i = remove_if( tmp.begin(), tmp.end(),
        bind2nd( equal_to< char >(), 'X' ) );
    tmp.erase( i, tmp.end() );
    cout << " tmp= '" << tmp << "'  tmp.size()= "
        << tmp.size() << endl;

the output is:
 tmp= 'use string::iterator'  tmp.size()= 21
.. that's ok.

Greetings
Werner

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]