名稱空間
變體
操作

std::basic_string<CharT,Traits,Allocator>::npos

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::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> 的公共靜態成員常量) [編輯]