std::basic_string<CharT,Traits,Allocator>::npos
提供: cppreference.com
< cpp | string | basic string
staticconst size_type npos =-1; | ||
これは size_type
型で表現可能な最大値と等しい特別な値です。 正確な意味は文脈によりますが、一般的には文字列のインデックスを期待する関数で文字列の終端として使用されたり、文字列のインデックスを返す関数でエラーインジケータとして使用されたりします。
[編集]ノート
定義は -1 を使用していますが、 size_type は符号なし整数型であり、 npos
の値は、符号付きから符号なしへの暗黙の変換により、保持できる正の最大値となります。 これはあらゆる符号なし型の最大値を指定する移植性のある方法です。
[編集]例
Run this code
#include <iostream>#include <bitset>#include <string> int main(){// string search functions return npos if nothing is foundstd::string s ="test";if(s.find('a')== std::string::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" indicatorstd::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