string类的find()函数总结

string类的头文件提供了很多搜索相关的函数比如find()函数及其变体。这使得我们可以以多种不同的方式在字符串中搜索给定的子字符串或字符。但是对于初学者来讲,经常被这些长相类似的函数所混淆。
下面总结了string类的find相关函数:

1、find():

find函数有四种变体:

方法原型描述
size_type find(const string & str, size_type pos = 0) const从字符串的pos位置开始,查找子字符串str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0) const从字符串的pos位置开始,查找子字符串s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0, size_type n) const从字符串的pos位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char ch, size_type pos = 0) const从字符串的pos位置开始,查找字符ch。如果找到,则返回该子字符串首次出现的位置;否则,返回string::npos

P.S.string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值。

string库还提供了相关的方法:rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()。他们的重载函数特征标都与find()方法相同。

2、rfind():

原型:

size_type rfind(const string & str, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos, size_type n) const;
size_type rfind(const char ch, size_type pos = npos) const;

rfind()方法查找子字符串或字符最后一次出现的位置。

3、find_first_of():

原型:

size_type find_first_of(const string & str, size_type pos = 0) const;
size_type find_first_of(const char * s, size_type pos, size_type n) const;
size_type find_first_of(const char * s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;

find_first_of()方法在字符串中查找参数中任何一个字符首次出现的位置。例如,下面的语句返回r在”cobra”中的位置(即索引3),因为这个”hark”中各个字母在”cobra”首次出现的位置:

string snake1 = "cobra";
int where = snake1.find_first_of("hark");

4、find_last_of():

原型:

size_type find_last_of(const string & str, size_type pos = npos) const;
size_type find_last_of(const char * s, size_type pos, size_type n) const;
size_type find_last_of(const char * s, size_type pos = npos) const;
size_type find_last_of(char c, size_type pos = npos) const;

find_last_of()方法在字符串中查找参数中任何一个字符最后一次出现的位置。

5、find_first_not_of():

原型:

size_type find_first_not_of(const string & str, size_type pos = 0) const;
size_type find_first_not_of(const char * s, size_type pos, size_type n) const;
size_type find_first_not_of(const char * s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;

find_first_not_of()方法在字符串中查找第一个不包含在参数中的字符,因此下面的语句返回c在”cobra”中的位置,因为”hark”中没有c:

string snake1 = "cobra";
int where = snake1.find_first_not_of("hark");

6、find_last_not_of():

原型:

size_type find_last_not_of(const string & str, size_type pos = npos) const;
size_type find_last_not_of(const char * s, size_type pos, size_type n) const;
size_type find_last_not_of(const char * s, size_type pos = npos) const;
size_type find_last_not_of(char c, size_type pos = npos) const;

find_last_not_of()方法在字符串中查找最后一个不包含在参数中的字符。

  • 50
    点赞
  • 205
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: C++中的string是标准库中提供的一个非常常用的,用来操作字符串。其中的find函数是用来在字符串中查找子字符串的方法。在C++中,string的find函数有多个重载版本,可以根据不同的参数型和个数进行调用。 基本的find函数签名为:size_t find(const string& str, size_t pos = 0) const。 其中,str是要查找的子字符串,pos表示开始查找的位置,默认为0。该函数的返回型为size_t,即一个无符号整数型,表示子字符串在字符串中的下标,如果找不到则返回string::npos,即-1。 下面是一个例子,演示了如何使用find函数: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); if (pos != string::npos) { cout << "子字符串\"" << subStr << "\"在位置" << pos << "找到了" << endl; } else { cout << "子字符串\"" << subStr << "\"未找到" << endl; } return 0; } ``` 在以上例子中,我们定义了一个字符串str和一个子字符串subStr,然后使用find函数在str中查找subStr。如果找到了,会打印子字符串的位置;如果未找到,则会打印未找到的提示。 需要注意的是,find函数是区分大小写的,如果要实现不区分大小写的查找,可以使用其他的函数或者自行实现。此外,find函数还有其他的重载版本,可以指定查找的方向、查找的次数等参数。不同的参数可以满足不同的需求。 ### 回答2: C++中的string是一个非常常用的字符串,它提供了许多用于处理字符串的函数,其中包括find函数。find函数的作用是在字符串中查找指定的子字符串,并返回其第一次出现的位置。 find函数有多个重载形式,最常用的形式是以下两种: 1. find(const string& str, size_t pos = 0):在字符串中从指定的位置pos开始查找子字符串str,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 2. find(const char* s, size_t pos = 0):在字符串中从指定的位置pos开始查找C风格字符串s,并返回其第一次出现的位置。如果找不到该子字符串,则返回string::npos。 下面是一个简单的示例代码,演示了find函数的使用: ``` #include <iostream> #include <string> using namespace std; int main() { string str = "Hello, world!"; string subStr = "world"; size_t pos = str.find(subStr); // 查找子字符串的位置 if (pos != string::npos) { cout << "子字符串在位置 " << pos << " 处找到了。" << endl; } else { cout << "子字符串未找到。" << endl; } return 0; } ``` 运行结果为: ``` 子字符串在位置 7 处找到了。 ``` 通过查找子字符串的位置,我们可以知道子字符串是否在原字符串中以及其位置。这对于字符串的处理非常有用,例如用于查找关键字、替换子字符串等操作。 ### 回答3: C++标准库中的string提供了一个名为find的成员函数,用于在字符串中查找子字符串。它的使用方法如下: string findstr = "example"; string str = "This is an example string."; int pos = str.find(findstr); if(pos != string::npos){ cout << "子字符串在原字符串中的位置是:" << pos << endl; } else{ cout << "子字符串未在原字符串中找到。" << endl; } 以上例子中,我们首先定义了一个待查找的子字符串findstr和一个原字符串str。然后通过调用str的find函数,传入待查找的子字符串findstr作为参数。find函数返回查找到的子字符串在原字符串中的位置(的首字符索引)。如果查找成功,则返回该位置值;如果查找失败,则返回一个特定的常数string::npos。因此,我们可以通过与string::npos进行比较,判断是否找到了子字符串。 需要注意的是,find函数还可以传入另外两个可选的参数,分别是起始搜索位置和要搜索的字符数量。例如: int pos = str.find(findstr, 5, 10); 表示从原字符串的第5个字符开始,最多搜索10个字符的范围内查找子字符串findstr。 总结起来,C++ string的find函数可以用于在字符串中查找子字符串,它返回子字符串在原字符串中的位置或者一个特定的常数string::npos表示查找失败。此外,还可以通过传入可选的参数来指定起始搜索位置和搜索字符数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值