Namespaces
Variants

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

From cppreference.net
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 [static] 特殊值 size_type ( - 1 ) ,其具体含义取决于上下文