首页 > 编程语言 > 详细

C++ string的查找函数和npos特殊值

时间:2016-04-17 17:25:38      阅读:229      评论:0      收藏:0      [点我收藏+]

STL中的string有6个查找函数:

1.find()

2.rfind()

从最后一个字符开始往前找。

3.find_first_of()

4.find_not_first_of()

5.find_last_of()

6.find_not_last_of()

所有这些查找函数返回值都是size_type类型(找到了)或者是一个名为 string::npos的特殊值(没找到)。

string::npos常用来表示没找到的结果或者string类型的末尾。

 

#include <iostream>
#include <bitset>
#include <string>
 
int main()
{
    // string search functions return npos if nothing is found
    std::string s = "test";
    if(s.find(a) == std::string::npos)
        std::cout << "no ‘a‘ in ‘test‘\n";
 
    // functions that take string subsets as arguments 
    // use npos as the "all the way to the end" indicator
    std::string s2(s, 2, std::string::npos);
    std::cout << s2 << \n;
 
    std::bitset<5> b("aaabb", std::string::npos, a, b);
    std::cout << b << \n;
}
//输出结果
/*
no ‘a‘ in ‘test‘
st
00011
*/

 

 

 

C++ string的查找函数和npos特殊值

原文:http://www.cnblogs.com/zywscq/p/5401318.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!