#include <stdio.h>
#include <iostream>
using namespace Std;
void Main ()
{
Find function return type Size_type
String S ("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string Flag;
String::size_type position;
The 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 in C + + with the NPOs representation, I'm here NPOs value is 4294967295,
{
cout << "position is:" << position << Endl;
}
Else
{
cout << "Not found the flag" + flag;
}
The Find function returns the subscript position of any character in flag for the first occurrence in S
Flag = "C";
Position = s.find_first_of (flag);
cout << "s.find_first_of (flag) is:" << position << Endl;
Starting with the string s subscript 5, find the string B and return the subscript of B in S
Position=s.find ("B", 5);
cout<< "S.find (b,5) is:" <<position<<endl;
Finds all locations in s where flag appears.
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 position in flag that does not match s first
Flag= "acb12389efgxyz789";
Position=flag.find_first_not_of (s);
cout<< "Flag.find_first_not_of (s):" <<position<<endl;
Reverse lookup, where flag appears last in S
Flag= "3";
Position=s.rfind (flag);
cout<< "S.rfind (flag):" <<position<<endl;
}
Description
1. If the string sub = "abc";
string s = "CDEABCIGLD";
S.find (sub), S.rfind (sub) These two functions, if they match exactly, return the matching index, that is, when s contains ABC three consecutive letters, the current index is returned.
S.find_first_of (sub), s.find_first_not_of (sub), s.find_last_of (sub), s.find_last_not_of (sub) These four functions, Finds an index in S that contains any of the letters in a sub.
2. If no query is reached, the String::npos is returned, which is a large number and its value does not need to be known.
Summary of usage of string.find () function in C + + (reproduced)