std::basic_string<CharT,Traits,Allocator>:: npos
From cppreference.net
<
cpp
|
string
|
basic string
C++
Strings library
| Classes | ||||
|
(C++17)
|
||||
std::basic_string
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
static
const
size_type npos
=
-
1
;
|
||
这是一个特殊值,等于类型
size_type
可表示的最大值。其具体含义取决于上下文,但通常被需要字符串索引的函数用作字符串结束指示符,或被返回字符串索引的函数用作错误指示符。
说明
虽然定义使用了
-
1
,但
size_type
是无符号整数类型,由于
有符号到无符号的隐式转换
,
npos
的值是其可容纳的最大正值。这是指定任何无符号类型最大值的可移植方法。
示例
运行此代码
#include <bitset> #include <iostream> #include <string> int main() { // 字符串搜索函数若未找到内容则返回 npos std::string s = "test"; if (s.find('a') == s.npos) std::cout << "no 'a' in 'test'\n"; // 接受字符串子集作为参数的函数 // 使用 npos 作为“直到末尾”的指示符 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
参见
|
[static]
|
特殊值。具体含义取决于上下文
(std::basic_string_view<CharT,Traits> 的公开静态成员常量) |