std::basic_string<CharT,Traits,Allocator>::npos
来自 cppreference.cn
< cpp | string | 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> 的公共静态成员常量) |