命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
 
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),其确切含义取决于上下文[编辑]