The string.find() function and string::npos in C++

Find if string a contains substring b,
not strA.find(strB) > 0 but strA.find(strB) != string:npos
string::size_type pos = strA.find(strB);
if(pos ! = string::npos){}
-------------------------------------------- -
int idx = str.find("abc");
if (idx == string::npos)
...
In the above code, the type of idx is defined as int, which is wrong, even if it is defined as unsigned int , it must be defined as string::size_type.
npos is defined like this:
static const size_type npos = -1;

because string::size_type (defined by the string allocator) describes size, it needs to be an unsigned integer type. Because the default configurator uses size_t as size_type, -1 is converted to an unsigned integer type, and npos becomes the largest unsigned value for that type. However, the actual value depends on the actual definition of the type size_type. Unfortunately none of these maximums are the same. In fact, (unsigned long)-1 and (unsigned short)-1 are different (provided they have different types and sizes). Therefore, in the comparison idx == string::npos, if the value of idx is -1, since the types of idx and the string string::npos are different, the comparison result may get false.
The best way to tell if the result of find() is npos is to compare it directly:

if (str.find("abc") == string::npos) { ... }

Error: if(str.find( "abc") ) 
Note: if abc is not found, it will return -1, if it is not 0, it will be True. 0 is False 
--------------------------------------------------------- -

copy code

 ////find function return type size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;

//find function returns the subscript position of jk in s 
position = s.find("jk");
 if (position != s.npos) //If not found, return a special flag which is represented by npos in c++, where the value of npos is 4294967295,
 {
  cout << "position is : " << position << endl;
 }
 else
 {
  cout << "Not found the flag" + flag;
 } 


//find function returns the subscript position of the first occurrence of any character in flag in s
 flag = "c";
 position = s.find_first_of(flag) ;
 cout << "s.find_first_of(flag) is : " << position << endl;

 //Start at subscript 5 of string s, find string b,Return the subscript of b in s
 position=s.find("b",5);
 cout<<"s.find(b,5) is : "<<position<<endl;

//Find all positions where flag appears in s.
 flag="a";
 position=0;
 int i=1;
 while((position=s.find_first_of(flag,position))!=string::npos)
 {
  //position=s.find_first_of(flag,position);
  cout<<"position "<<i<<" : "<<position<<endl;
  position++;
  i++;
 }

 //Find the first position in flag that does not match s
 flag="acb12389efgxyz789";
 position=flag. find_first_not_of (s);
 cout<<"flag.find_first_not_of (s) :"<<position<<endl;


 //Reverse search, the last position of flag in s
 flag="3";
 position=s.rfind ( flag);

copy code

illustrate:

1. if string sub = "abc";

              string s = ”cdeabcigld“;

     The two functions s.find(sub) and s.rfind(sub) will only return the matching index if they match completely, that is, when s contains three consecutive letters of abc, the current index will be returned.

     The four functions s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) find the index of any letter in sub in s.

2. If no query is found, return string::npos, which is a large number, and its value does not need to be known.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981090&siteId=291194637
Recommended