What does string::npos mean

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

What does the statement string::npos mean here

found=str.find(str2);  if (found!=string::npos)     cout 

回答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.



回答2:

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



回答3:

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.



回答4:

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



回答5:

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<:string::size_type>::max () which is (thanks to the standard integer promotions) usually comparable to -1.



回答6:

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



回答7:

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 ... } 


回答8:

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

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



回答9:

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,

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!