首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_string::npos

static const size_type npos = -1;

这是一个特殊值,它等于可由类型表示的最大值。size_type确切的含义取决于上下文,但是它通常被期望字符串索引的函数用作字符串指示符的结尾,或者由返回字符串索引的函数用作错误指示符。

虽然定义使用了-1,,,size_type是无符号整数类型,且npos是它所能持有的最大正值,因为有符号到无符号的隐式转换这是一种可移植的方法,可以指定任何无符号类型的最大值。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
no 'a' in 'test'
st
00011

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券