Namespaces
Variants
Actions

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

From cppreference.com
 
C++
 
Strings library
 
std::basic_string_view
static constexpr size_type npos = size_type(-1);
(since C++17)

This is a special value equal to the maximum value representable by the type size_type. The exact meaning depends on context, but it is generally used either as end of view indicator by the functions that expect a view index or as the error indicator by the functions that return a view index.

[edit] Example

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

[edit] See also

constexpr size_type npos [static] the special value size_type(-1), its exact meaning depends on the context[edit]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant