这段代码中的 string::npos 是什么意思?
下面的代码片段中的短语 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
就是没找到的意思。
它通常是这样定义的:
最好与 npos 进行比较,而不是与 -1 进行比较,因为代码更清晰。
It means not found.
It is usually defined like so:
It is better to compare to npos instead of -1 because the code is more legible.
string::npos
是一个表示非位置的常量(可能是-1
)。当未找到模式时,它由find
方法返回。string::npos
is a constant (probably-1
) representing a non-position. It's returned by methodfind
when the pattern was not found.string::npos
的文档说:The document for
string::npos
says:size_t
是一个无符号变量,因此“unsigned value = - 1”会自动使其成为size_t
的最大可能值:18446744073709551615size_t
is an unsigned variable, thus 'unsigned value = - 1' automatically makes it the largest possible value forsize_t
: 18446744073709551615std::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 anystd::string
instance. Variousstd::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 usuallystd::numeric_limits<std::string::size_type>::max ()
which is (thanks to the standard integer promotions) usually comparable to-1
.如果在搜索字符串中找不到子字符串,
found
将是npos
。found
will benpos
in case of failure to find the substring in the search string.我们必须使用
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 的值为
-1
并且 idx 和string::npos
具有不同的类型,则比较可能会产生 false:避免此错误的一种方法是检查是否搜索直接失败:
但是,通常您需要匹配字符位置的索引。因此,另一个简单的解决方案
是为 npos 定义您自己的有符号值:
现在比较看起来有点不同,甚至更方便:
we have to use
string::size_type
for the return type of the find function otherwise the comparison withstring::npos
might not work.size_type
, which is defined by the allocator of the string, must be anunsigned
integral type. The default allocator, allocator, uses type
size_t
assize_type
. Because-1
isconverted 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 maximumvalues differ. In fact,
(unsigned long)-1
differs from(unsigned short)-
1 if the size of thetypes differs. Thus, the comparison
might yield false if idx has the value
-1
and idx andstring::npos
have different types:One way to avoid this error is to check whether the search fails directly:
However, often you need the index of the matching character position. Thus, another simple solution
is to define your own signed value for npos:
Now the comparison looks a bit different and even more convenient:
它由字符串函数返回,指示错误/未找到等。
It is returned by string functions indicating error/not found etc.
静态常量 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.
string::npos 的值为 18446744073709551615。如果没有找到字符串,则返回该值。
Value of string::npos is 18446744073709551615. Its a value returned if there is no string found.
正如其他人提到的,string::npos 它是 size_t 的最大值。
它的定义是这样的:
对错误的答案获得了投票感到困惑。
这是一个快速测试示例:
输出:
As others have mentioned, string::npos it's the maximum value for size_t.
Here is its definition:
Puzzled that the wrong answer got the vote.
And here is a quick testing sample:
output:
当我们有
std:: 时,C++17 的这些日子的答案可选
:如果你眯着眼睛假装
std::string::find()
返回一个std::Optional;
(它应该......) - 那么条件就变成:An answer for these days of C++17, when we have
std::optional
:If you squint a bit and pretend
std::string::find()
returns anstd::optional<std::string::size_type>
(which it sort of should...) - then the condition becomes:我也想知道当我使用 substr() 时 npos 是什么,因为 npos 是子字符串的默认长度,如下所示:
对我来说重要的是要注意的是子字符串不会比原始字符串的长度长,这就是 len 的 npos 起作用的原因,如下所示:
I too was wondering what npos was when I was using substr(), since npos is the default length of the substring, as per:
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: