1、C++ 中 string 类的 find 方法列表
size_type std::basic_string::find(const basic_string &__str, size_type __pos);
size_type std::basic_string::find(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find(const _CharT *__s, size_type __pos);
size_type std::basic_string::find(_CharT __c, size_type __pos);
size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::rfind(const _CharT *__s, size_type __pos);
size_type std::basic_string::rfind(_CharT __c, size_type __pos);
size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_last_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_first_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_first_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_first_not_of(_CharT __c, size_type __pos);
size_type std::basic_string::find_last_not_of(const basic_string &__str, size_type __pos);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos, size_type __n);
size_type std::basic_string::find_last_not_of(const _CharT *__s, size_type __pos);
size_type std::basic_string::find_last_not_of(_CharT __c, size_type __pos);
本文以代码和运行实例进行解析,实例中的颜色部分的规则如下:
黄色底色:在原字符串中经过了搜索,没有黄色底色的表示不在我们的搜索范围内
红色字体:搜索错误的地方
绿色字体:搜索正确
下面逐个解析
find()、
rfind()、
find_first_of()、
find_last_of()、
find_first_not_of()、
find_last_not_of()
2、find()
find函数在C++的头文件basic_string.h中有四种定义,定义如下:
2.1)第一个定义:size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
/** * @brief Find position of a C substring. * @param __s C string to locate. * @param __pos Index of character to search from. * @param __n Number of characters from @a s to search for. * @return Index of start of first occurrence. * * Starting from @a __pos, searches forward for the first @a * __n characters in @a __s within this string. If found, * returns the index where it begins. If not found, returns * npos. */ size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
实例代码:
1 void xx_find_3()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 const char * pStr = NULL;
6 int pos = 0;
7
8 pStr= "cdefppp";
9 pos = strSrc.find(pStr, 0, 4);
10 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
11
12 pStr= "cdefppp";
13 pos = strSrc.find(pStr, 0, 5);
14 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
15
16 pStr= "cdefppp";
17 pos = strSrc.find(pStr, 5, 5);
18 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
19 }
运行结果:
xx_find_3():
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.2)第二个定义:size_type find(const basic_string& __str, size_type __pos = 0) const
实例代码:
void xx_find_1()
{
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
string strDst;
size_t pos = 0;
strDst= "abcdef";
pos = strSrc.find(strDst);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.find(strDst, 0);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
strDst = "cdef";
pos = strSrc.find(strDst, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.find(strDst, 3);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
}
运行结果:
xx_find_1():
pos = 0 [find] abcdefghijklmnopqrstuvwxyz
pos = 0 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.3)第三个定义:size_type find(const _CharT* __s, size_type __pos = 0) const
/** * @brief Find position of a C string. * @param __s C string to locate. * @param __pos Index of character to search from (default 0). * @return Index of start of first occurrence. * * Starting from @a __pos, searches forward for the value of @a * __s within this string. If found, returns the index where * it begins. If not found, returns npos. */ size_type find(const _CharT* __s, size_type __pos = 0) const { __glibcxx_requires_string(__s); return this->find(__s, __pos, traits_type::length(__s)); }
示例代码:
1 void xx_find_2()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 const char * pStr = NULL;
6 size_t pos = 0;
7
8 pStr = "cdef";
9 pos = strSrc.find(pStr);
10 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
11
12 pos = strSrc.find(pStr, 0);
13 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
14
15 pos = strSrc.find(pStr, 2);
16 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
17
18 pos = strSrc.find(pStr, 3);
19 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
20 }
运行结果:
xx_find_2():
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = 2 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
2.4)第四个定义:size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
示例代码:
1 void xx_find_4()
2 {
3 printf("%s():\n", __func__);
4 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
5 int pos = 0;
6
7 char c = 'b';
8 pos = strSrc.find(c);
9 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
10
11 pos = strSrc.find(c, 0);
12 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
13
14 pos = strSrc.find(c, 1);
15 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
16
17 pos = strSrc.find(c, 2);
18 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
19 }
运行结果:
xx_find_4():
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = 1 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3、rfind()
rfind()函数在basic_string.h中同样有四种定义,定义如下:
3.1)第一个定义:size_type rfind(const basic_string& __str, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
实例代码:
1 void xx_rfind_1()
2 {
3 //size_type std::basic_string::rfind(const basic_string &__str, size_type __pos);
4 printf("%s():\n", __func__);
5 const string strSrc = "abcdefghijklmnopqrstuvwxyz";
6 string strDst;
7 size_t pos = 0;
8
9 strDst= "uvw";
10 pos = strSrc.rfind(strDst);
11 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
12
13 pos = strSrc.rfind(strDst, 25);
14 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
15
16 pos = strSrc.rfind(strDst, 20);
17 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
18
19 pos = strSrc.rfind(strDst, 19);
20 printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
21 }
运行结果:
xx_rfind_1():
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.2)第二个定义:size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
示例代码:
void xx_rfind_2()
{
//size_type std::basic_string::rfind(const _CharT *__s, size_type __pos, size_type __n);
printf("%s():\n", __func__);
const string strSrc = "abcdefghijklmnopqrstuvwxyz";
const char * p = NULL;
size_t pos = 0;
p = "uvw";
pos = strSrc.rfind(p, 25, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.rfind(p, 20, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
pos = strSrc.rfind(p, 19, 2);
printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find");
}
运行结果:
xx_rfind_2():
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = 20 [find] abcdefghijklmnopqrstuvwxyz
pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.3)第三个定义:size_type rfind(const _CharT* __s, size_type __pos = npos) const
示例代码:
void xx_rfind_3() { //size_type std::basic_string::rfind(const _CharT *__s, size_type __pos); printf("%s():\n", __func__); const string strSrc = "abcdefghijklmnopqrstuvwxyz"; const char * p = NULL; size_t pos = 0; p = "uvw"; pos = strSrc.rfind(p); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 25); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 20); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 19); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); }
运行结果:
xx_rfind_3(): pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
3.4)第四个定义: size_type rfind(_CharT __c, size_type __pos = npos) const
示例代码:
void xx_rfind_4() { //size_type std::basic_string::rfind(_CharT __c, size_type __pos); printf("%s():\n", __func__); const string strSrc = "abcdefghijklmnopqrstuvwxyz"; size_t pos = 0; char p = 'u'; pos = strSrc.rfind(p); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 25); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 20); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); pos = strSrc.rfind(p, 19); printf("pos = %2d\t[%s]\n", pos, -1 == pos ? "not find" : "find"); }
运行结果:
xx_rfind_4(): pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = 20 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
4、find_first_of()
find_first_of()函数也有4个定义,函数作用是在当前字符串中查找给定参数中的任意字符的第一次出现位置,有点儿拗口,下面用实例说明。
4.1)第一个定义 size_type find_first_of(const basic_string& __str, size_type __pos = 0)
示例代码
1 void xx_find_first_of_1() 2 { 3 //size_type std::basic_string::find_first_of(const basic_string &__str, size_type __pos); 4 printf("%s():\n", __func__); 5 const string strSrc = "abcdefghijklmnopqrstuvwxyz"; 6 string strDst; 7 size_t pos = 0; 8 9 strDst= "cde"; 10 pos = strSrc.find_first_of(strDst);//default value of pos is 0 11 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 12 13 pos = strSrc.find_first_of(strDst, 2); 14 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 15 16 pos = strSrc.find_first_of(strDst, 3); 17 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 18 19 pos = strSrc.find_first_of(strDst, 4); 20 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 21 22 pos = strSrc.find_first_of(strDst, 5); 23 printf("pos = %2d\t[%s]\n", pos, string::npos == pos ? "not find" : "find"); 24 }
运行结果
xx_find_first_of_1(): find [cde] pos = 2 [find] abcdefghijklmnopqrstuvwxyz pos = 2 [find] abcdefghijklmnopqrstuvwxyz pos = 3 [find] abcdefghijklmnopqrstuvwxyz pos = 4 [find] abcdefghijklmnopqrstuvwxyz pos = -1 [not find] abcdefghijklmnopqrstuvwxyz
本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4533328.html,如需转载请自行联系原作者