473,385 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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_type 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 3674

"Ko van der Sloot" <Ko************@uvt.nlwrote in message
news:b3**************************@news1.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_type 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...@mkwahler.netwrote:
"Ko van der Sloot" <Ko.vanderSl...@uvt.nlwrote in messagenews:b3**************************@news1.tud elft.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_type 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*********@hotmail.comwrote in
news:11*********************@o5g2000hsb.googlegrou ps.com:
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"Ko van der Sloot" <Ko.vanderSl...@uvt.nlwrote in
messagenews:b3**************************@news1.tu delft.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_type 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...@kostur.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::find 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*********@hotmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On 29 Mar, 19:16, Andre Kostur <nntps...@kostur.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::find 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...@mkwahler.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...@kostur.netwrote:
"Gavin Deane" <deane_ga...@hotmail.comwrote innews:11*********************@o5g2000hsb.googlegr oups.com:
On 29 Mar, 17:12, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Ko van der Sloot" <Ko.vanderSl...@uvt.nlwrote in
messagenews:b3**************************@news1.tud elft.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_type 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::npos
? 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 objektorientierter Datenverarbeitung
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...@mkwahler.netwrote:
"Gavin Deane" <deane_ga...@hotmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On 29 Mar, 19:16, Andre Kostur <nntps...@kostur.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::find 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 objektorientierter Datenverarbeitung
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
"James Kanze" <ja*********@gmail.comwrote in
news:11**********************@y66g2000hsf.googlegr oups.com:

[snip example of using string::npos as second argument to string::find()]
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).
That's one way of looking at it. My first introduction to the find method
talks about starting its find at position pos. And since pos is an index
into the string, string::npos is an invalid index. size_type(-1) is way
beyond the end of the string, so it's not even one-past-the-end to make it
iterator-ish. But I can see the argument by reading the standard directly,
and that by passing any index beyond the end of the string would result in
a simple string::npos response.
Mar 30 '07 #11
* Andre Kostur:
>
My first introduction to the find method
talks about starting its find at position pos. And since pos is an index
into the string, string::npos is an invalid index.
The standard doesn't use language like "starting" somewhere.

It merely states what the result should be.

There are no requirements on the second argument to 'find'.

--
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 #12

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

Similar topics

10
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 =...
3
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...
3
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
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...
5
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
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...
5
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>...
2
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...
12
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.