std::basic_string_view<CharT,Traits>::npos

出��cppreference.com
 
 
 
 
static constexpr size_type npos = size_type(-1);
(C++17 起)

這是等於 size_type 類型所能表示最大值的特殊值。確切的含義依賴於語境,不過通常被期待視圖索引的函數用作視圖尾指示器,或者被返回視圖索引的函數用作錯誤指示器。

示例

#include <string_view>

constexpr bool
contains(std::string_view const what, std::string_view const where) noexcept
{
    return std::string_view::npos != where.find(what);
}

int main()
{
    using namespace std::literals;

    static_assert(contains("water", "in a bottle of water"));
    static_assert(!contains("wine", "in a bottle of champagne"));
    static_assert(""sv.npos == "haystack"sv.find("needle"));
}

參閱

constexpr size_type npos [靜態] 特殊值 size_type(-1),它的確切含義依賴語境[編輯]