这段代码中的 string::npos 是什么意思?

发布于 2024-09-25 04:40:35 字数 230 浏览 3 评论 0原文

下面的代码片段中的短语 std::string::npos 是什么意思?

found = str.find(str2);

if (found != std::string::npos)
    std::cout << "first 'needle' found at: " << int(found) << std::endl;

What does the phrase std::string::npos mean in the following snippet of code?

found = str.find(str2);

if (found != std::string::npos)
    std::cout << "first 'needle' found at: " << int(found) << std::endl;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

梦明 2024-10-02 04:40:35

就是没找到的意思。

它通常是这样定义的:

static const size_t npos = -1;

最好与 npos 进行比较,而不是与 -1 进行比较,因为代码更清晰。

It means not found.

It is usually defined like so:

static const size_t npos = -1;

It is better to compare to npos instead of -1 because the code is more legible.

指尖凝香 2024-10-02 04:40:35

string::npos 是一个表示非位置的常量(可能是-1)。当未找到模式时,它由 find 方法返回。

string::npos is a constant (probably -1) representing a non-position. It's returned by method find when the pattern was not found.

離人涙 2024-10-02 04:40:35

string::npos 的文档说:

npos 是一个静态成员常量值,对于 size_t 类型的元素具有最大可能值。

作为返回值,它通常用于指示失败。

该常量实际上定义为 -1 值(对于任何特征),因为 size_t 是无符号整数类型,因此成为该类型的最大可能可表示值。

The document for string::npos says:

npos is a static member constant value with the greatest possible value for an element of type size_t.

As a return value it is usually used to indicate failure.

This constant is actually defined with a value of -1 (for any trait), which because size_t is an unsigned integral type, becomes the largest possible representable value for this type.

油饼 2024-10-02 04:40:35

size_t 是一个无符号变量,因此“unsigned value = - 1”会自动使其成为 size_t 的最大可能值:18446744073709551615

size_t is an unsigned variable, thus 'unsigned value = - 1' automatically makes it the largest possible value for size_t: 18446744073709551615

请持续率性 2024-10-02 04:40:35

std::string::npos 是实现定义的索引,它始终超出任何 std::string 实例的范围。各种 std::string 函数返回它或接受它以表示超出字符串结尾的情况。它通常是某种无符号整数类型,其值通常是 std::numeric_limits::max () (由于标准整数提升)通常是可比较的到-1

std::string::npos is implementation defined index that is always out of bounds of any std::string instance. Various std::string functions return it or accept it to signal beyond the end of the string situation. It is usually of some unsigned integer type and its value is usually std::numeric_limits<std::string::size_type>::max () which is (thanks to the standard integer promotions) usually comparable to -1.

素年丶 2024-10-02 04:40:35

如果在搜索字符串中找不到子字符串,found 将是 npos

found will be npos in case of failure to find the substring in the search string.

山人契 2024-10-02 04:40:35

我们必须使用 string::size_type 作为 find 函数的返回类型,否则与 string::npos 的比较可能不起作用。
size_type,由字符串的分配器定义,必须是unsigned
积分型。默认分配器 allocator 使用类型 size_t 作为 size_type。因为 -1
转换为无符号整数类型,npos 是其类型的最大无符号值。然而,
确切的值取决于类型 size_type 的确切定义。不幸的是,这些最大
价值观不同。事实上,(unsigned long)-1(unsigned Short)-1 不同,如果
类型不同。因此,

idx == std::string::npos

如果 idx 的值为 -1 并且 idx 和 string::npos 具有不同的类型,则比较可能会产生 false:

std::string s;
...
int idx = s.find("not found"); // assume it returns npos
if (idx == std::string::npos) { // ERROR: comparison might not work
...
}

避免此错误的一种方法是检查是否搜索直接失败:

if (s.find("hi") == std::string::npos) {
...
}

但是,通常您需要匹配字符位置的索引。因此,另一个简单的解决方案
是为 npos 定义您自己的有符号值:

const int NPOS = -1;

现在比较看起来有点不同,甚至更方便:

if (idx == NPOS) { // works almost always
...
}

we have to use string::size_type for the return type of the find function otherwise the comparison with string::npos might not work.
size_type, which is defined by the allocator of the string, must be an unsigned
integral type. The default allocator, allocator, uses type size_t as size_type. Because -1 is
converted into an unsigned integral type, npos is the maximum unsigned value of its type. However,
the exact value depends on the exact definition of type size_type. Unfortunately, these maximum
values differ. In fact, (unsigned long)-1 differs from (unsigned short)-1 if the size of the
types differs. Thus, the comparison

idx == std::string::npos

might yield false if idx has the value -1 and idx and string::npos have different types:

std::string s;
...
int idx = s.find("not found"); // assume it returns npos
if (idx == std::string::npos) { // ERROR: comparison might not work
...
}

One way to avoid this error is to check whether the search fails directly:

if (s.find("hi") == std::string::npos) {
...
}

However, often you need the index of the matching character position. Thus, another simple solution
is to define your own signed value for npos:

const int NPOS = -1;

Now the comparison looks a bit different and even more convenient:

if (idx == NPOS) { // works almost always
...
}
别理我 2024-10-02 04:40:35
$21.4 - "static const size_type npos = -1;"

它由字符串函数返回,指示错误/未找到等。

$21.4 - "static const size_type npos = -1;"

It is returned by string functions indicating error/not found etc.

°如果伤别离去 2024-10-02 04:40:35

静态常量 size_t npos = -1;

size_t npos 的最大值

是一个静态成员常量值,具有 size_t 类型元素的最大可能值。

当该值用作字符串成员函数中 len(或 sublen)参数的值时,表示“直到字符串末尾”。

作为返回值,通常用于指示不匹配。

该常量定义为值 -1,因为 size_t 是无符号整数类型,所以它是该类型的最大可能表示值。

static const size_t npos = -1;

Maximum value for size_t

npos is a static member constant value with the greatest possible value for an element of type size_t.

This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string".

As a return value, it is usually used to indicate no matches.

This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

ヤ经典坏疍 2024-10-02 04:40:35

string::npos 的值为 18446744073709551615。如果没有找到字符串,则返回该值。

Value of string::npos is 18446744073709551615. Its a value returned if there is no string found.

痴情 2024-10-02 04:40:35

正如其他人提到的,string::npos 它是 size_t 的最大值

它的定义是这样的:

static constexpr auto npos{static_cast<size_type>(-1)};

对错误的答案获得了投票感到困惑。

这是一个快速测试示例:

int main()
{
    string s = "C   :";
    size_t i = s.rfind('?');
    size_t b = size_t (-1);
    size_t c = (size_t) -1;
    cout<< i <<" == " << b << " == " << string::npos << " == " << c;

    return 0;
}

输出:

18446744073709551615 == 18446744073709551615 == 18446744073709551615 == 18446744073709551615

...Program finished with exit code 0

As others have mentioned, string::npos it's the maximum value for size_t.

Here is its definition:

static constexpr auto npos{static_cast<size_type>(-1)};

Puzzled that the wrong answer got the vote.

And here is a quick testing sample:

int main()
{
    string s = "C   :";
    size_t i = s.rfind('?');
    size_t b = size_t (-1);
    size_t c = (size_t) -1;
    cout<< i <<" == " << b << " == " << string::npos << " == " << c;

    return 0;
}

output:

18446744073709551615 == 18446744073709551615 == 18446744073709551615 == 18446744073709551615

...Program finished with exit code 0
偏爱你一生 2024-10-02 04:40:35

当我们有 std:: 时,C++17 的这些日子的答案可选

如果你眯着眼睛假装std::string::find()返回一个std::Optional; (它应该......) - 那么条件就变成:

auto position = str.find(str2);

if ( position.has_value() ) {
    std::cout << "first 'needle' found at: " << position.value() << std::endl;
}

An answer for these days of C++17, when we have std::optional:

If you squint a bit and pretend std::string::find() returns an std::optional<std::string::size_type> (which it sort of should...) - then the condition becomes:

auto position = str.find(str2);

if ( position.has_value() ) {
    std::cout << "first 'needle' found at: " << position.value() << std::endl;
}
静待花开 2024-10-02 04:40:35

我也想知道当我使用 substr() 时 npos 是什么,因为 npos 是子字符串的默认长度,如下所示:

string substr (size_t pos = 0, size_t len = npos) const;

对我来说重要的是要注意的是子字符串不会比原始字符串的长度长,这就是 len 的 npos 起作用的原因,如下所示:

子字符串是对象中以字符开头的部分
位置 pos 并跨越 len 个字符(或直到字符串末尾,
以先到者为准)。

I too was wondering what npos was when I was using substr(), since npos is the default length of the substring, as per:

string substr (size_t pos = 0, size_t len = npos) const;

The important thing for me to note was that the substring will not be longer than the length of the original string, which is why a len of npos works, as per:

The substring is the portion of the object that starts at character
position pos and spans len characters (or until the end of the string,
whichever comes first).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文