命名空间
变体
操作

std::basic_string<CharT,Traits,Allocator>::npos

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
static const size_type npos = -1;

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

[编辑] 注意

尽管定义使用了 -1, size_type 是一个无符号整数类型,并且由于有符号到无符号的隐式转换npos 的值是它可以容纳的最大正值。这是一种指定任何无符号类型的最大值的可移植方法。

[编辑] 示例

#include <bitset>
#include <iostream>
#include <string>
 
int main()
{
    // string search functions return npos if nothing is found
    std::string s = "test";
    if (s.find('a') == s.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

[编辑] 参见

[静态]
特殊值。确切含义取决于上下文
(std::basic_string_view<CharT,Traits> 的公共静态成员常量) [编辑]