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

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
*/


原文链接: https://www.cnblogs.com/zywscq/p/5401318.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/232035

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午3:16
下一篇 2023年2月13日 下午3:17

相关推荐