472,958 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 4164
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David Rubin | last post by:
I looked on google for an answer, but I didn't find anything short of using boost which sufficiently answers my question: what is a good way of doing string tokenization (note: I cannot use boost)....
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Christopher Benson-Manica | last post by:
The function in question follows: vector<string>& tokenize( const string& s, vector<string>& v, char delimiter=',' ) { int delim_idx, begin_idx=0, len=s.length(); for(...
6
by: ma740988 | last post by:
Oh what fun it is to get acclimated with the various containers and algorithms.. I'm trying to determine if I could use find/find_first_of algorithms to achieve the same object of a for loop when...
1
by: sparks | last post by:
I was trying to find occurances of duplicate entries in a string something like this "1 + (2++34)-4*6/6*89" So at first I tried this String* str = S"1 + (2++34)-4*6/6*89"; int start; int at;...
1
by: Allerdyce.John | last post by:
I have my source string like this: As i step thru the debugger, I can't understand why namePosStart is 2 namePosEnd is 15 I expect: namePosStart is 9 namePosStart is 11
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
12
by: Simon | last post by:
Well, the title's pretty descriptive; how would I be able to take a line of input like this: getline(cin,mostrecentline); And split into an (flexible) array of strings. For example: "do this...
8
by: rushik | last post by:
Hello, I am writing one application in which I am getting data as a string " 10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it by pipe '|' and get integer outputs in some...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.