"scottys0" <fm*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@z34g2000cwc.googlegro ups.com...
Hi everyboy,
I'm working in an IOC-IBM (IBM open class) replamecent library. the
code seens confused but , no, isn't.
#define INumber long
class IString : public std::string {
private:
... // some control variables
public
... // constructors & destructor
... // some methods
virtual INumber ocurrencesOF( IString );
}
INumber IString::ocurrencesOf( IString _lcValue ) {
std::string::size_type _offset = this->find_first_of(
_lcValue.c_str());
while ( _offset != std::string::npos ) {
_carry++;
_offset = this->find_first_of( _lcValue.c_str(), _offset);
std::cout << "Offset : " << _offset << std::endl;
}
return _carry;
}
I've anomalous behavior from std::string class when I'm using the
find_first_of.
my question: is this a correct implementation ?, 'cause this compiles,
but I got an infinite loop.
B&R
Meyer
The code does what you told it to do. It runs in an endless loop. You find
the first occurence of a character. Then you start looking for that
character at the place you have already found it. And you will find it just
where you start looking for it.
Assume _*this to hold "some string with spaces" and _lcValue to be equal to
" ". Now
_offset = this->find_first_of(_lcValue)
return sthe offset of the first space -- 4. Now
_offset = this->find_first_of(_lcValue, _offset)
starts searching at offset 4, the first space. Where will it find the first
space? At offset 4, of cause. Once you have found an occurence of one of the
characters you are looking for, you have to skip that character before you
can search again. Replace that line with
_offset = this->find_first_of(_lcValue, _offset + 1)
HTH
Heinz