C++ string::npos 在这段代码中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3827926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does string::npos mean in this code?
提问by boom
What does the phrase std::string::npos
mean in the following snippet of code?
std::string::npos
以下代码片段中的短语是什么意思?
found = str.find(str2);
if (found != std::string::npos)
std::cout << "first 'needle' found at: " << int(found) << std::endl;
回答by Brian R. Bondy
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.
最好与 npos 而不是 -1 进行比较,因为代码更清晰。
回答by Sheldon L. Cooper
string::npos
is a constant (probably -1
) representing a non-position. It's returned by method find
when the pattern was not found.
string::npos
是一个常数(可能-1
)代表一个非位置。find
当找不到模式时,它由方法返回。
回答by codaddict
The document for string::npos
says:
的文件string::npos
说:
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.
npos 是一个静态成员常量值,对于 size_t 类型的元素具有最大可能值。
作为返回值,它通常用于指示失败。
这个常量实际上定义为 -1 的值(对于任何特征),因为 size_t 是无符号整数类型,成为该类型的最大可能表示值。
回答by codaddict
size_t
is an unsigned variable, thus 'unsigned value = - 1' automatically makes it the largest possible value for size_t
: 18446744073709551615
size_t
是一个无符号变量,因此 'unsigned value = - 1' 自动使它成为最大的可能值size_t
: 18446744073709551615
回答by wilx
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
.
std::string::npos
是实现定义的索引,它总是超出任何std::string
实例的范围。各种std::string
函数返回它或接受它以在字符串情况结束后发出信号。它通常是一些无符号整数类型,它的值通常std::numeric_limits<std::string::size_type>::max ()
是(由于标准整数提升)通常与-1
.
回答by Debashish
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
我们必须使用string::size_type
find 函数的返回类型,否则与 的比较string::npos
可能不起作用。
size_type
由字符串的分配器定义,必须是unsigned
整数类型。默认分配器 allocator 使用 type size_t
as size_type
。因为-1
转换为无符号整数类型,npos 是其类型的最大无符号值。但是,确切的值取决于 type 的确切定义size_type
。不幸的是,这些最大值不同。实际上,如果类型的大小(unsigned long)-1
不同,则与(unsigned short)-
1 不同。因此,比较
idx == std::string::npos
might yield false if idx has the value -1
and idx and string::npos
have different types:
如果 idx 具有 value-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
...
}
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:
但是,通常您需要匹配字符位置的索引。因此,另一个简单的解决方案是为 npos 定义您自己的有符号值:
const int NPOS = -1;
Now the comparison looks a bit different and even more convenient:
现在比较看起来有点不同,甚至更方便:
if (idx == NPOS) { // works almost always
...
}
回答by Raghuram
found
will be npos
in case of failure to find the substring in the search string.
found
将npos
在无法在搜索字符串中找到子字符串的情况下。
回答by Chubsdad
.4 - "static const size_type npos = -1;"
It is returned by string functions indicating error/not found etc.
它由指示错误/未找到等的字符串函数返回。
回答by asad_nitp
npos is just a token value that tells you that find() did not find anything (probably -1 or something like that). find() checks for the first occurence of the parameter, and returns the index at which the parameter begins. For Example,
npos 只是一个标记值,它告诉您 find() 没有找到任何东西(可能是 -1 或类似的东西)。find() 检查参数的第一次出现,并返回参数开始的索引。例如,
string name = "asad.txt";
int i = name.find(".txt");
//i holds the value 4 now, that's the index at which ".txt" starts
if (i==string::npos) //if ".txt" was NOT found - in this case it was, so this condition is false
name.append(".txt");
回答by Leninkumar
static const size_t npos = -1;
静态常量 size_t npos = -1;
Maximum value for size_t
size_t 的最大值
npos is a static member constant value with the greatest possible value for an element of type size_t.
npos 是一个静态成员常量值,对于 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".
该值在字符串成员函数中用作 len(或 sublen)参数的值时,表示“直到字符串结束”。
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.
此常量定义为值 -1,因为 size_t 是无符号整数类型,因此它是此类型的最大可能表示值。