什么是npos:
std::basic_string<CharT,Traits,Allocator>::npos static const size_type npos = -1;
这是特殊值,等于 size_type
类型可表示的最大值。准确含义依赖于语境,但通常,期待 string 下标的函数以之为字符串尾指示器,返回 string 下标的函数以之为错误指示器。
注意
虽然定义使用 -1 ,由于有符号到无符号隐式转换,且 size_type 是无符号整数类型, npos
的值是其所能保有的最大正值。这是指定任何无符号类型的最大值的可移植方式。
1.find
size_type find( const basic_string& str, size_type pos = 0 ) const;
可见需要两个参数,一个是想要找到的string str子串,已经指明从那个位置pos开始找,返回类型为string::size_type
寻找首个等于给定字符序列的子串。搜索始于 pos
,即找到的子串必须不始于 pos
之前的位置。
当然,也可以用来寻找一个字符
正式而言,若下列全部为 true ,则说在位置 xpos
找到子串 str
:
- xpos >= pos
- xpos + str.size() <= size()
- 对于
str
中所有位置n
, Traits::eq(at(xpos+n), str.at(n))
特别是,这隐含
- 仅若 pos <= size() - str.size() 才能找到子串
- 在
pos
找到空子串,若且唯若 pos <= size() - 对于非空子串,若 pos >= size() ,则函数始终返回 npos 。
参数
str | - | 要搜索的 string |
pos | - | 开始搜索的位置 |
count | - | 要搜索的子串长度 |
s | - | 指向要搜索的字符串的指针 |
ch | - | 要搜索的字符 |
t | - | 要搜索的对象(可转换为 std::basic_string_view ) |
返回值
找到的子串的首字符位置,或若找不到这种子串则为 npos 。
例子:
#include <string> #include <iostream> void print(std::string::size_type n, std::string const &s){ if(n == std::string::npos){ std::cout << "not found "; }else{ std::cout << "found: " << s.substr(n) << ' '; } } int main(){ std::string::size_type n; std::string const s = "This is a string"; n = s.find("is"); print(n,s); n = s.find("is", 5); print(n,s); n = s.find('a'); print(n,s); n = s.find('q'); print(n,s); }
返回:
found: is is a string found: is a string found: a string not found
2.rfind
size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; size_type rfind( CharT ch, size_type pos = npos ) const noexcept;
寻找等于给定字符序列的最后子串。搜索始于 pos
,即找到的子串必须不始于 pos
后的位置。若将 npos 或任何不小于 size()-1 的值作为 pos
传递,则在整个字符串中搜索。
参数
str | - | 要搜索的 string |
pos | - | 开始搜索的位置 |
count | - | 要搜索的子串长度 |
s | - | 指向要搜索的字符串的指针 |
ch | - | 要搜索的字符 |
t | - | 要搜索的对象(可转换为 std::basic_string_view ) |
返回值
找到的子串的首字符位置,或若找不到这种子串则为 npos 。注意这是从字符串开始,而非末尾的偏移。
若搜索任何空字符串( str.size() 、 count 或 Traits::length(s) 为零),则返回 pos
(立即找到空字符串),除非 pos > size() (包括 pos == npos 的情况),该情况下返回 size() 。
这个函数的返回值还是从左边开始数的,只是寻找的时候从字符串的末尾开始找
例子:
#include <string> #include <iostream> void print(std::string::size_type n, std::string const &s){ if(n == std::string::npos){ std::cout << "not found "; }else{ std::cout << "found: "" << s.substr(n) << "" at " << n << ' '; } } int main(){ std::string::size_type n; std::string const s = "This is a string"; n = s.rfind("is");// 从字符串尾反向搜索 print(n,s); n = s.rfind("is", 4);// 从位置 4 反向搜索,s所以找到的是This的is print(n,s); n = s.rfind('a'); print(n,s); n = s.rfind('q'); print(n,s); }
返回:
found: "is a string" at 5 found: "is is a string" at 2 found: "a string" at 8 not found
3.find_first_of
constexpr size_type find_first_of( const basic_string& str, size_type pos = 0 ) const noexcept; size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
也是可用于查看字符和字符串,不同在于它会寻找str某个字符出现最早的位置,不一定要全部str
寻找等于给定字符序列中字符之一的首个字符。搜索只考虑区间 [pos
, size()) 。若区间中不存在字符,则返回 npos 。
参数
str | - | 鉴别要搜索的字符的 string |
pos | - | 搜索开始的位置 |
count | - | 鉴别要搜索的字符的字符串长度 |
s | - | 指向鉴别要搜索的字符的字符串的指针 |
ch | - | 鉴别要搜索的字符的字符 |
t | - | 鉴别要搜索的字符的对象(可转换为 std::basic_string_view ) |
返回值
找到的字符的位置,或若找不到这种字符则为 npos 。
例子:
#include <string> #include <iostream> int main(){ std::string str = std::string("Hello world!"); //用来搜索的字符和字符串 std::string search_str = std::string("o"); const char* search_cstr = "Good Bye!"; std::cout << str.find_first_of(search_str) << ' '; //找到第一个出现o的位置,即Hello的o std::cout << str.find_first_of(search_str, 5) << ' '; //找到从位置5开始第一个出现o的位置,即World的o std::cout << str.find_first_of(search_cstr) << ' '; //寻找字符串中与str有重叠的字符的第一个位置 std::cout << str.find_first_of(search_cstr, 5, 4) << ' '; ////寻找字符串search_cstr[0:4]中与str从位置5开始有重叠的字符的第一个位置 std::cout << str.find_first_of('x') << ' '; //不存在,则返回std::string::npos }
返回:
4 7 1 7 18446744073709551615
4.find_first_not_of
寻找不等于给定字符序列中任何字符的首个字符。搜索只考虑区间 [pos
, size()) 。若区间中不存在字符,则将返回 npos
参数
str | - | 鉴别要搜索的字符的 string |
pos | - | 搜索开始的位置 |
count | - | 鉴别要搜索的字符的字符串长度 |
s | - | 指向鉴别要搜索的字符的字符串的指针 |
ch | - | 鉴别要搜索的字符的字符 |
t | - | 鉴别要搜索的字符的对象(可转换为 std::basic_string_view ) |
返回值
找到的字符的位置,或若找不到这种字符则为 npos 。
例子:
#include <string> #include <iostream> int main(){ std::string to_search = "Some data with %MACROS to substitute"; std::cout << "Before: " << to_search << ' '; auto pos = std::string::npos; while((pos = to_search.find('%')) != std::string::npos){ // 宏名中容许大写字母、小写字母和数字,会找到空格的位置 const auto after = to_search.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", pos + 1); // 现在 to_search[pos] == '%' 而 to_search[after] == ' ' (在 'S' 后) if(after != std::string::npos){ to_search.replace(pos, after-pos, "some very nice macros"); } } std::cout << "After: " << to_search << ' '; }
返回:
Before: Some data with %MACROS to substitute
After: Some data with some very nice macros to substitute
5.find_last_of
寻找等于给定字符序列中字符之一的最后字符。不指定准确的搜索算法。搜索只考虑区间 [0, pos] 。若区间中不存在这种字符,则返回 npos 。
例子:
#include <string> #include <iostream> int main(){ const std::string path = "/root/config"; auto const pos = path.find_last_of('/'); const auto leaf = path.substr(pos+1); std::cout << leaf << ' '; std::cout << path.find_last_of("ct") << ' '; //找到config的c }
返回:
config 6
6.find_last_not_of
寻找不等于给定字符序列中任何字符的最后字符。搜索只考虑区间 [0, pos] 。若区间中不存在这种字符,则返回 npos 。
例子:
#include <string> #include <iostream> int main(){ const std::string path = "/root/config"; std::cout << path.find_last_not_of("ctg") << ' '; //找到config的i }
返回10
7.strchr
定义于头文件 <cstring>,所以下面应该#include <cstring>,但是发现<string>也没事,不知道为什么!!!!!!!
const char* strchr( const char* str, int ch ); char* strchr( char* str, int ch );
在 str
所指向的字节字符串中寻找字符 static_cast<char>(ch) 的首次出现。
认为终止空字符是字符串的一部分,而且若搜索 '