字符串查找 [英] string find

查看:91
本文介绍了字符串查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我想知道

以下小程序可能会(或需要)的行为。

我希望find(a,string :: npos)将返回string :: npos

,但似乎取决于搜索的字符串。


在我的系统上,使用gcc 4.1.2,我得到:


pos1 = 2

问题因为pos1 = 0

我期待这个

#include< iostream>

#include< string>


使用命名空间std;


int main(){

string use =" anu" ;;

string :: size_type pos1 = use.find_first_not_of(" ;一个);

cerr<< " POS1 = QUOT; << pos1<< endl;

pos1 = use.find(" a",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

pos1 = use.find(" q",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

}

解决方案



" Ko van der Sloot" < Ko ************ @ uvt.nlwrote in message

news:b3 ****************** ********@news1.tudelft.nl ...


你好

我想知道可能出现的行为(或者是必需的)

以下小程序。

我希望find(a,string :: npos)会返回string :: npos

但似乎取决于搜索的字符串。


在我的系统上,使用gcc 4.1.2,我得到:


pos1 = 2

问题因为pos1 = 0

我预计这个


#include< iostream>

#include< string>


使用命名空间std;


int main(){

string use =" anu" ;;

string :: size_type pos1 = use.find_first_not_of(" an");

cerr< < " POS1 = QUOT; << pos1<< endl;

pos1 = use.find(" a",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

pos1 = use.find(" q",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

}



为什么你把''npos''作为''find()''的第二个参数?

你知道第二个参数的含义吗?

你知道'npos'是什么意思吗?


- Mike


3月29日17:12,Mike Wahler < mkwah ... @ mkwahler.netwrote:


" Ko van der Sloot" < Ko.vanderSl ... @ uvt.nlwrote in messagenews:b3 ************************** @ news1.tud elft.nl. ..


你好

我想知道

的预期(或是必需的)行为以下小程序。

我希望find(a,string :: npos)会返回string :: npos

,但似乎取决于哪个搜索字符串。


在我的系统上,使用gcc 4.1.2,我得到:


pos1 = 2

问题因为pos1 = 0

我预计这个


#include< iostream>

#include< string>


using namespace std;


int main(){

string use =" anu" ;;

string :: size_type pos1 = use.find_first_not_of(" an");

cerr<< " POS1 = QUOT; << pos1<< endl;

pos1 = use.find(" a",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

pos1 = use.find(" q",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

}



为什么你把''npos''作为''find()''的第二个参数?

你知道第二个参数的含义吗?

你知道'npos'是什么意思吗?



在实践中想要做的事情可能很奇怪,但我会感兴趣

一个明确的答案。我的第一个想法是,可能使用npos作为

找到的第二个参数有未定义的行为,但我找不到

任何东西来支持它(不是'''这意味着它当然不存在。当我使用Visual Studio 2005编译并运行代码时,我得到了输出

我相信OP的期望:


pos1 = 2

我预计这个

我预计这个


所以我想知道这个行为是不确定的,我是''我只是错过了

在哪里说,或者OP发现了编译器错误?


Gavin Deane


" Gavin Deane" < de ********* @ hotmail.comwrote in

新闻:11 ********************* @ o5g2000hsb.googlegrou ps.com:


3月29日17:12,Mike Wahler < mkwah ... @ mkwahler.netwrote:


>" Ko van der Sloot" < Ko.vanderSl ... @ uvt.nlwrote在
messagenews:b3 ************************** @ news1.tu delft.nl ...


你好

我想知道哪些行为可能是预期的(或者是必需的)

用于以下小程序。

我希望find(a,string :: npos)将返回

string :: npos但似乎是取决于搜索哪个字符串



在我的系统上,使用gcc 4.1.2,我得到:


pos1 = 2

问题因为pos1 = 0

我预计这个


#include< iostream>

#include< string>


using namespace std;


int main(){

string use =" anu" ;;

string :: size_type pos1 = use.find_first_not_of(" an");

cerr<< " POS1 = QUOT; << pos1<< endl;

pos1 = use.find(" a",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

pos1 = use.find(" q",string :: npos);

if(pos1 == string :: npos)

cerr<< 我期待这个 << endl;

else

cerr<< 问题因为pos1 = << pos1<< endl;

}


为什么你把''npos''作为''find()''的第二个参数?
你知道第二个论点的含义吗?
你知道'npos'是什么意思吗?



在实践中想要做的事情可能很奇怪,但我会感兴趣

一个确定的答案。我的第一个想法是,可能使用npos作为

找到的第二个参数有未定义的行为,但我找不到

任何东西来支持它(不是'''这意味着它当然不存在。当我使用Visual Studio 2005编译并运行代码时,我得到了输出

我相信OP的期望:


pos1 = 2

我预计这个

我预计这个


所以我想知道这个行为是不确定的,我是''我刚刚错过

它说的那个,或者OP发现了编译器错误?



AFAIK,查找的第二个参数是字符串的索引。

string :: npos不是字符串的有效索引,所以我希望

与指定一个越界索引相同的影响。


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;
}

解决方案


"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


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


"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.


这篇关于字符串查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆