468,247 Members | 1,323 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

STL string find_first_of ( infinite loop )

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

Mar 4 '06 #1
2 3943
"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
Mar 4 '06 #2
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

Mar 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by David Rubin | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by Christopher Benson-Manica | last post: by
6 posts views Thread by ma740988 | last post: by
1 post views Thread by sparks | last post: by
1 post views Thread by Allerdyce.John | last post: by
4 posts views Thread by dor | last post: by
12 posts views Thread by Simon | last post: by
8 posts views Thread by rushik | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.