Why doesn't std :: string.find (text, std :: string: npos) return npos?

I am doing a series of searches in a row, and somewhere along the row one of the rows will be skipped, and my set of queries should fail.

I expected that when the position reaches std :: string :: npos, it will remain there, but it is not. Passing std :: string :: npos to std :: string.find seems to start the search at the beginning again

std::string str("frederick");
std::string::size_type pos = str.find("der",std::string::npos);
TS_ASSERT_EQUALS(pos, std::string::npos); // FAIL, 3 is returned

Why is this not done to indicate the end of a line?

Update: The goal is to arrange the sequence of lines and check the result at the end

pos = str.find(string1, pos)
pos = str.find(string2, pos)
pos = str.find(string3, pos)
if (pos != std:string::npos)
{ // All strings found
+3
source share
7 answers

Looking at the spec, I think there might be a mistake in your implementation.

basic_string::find xpos , pos <= xpos xpos + str.size() <= size() at(xpos + I) == str.at(I) I, str.

basic_string::npos -1, , , . , xpos npos <= xpos, find npos , , npos basic_string::find npos .

+10

string:: find() string:: copy(). ( N2798, 21.3.7.2 21.3.6.7, . 686/687) . , :: copy ": pos <= size()". , string:: find pos <= size().

. , , , rqeuirements, string:: npos. , string:: npos, 21.3.7.2/1.


N2798 = 08-0308, ISO/IEC:

21.3.7.2 basic_string::find [string::find]

size_type find(const basic_string<charT,traits,Allocator>& str, size_type pos = 0) const;

1 : xpos, , , : - pos <= xpos xpos + str.size() <= size(); - traits::eq(at(xpos+I), str.at(I)) I , str. 2 : xpos, xpos. npos. 3 : traits::eq().

+4

std::string::npos std::string::find.

find npos , .

+3

, std:: search . .

std::string::const_iterator iter = str.begin();

iter = std::search( iter, str.end(), string1.begin(), string1.end() );
iter = std::search( iter, str.end(), string2.begin(), string2.end() );
iter = std::search( iter, str.end(), string3.begin(), string3.end() );
+3

undefined, npos:

[]
STL ( , ) string::npos , pos. - , .

. ( ISO, , ).

STL , (, ((size_type)-1)). , , . [/]

, 0 pos != npos , :

 pos = str.find(string1, 0)
 if (pos != std:string::npos)
   pos = str.find(string2, pos)
 if (pos != std:string::npos)
   pos = str.find(string3, pos)

 if (pos != std:string::npos)
 { 
   // All strings found
 }
+1

Passing std :: string :: npos as the second argument to search means "start the search after or after std :: string :: npos position in the string".

Obviously, this is not what you intended.

EDIT:

This can do what you originally planned:

string s;
string::size_type pos;

if ((pos = s.find(s1)) != string::npos && (pos = s.find(s2, pos)) != npos && 
    (pos = s.find(s3,pos)) != string::npos)
{
    // okay
}

I have not tested it, but it should work, you may prefer the style of petrified, as it is more readable.

0
source

You should use the length of the string as the starting position.

0
source

Source: https://habr.com/ru/post/1710746/


All Articles