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"));
}

参阅

[静态]
特殊值。确切含义依赖语境
(std::basic_string<CharT,Traits,Allocator> 的公开静态成员常量)