请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
9 m& Y: Q! J. |' z+ P* U9 w- ]函数名 | 描述 | | find | 查找 | | rfind | 反向查找 | | find_first_of | 查找包含子串中的任何字符,返回第一个位置 | | find_first_not_of | 查找不包含子串中的任何字符,返回第一个位置 | | find_last_of | 查找包含子串中的任何字符,返回最后一个位置 | | find_last_not_of | 查找不包含子串中的任何字符,返回最后一个位置 | 3 }0 E& U& ~3 F
以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数。
* G' A8 U% y( U) }: w+ [4 N9 C
' H9 I1 o4 N- t8 ]7 {2 T$ O! p2 `! |3 V6 J: k W0 T) T9 t
- size_type find_first_of(const basic_string& s, size_type pos = 0)
- size_type find_first_of(const charT* s, size_type pos, size_type n)
- size_type find_first_of(const charT* s, size_type pos = 0)
- size_type find_first_of(charT c, size_type pos = 0)
0 f& ^" A: h) l/ f$ U
: Z! u \; F# X' C* Psize_type find_first_of(const basic_string& s, size_type pos = 0)size_type find_first_of(const charT* s, size_type pos, size_type n)size_type find_first_of(const charT* s, size_type pos = 0)size_type find_first_of(charT c, size_type pos = 0) % t* y( w1 p1 E& k8 G$ j, ]
) c7 I* s9 e0 h. M所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。有一点需要特别注意,所有和string::npos的比较一定要用string::size_type来使用,不要直接使用int 或者unsigned int等类型。其实string::npos表示的是-1, 看看头文件:4 S+ E8 ?3 f, H: v' G) p2 M: a `
! V4 T4 b7 h& M, P" T9 r4 S+ \
% ^& \- @$ F0 K2 f1 g; C9 g- A- template <class _CharT, class _Traits, class _Alloc>
- const basic_string<_CharT,_Traits,_Alloc>::size_type
- basic_string<_CharT,_Traits,_Alloc>::npos
- = basic_string<_CharT,_Traits,_Alloc>::size_type) -1;
3 ^: v/ G) e- M5 {2 U 8 r" k* W/ a- g3 x x3 a9 G
template <class _CharT, class _Traits, class _Alloc> const basic_string<_CharT,_Traits,_Alloc>::size_type basic_string<_CharT,_Traits,_Alloc>::npos = basic_string<_CharT,_Traits,_Alloc>::size_type) -1; $ ^9 Z! @% Y! B9 i" \
n2 J5 F) P3 n3 m M
find 和 rfind 都还比较容易理解,一个是正向匹配,一个是逆向匹配,后面的参数pos都是用来指定起始查找位置。对于find_first_of 和find_last_of 就不是那么好理解。 find_first_of 是给定一个要查找的字符集,找到这个字符集中任何一个字符所在字符串中第一个位置。或许看一个例子更容易明白。 有这样一个需求:过滤一行开头和结尾的所有非英文字符。看看用string 如何实现: 2 g1 y* C9 X* |, F U9 Z1 `: j8 H; V
; Q0 v. b) H* M# B
- #include <string>
- #include <iostream>
- using namespace std;
- int main(){
- string strinfo=" //*---Hello Word!......------";
- string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- int first = strinfo.find_first_of(strset);
- if(first == string::npos) {
- cout<<"not find any characters"<<endl;
- return -1;
- }
- int last = strinfo.find_last_of(strset);
- if(last == string::npos) {
- cout<<"not find any characters"<<endl;
- return -1;
- }
- cout << strinfo.substr(first, last - first + 1)<<endl;
- return 0;
- }
+ `* T u3 @! {/ K5 T % h) a0 K1 e5 h$ J9 @5 _
#include <string>#include <iostream>using namespace std;int main(){ string strinfo=" //*---Hello Word!......------"; string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int first = strinfo.find_first_of(strset); if(first == string::npos) { cout<<"not find any characters"<<endl; return -1; } int last = strinfo.find_last_of(strset); if(last == string::npos) { cout<<"not find any characters"<<endl; return -1; } cout << strinfo.substr(first, last - first + 1)<<endl; return 0;}) B6 M) @" J6 r$ Y: k9 F
& @ e1 _ S8 @% a+ Z
' W% Y/ H1 s4 ^9 N0 E
& \: v5 @" a; R5 O- B |