PLM之家精品课程培训,联系电话:18301858168 QQ: 939801026

  • NX二次开培训

    NX二次开培训

    适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术对于老鸟也值得借鉴!.

    NX CAM二次开发培训报名 NX二次开发基础培训报名
  • PLM之家Catia CAA二次开发培训

    Catia二次开发培训

    Catia二次开发的市场大,这方面开发人才少,难度大。所以只要你掌握了开发,那么潜力巨大,随着时间的积累,你必将有所用武之地!

  • PLM之Teamcenter最佳学习方案

    Teamcenter培训

    用户应用基础培训,管理员基础培训,管理员高级培训,二次开发培训应有尽有,只要你感兴趣肯学习,专业多年经验大师级打造!

  • PLM之Tecnomatix制造领域培训

    Tecnomatix培训

    想了解制造领域数字化吗?想了解工厂,生产线设计吗?数字化双胞胎,工业4.0吗?我们的课程虚位以待!

PLM之家PLMHome-国产软件践行者

[转载电子书] C++中的string查找使用详解

[复制链接]

2014-3-9 12:47:32 4225 0

mildcat 发表于 2014-3-9 12:47:32 |阅读模式

mildcat 楼主

2014-3-9 12:47:32

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
: l6 f& S8 D% \- s6 M, T
函数名
描述
find查找
rfind反向查找
find_first_of查找包含子串中的任何字符,返回第一个位置
find_first_not_of查找不包含子串中的任何字符,返回第一个位置
find_last_of查找包含子串中的任何字符,返回最后一个位置
find_last_not_of查找不包含子串中的任何字符,返回最后一个位置
2 ?! |; O  d% p# N3 O. Y
以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数。
6 C0 n! V( y. i: E7 A; b4 ~0 H% M& l2 L; w

+ ~+ e* B8 {9 Z$ J. i2 k9 _) I/ ^
  • 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)  5 Y  p: v. B  K8 A, B
# R8 u* R0 A) E/ d
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)
5 |9 O9 J; Y+ @9 P( y; Y0 t* F
3 x5 w" s. z( W! F4 b% K所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。有一点需要特别注意,所有和string::npos的比较一定要用string::size_type来使用,不要直接使用int 或者unsigned int等类型。其实string::npos表示的是-1, 看看头文件:5 T2 y5 r9 @' p6 {- h3 I4 d+ L1 ]

9 v5 |+ F- t& {) a: Y- `6 ~: v/ t3 w2 P
  • 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;  ' U8 P, ~3 X/ U: u& b8 |1 }

- }, P# z; G7 W* [" f0 e6 \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; ! n$ I7 `  o, U0 f$ C2 l9 {8 ?/ E

' Z  X0 z, F6 G/ Y; [. C
find 和 rfind 都还比较容易理解,一个是正向匹配,一个是逆向匹配,后面的参数pos都是用来指定起始查找位置。对于find_first_of 和find_last_of 就不是那么好理解。
find_first_of 是给定一个要查找的字符集,找到这个字符集中任何一个字符所在字符串中第一个位置。或许看一个例子更容易明白。
有这样一个需求:过滤一行开头和结尾的所有非英文字符。看看用string 如何实现:

' `1 u$ y- @3 ^) u( o  P2 T2 M, R! v) C! B7 w) G% \
  • #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;  
  • }  
    7 C/ W7 S* d/ c! L) E* L

' J3 t/ Z% p% P' c* p#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;}
( _" D$ g4 g9 t2 E8 v* I& [" j! ?! t7 ^: h7 Z: w3 `7 V

& ?2 E( j6 q% K0 S' A
8 W  q  W' v! [. g
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了