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>
の
公開静的メンバー定数)
|