473,763 Members | 6,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::ocurre ncesOf( IString _lcValue ) {

std::string::si ze_type _offset = this->find_first_o f(
_lcValue.c_str( ));

while ( _offset != std::string::np os ) {
_carry++;
_offset = this->find_first_o f( _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 4211
"scottys0" <fm*******@gmai l.com> schrieb im Newsbeitrag
news:11******** *************@z 34g2000cwc.goog legroups.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::ocurre ncesOf( IString _lcValue ) {

std::string::si ze_type _offset = this->find_first_o f(
_lcValue.c_str( ));

while ( _offset != std::string::np os ) {
_carry++;
_offset = this->find_first_o f( _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_o f( _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
8081
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). For example, I have tried this: #include <algorithm> #include <cctype> #include <climits> #include <deque> #include <iostream>
43
5599
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
5
1797
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( delim_idx=s.find_first_of(delimiter,begin_idx) ;
6
14905
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 searching for the first element in a vector of pairs. So now: typedef std::pair<int, int > int_pair; typedef std::vector<int_pair> vec_int_pair; int main() { vec_int_pair p;
1
1367
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; int end; int count;
1
1589
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
2572
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
4457
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 action" would go to: item 0: do
8
1898
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 array or other data structure..... do anybody worked in the same functionality, then please let me know. Thanks,
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.