473,654 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string find

Hello
I was wondering which behaviour might be expected (or is required) for
the following small program.
I would expect that find( "a", string::npos ) would return string::npos
but is seems to be dependant on which string is searched for.

On my system, using gcc 4.1.2, I get:

pos1=2
problem because pos1=0
I expected this
#include <iostream>
#include <string>

using namespace std;

int main(){
string use = "anu";
string::size_ty pe pos1 = use.find_first_ not_of( "an" );
cerr << "pos1=" << pos1 << endl;
pos1 = use.find( "a", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
pos1 = use.find( "q", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
}
Mar 29 '07 #1
11 3696

"Ko van der Sloot" <Ko************ @uvt.nlwrote in message
news:b3******** *************** ***@news1.tudel ft.nl...
Hello
I was wondering which behaviour might be expected (or is required) for
the following small program.
I would expect that find( "a", string::npos ) would return string::npos
but is seems to be dependant on which string is searched for.

On my system, using gcc 4.1.2, I get:

pos1=2
problem because pos1=0
I expected this
#include <iostream>
#include <string>

using namespace std;

int main(){
string use = "anu";
string::size_ty pe pos1 = use.find_first_ not_of( "an" );
cerr << "pos1=" << pos1 << endl;
pos1 = use.find( "a", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
pos1 = use.find( "q", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
}
Why are you giving 'npos' as the second argument to 'find()'?
Do you know the meaning of the second argument?
Do you know what 'npos' is for?

-Mike
Mar 29 '07 #2
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Ko van der Sloot" <Ko.vanderSl... @uvt.nlwrote in messagenews:b3* *************** **********@news 1.tudelft.nl...
Hello
I was wondering which behaviour might be expected (or is required) for
the following small program.
I would expect that find( "a", string::npos ) would return string::npos
but is seems to be dependant on which string is searched for.
On my system, using gcc 4.1.2, I get:
pos1=2
problem because pos1=0
I expected this
#include <iostream>
#include <string>
using namespace std;
int main(){
string use = "anu";
string::size_ty pe pos1 = use.find_first_ not_of( "an" );
cerr << "pos1=" << pos1 << endl;
pos1 = use.find( "a", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
pos1 = use.find( "q", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
}

Why are you giving 'npos' as the second argument to 'find()'?
Do you know the meaning of the second argument?
Do you know what 'npos' is for?
Might be an odd thing to want to do in practice, but I'd be interested
in a definitive answer. My first thought was that maybe using npos as
the second argument to find has undefined behaviour, but I can't find
anything to back that up (doesn't mean it's not there of course). When
I compiled and ran the code using Visual Studio 2005, I got the output
I believe the OP ws expecting:

pos1=2
I expected this
I expected this

So I wonder whether the behaviour is undefined and I've just missed
where it says that, or has the OP found a compiler bug?

Gavin Deane

Mar 29 '07 #3
"Gavin Deane" <de*********@ho tmail.comwrote in
news:11******** *************@o 5g2000hsb.googl egroups.com:
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"Ko van der Sloot" <Ko.vanderSl... @uvt.nlwrote in
messagenews:b3 *************** ***********@new s1.tudelft.nl.. .
Hello
I was wondering which behaviour might be expected (or is required)
for the following small program.
I would expect that find( "a", string::npos ) would return
string::npos but is seems to be dependant on which string is
searched for.
On my system, using gcc 4.1.2, I get:
pos1=2
problem because pos1=0
I expected this
#include <iostream>
#include <string>
using namespace std;
int main(){
string use = "anu";
string::size_ty pe pos1 = use.find_first_ not_of( "an" );
cerr << "pos1=" << pos1 << endl;
pos1 = use.find( "a", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
pos1 = use.find( "q", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
}

Why are you giving 'npos' as the second argument to 'find()'?
Do you know the meaning of the second argument?
Do you know what 'npos' is for?

Might be an odd thing to want to do in practice, but I'd be interested
in a definitive answer. My first thought was that maybe using npos as
the second argument to find has undefined behaviour, but I can't find
anything to back that up (doesn't mean it's not there of course). When
I compiled and ran the code using Visual Studio 2005, I got the output
I believe the OP ws expecting:

pos1=2
I expected this
I expected this

So I wonder whether the behaviour is undefined and I've just missed
where it says that, or has the OP found a compiler bug?
AFAIK, the second argument to find is an index into the string.
string::npos is not a valid index into a string, so I would expect the
same impact as specifying an out-of-bounds index to find.
Mar 29 '07 #4
On 29 Mar, 19:16, Andre Kostur <nntps...@kostu r.netwrote:
AFAIK, the second argument to find is an index into the string.
string::npos is not a valid index into a string, so I would expect the
same impact as specifying an out-of-bounds index to find.- Hide quoted text
21.3.6.1 (in the 1998 standard) doesn't mention any precondition that
the second argument to std::string::fi nd must be a valid index.
Conditions that indicate the substring has been found are defined, and
it just says that if those conditons can not be met, the return is
npos. Based on that, I would conclude that the OP's compiler is wrong.

Gavin Deane

Mar 29 '07 #5

"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11******** **************@ p15g2000hsd.goo glegroups.com.. .
On 29 Mar, 19:16, Andre Kostur <nntps...@kostu r.netwrote:
>AFAIK, the second argument to find is an index into the string.
string::npos is not a valid index into a string, so I would expect the
same impact as specifying an out-of-bounds index to find.- Hide quoted
text

21.3.6.1 (in the 1998 standard) doesn't mention any precondition that
the second argument to std::string::fi nd must be a valid index.
I believe it's implied in that the argument type is 'size_type'.
(My implementation' s documentation describes 'size_type' as
"An unsigned integer type that can represent the number of elements
and indices in a string.")
Conditions that indicate the substring has been found are defined, and
it just says that if those conditons can not be met, the return is
npos.
Right. That's what npos is used for.
Based on that, I would conclude that the OP's compiler is wrong.
Undefined behavior is neither 'right' nor 'wrong', it's simpy
undefined. :-)

-Mike
Mar 29 '07 #6
Gavin Deane wrote:
<code skipped>
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>Why are you giving 'npos' as the second argument to 'find()'?
Do you know the meaning of the second argument?
Do you know what 'npos' is for?
Of course I do. It was a simplified excerpt from a program that ran havoc.
In real life, the second argument was an index that at some point
reached npos.
Might be an odd thing to want to do in practice, but I'd be interested
in a definitive answer. My first thought was that maybe using npos as
the second argument to find has undefined behaviour, but I can't find
anything to back that up (doesn't mean it's not there of course). When
I compiled and ran the code using Visual Studio 2005, I got the output
I believe the OP ws expecting:

pos1=2
I expected this
I expected this

So I wonder whether the behaviour is undefined and I've just missed
where it says that, or has the OP found a compiler bug?
That is the point: My question boiles down to:
is calling find with npos as second argument legal, is it undefined? or
what?
I would expect (hope) that it is legal, and should return npos.
"because that is only reasonable thing to do"
Gavin Deane
Thanx
Ko

Mar 30 '07 #7
On Mar 29, 8:16 pm, Andre Kostur <nntps...@kostu r.netwrote:
"Gavin Deane" <deane_ga...@ho tmail.comwrote innews:11****** *************** @o5g2000hsb.goo glegroups.com:
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Ko van der Sloot" <Ko.vanderSl... @uvt.nlwrote in
messagenews:b3* *************** **********@news 1.tudelft.nl...
I was wondering which behaviour might be expected (or is required)
for the following small program.
I would expect that find( "a", string::npos ) would return
string::npos but is seems to be dependant on which string is
searched for.
On my system, using gcc 4.1.2, I get:
pos1=2
problem because pos1=0
I expected this
#include <iostream>
#include <string>
using namespace std;
int main(){
string use = "anu";
string::size_ty pe pos1 = use.find_first_ not_of( "an" );
cerr << "pos1=" << pos1 << endl;
pos1 = use.find( "a", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
pos1 = use.find( "q", string::npos );
if ( pos1 == string::npos )
cerr << "I expected this" << endl;
else
cerr << "problem because pos1=" << pos1 << endl;
}
Why are you giving 'npos' as the second argument to 'find()'?
Do you know the meaning of the second argument?
Do you know what 'npos' is for?
Might be an odd thing to want to do in practice,
Not really. Presumably, in practice, the position is the
results of an earlier find. And it's rather convenient to not
have to check. Consider something like the following:

std::string
getInBraces(
std::string const& s )
{
size_t pos1 = s.find( '{' ) ;
size_t pos2 = s.find( '}', pos1 ) ;
return pos2 == std::string::np os
? std::string()
: s.substr( pos1 + 1, pos2 - pos1 - 1 ) ;
}

It looks reasonable to me, and having to check before the second
call to find would make the code (slightly) more complicated.
but I'd be interested
in a definitive answer. My first thought was that maybe using npos as
the second argument to find has undefined behaviour, but I can't find
anything to back that up (doesn't mean it's not there of course). When
I compiled and ran the code using Visual Studio 2005, I got the output
I believe the OP ws expecting:
pos1=2
I expected this
I expected this
So I wonder whether the behaviour is undefined and I've just missed
where it says that, or has the OP found a compiler bug?
AFAIK, the second argument to find is an index into the string.
string::npos is not a valid index into a string, so I would expect the
same impact as specifying an out-of-bounds index to find.
What makes you say that? The standard doesn't place any such
restriction on it. In fact, the standard actually specifies as
a condition for not returning npos the fact that pos is in
range, which seems pretty clear to me. The argument is a
constraint on the return value; i.e. the value returned *must*
be <= pos (or npos, if a qualifying value cannot be found).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Mar 30 '07 #8
On Mar 29, 9:41 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Gavin Deane" <deane_ga...@ho tmail.comwrote in message
news:11******** **************@ p15g2000hsd.goo glegroups.com.. .
On 29 Mar, 19:16, Andre Kostur <nntps...@kostu r.netwrote:
AFAIK, the second argument to find is an index into the string.
string::npos is not a valid index into a string, so I would expect the
same impact as specifying an out-of-bounds index to find.- Hide quoted
text
21.3.6.1 (in the 1998 standard) doesn't mention any precondition that
the second argument to std::string::fi nd must be a valid index.
I believe it's implied in that the argument type is 'size_type'.
Why? What's implied in size_type is that the argument must be
representable in a size_type. Since npos also has size_type, it
must be representable in a size_type.
(My implementation' s documentation describes 'size_type' as
"An unsigned integer type that can represent the number of elements
and indices in a string.")
Or npos, of course, since npos has the type size_type as well.
Other than that, the specification you quote says "can
represent", not is a valid index in a particular string.
Conditions that indicate the substring has been found are defined, and
it just says that if those conditons can not be met, the return is
npos.
Right. That's what npos is used for.
Amongst other things. It's also used to indicate "to the end",
e.g. as the second parameter in substr.
Based on that, I would conclude that the OP's compiler is wrong.
Undefined behavior is neither 'right' nor 'wrong', it's simpy
undefined. :-)
But in this case, the behavior is clearly defined. The standard
places no precondition on npos (which it does in other cases),
and defines a set of post-conditions for the function: if the
return value is r, then r == npos || (r >= pos && s[ r ] == c).
The caller has met the pre-conditions (so there is no undefined
behavior), and the function has not fulfilled the
post-conditions. Looks like an error to me.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Mar 30 '07 #9
* Ko van der Sloot:
>
is calling find with npos as second argument legal, is it undefined? or
what?
It's valid.

There are no restrictions on the second argument's value.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
33672
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find = string.find(file(os.path.join(root,fname), 'rb').read(), 'THIS') print find
3
2978
by: Chris Mantoulidis | last post by:
I posted this here one day ago but it seems like it hasn't been put up for some unknown reason. That gives me a chance to say things a bit better in this post. 1st of all let's desribe the problem: In ParamGenerate() I want to find all whitespaces before a certain point in one string. Thus I use the string::find() function. However it seems like this function ignores some whitespaces :/ This is really weird and I need your help. Okay...
3
2466
by: Old Wolf | last post by:
Hi all. G++ fails to compile the following: #include <string> int main() { std::string foo("abc=123"); std::string::const_iterator delimiter = std::find(foo.begin(), foo.end(), '=');
32
14836
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
5
3300
by: Gary Wessle | last post by:
hi or is there a better way to check the existence of "py" in a string "s"? string s = "djrfpyfd"; string t = "py"; string r = s.substr(s.find("py"),2); cout << (t==r) << endl;
5
471
by: Joe Nova | last post by:
I'm a C++ noob and I need a little help manipulating strings. I've got a program that takes an expression in the form: "operand1 operator operand2" I'd like to: 1. Find the total length of the string
5
5474
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h> /*------------------------------------------------------------------------------------------- | Function name : strrep
2
2483
by: kevin.eugene08 | last post by:
hi all, i'm trying to replace a string with known "tokens" with values -- and am not sure how to do this effectively. Since i don't know the size of the string containing the tokens to be replaced, i can't make a buffer -- hence i thought of using malloc() -- is what i have below effective? it doesn't seem to work though - the returned string just returns the substituted part. Any thoughts / comments are welcomed.
12
1915
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char array. For example: char teststr; strcpy(teststr, "test:blah;");
0
8290
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,...
1
8489
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,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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
5622
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();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
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

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.