STL string find_first_of ( infinite loop )

S

scottys0

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::eek:currencesOf( 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
 
H

Heinz Ozwirk

scottys0 said:
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::eek:currencesOf( 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
 
S

scottys0

Thanks Ozwirk,

but I've need added the string length;

_offset = this->find_first_of( _lcValue.c_str(), _offset +
_lcValue.length()+1);

B&R
Meyer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top