C++中string.find()函数,string.find_first_of函数与string::npos
2017-09-05 14:34
495 查看
查找字符串a是否包含子串b,
不是用strA.find(strB) > 0而是strA.find(strB) != string:npos
string::size_type pos = strA.find(strB);
if(pos != string::npos){}
-------------------------------------------
int idx = str.find("abc");
if (idx == string::npos)
...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。
npos 是这样定义的:
static const size_type npos = -1;
因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:
if (str.find("abc") == string::npos) { ... }
错误:if(str.find("abc") )
注:找不到abc会返回-1,不为0为True。0为False
说明:
1. 如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 这四个函数,查找s中含有sub中任意字母的索引。
2. 如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。
特别注意:
find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;
不是用strA.find(strB) > 0而是strA.find(strB) != string:npos
string::size_type pos = strA.find(strB);
if(pos != string::npos){}
-------------------------------------------
int idx = str.find("abc");
if (idx == string::npos)
...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。
npos 是这样定义的:
static const size_type npos = -1;
因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:
if (str.find("abc") == string::npos) { ... }
错误:if(str.find("abc") )
注:找不到abc会返回-1,不为0为True。0为False
说明:
1. 如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 这四个函数,查找s中含有sub中任意字母的索引。
2. 如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。
特别注意:
find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;
相关文章推荐
- string 中find_first_of()函数用法例题
- 20170604_string中的函数find_first_of()和 find_last_of()
- std::string 类中find_first_not_of ()函数
- string::npos在 c++ 的find_first_of判断是什么意思
- string::npos在 c++ 的find_first_of判断是什么意思
- string 类成员函数 find() / find_first_of() / find_last_of() 的用法详解
- 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"
- std :: string :: find_first_not_of
- 函数find_first_of()和 find_last_of()
- C++中string类的成员函数find_first_not_of()
- string类中find(),find_first_of()和find_first_not_of()函数
- C++中string.find()函数与string::npos
- C++ 11.2 accumulate、find_first_of 函数
- 字符串的查找删除---C++中string.find()函数与string::npos
- C++中string.find()函数与string::npos
- string的find和find_first_of的区别
- string类型中find和find_first_of
- C++填坑之std::string::find_first_of
- CString中find_first_of和find_last_of()函数
- C++STL库中string类的find()和findfirstof()函数比较