std::basic_string<CharT,Traits,Allocator>::npos
From cppreference.com
< 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
[編集] 関連項目
| [static] |
特別な値。正確な意味は文脈に依存する ( std::basic_string_view<CharT,Traits> の公開静的メンバ定数) |