命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
 
static constexpr size_type npos = size_type(-1);
(since 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),其确切含义取决于上下文[编辑]