An example of c++ string search functions find_first_of and find_last_of

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
  string numbers("0123456789"),name("r3d2");
  string::size_type pos;
  pos = name.find_last_not_of(numbers,3);
  cout << pos << endl;
  char cp = '2';

  cout << "new find:" << endl;
  pos = name.find_last_of(cp,2);
  cout << pos << endl; 
  return 0;
}

代码运行结果:
r@r:~/computer_langugage/c++/9/9.5.3$ ./123
2
new find:
18446744073709551615
r@r:~/computer_langugage/c++/9/9.5.3$ 

评注:从结果来看,也即是没找到。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
  string numbers = {"0123456789"};
  string dept = {"03714p3"};
  string::size_type pos = dept.find_last_not_of(numbers,4);
  cout << pos << endl;
  
  return 0;
}

运行结果:
r@r:~/computer_langugage/c++/9/9.5.3$ ./123
18446744073709551615
评注:结果也就是string::npos,也就是没找到。

Cause Analysis:

 

Guess you like

Origin blog.csdn.net/digitalkee/article/details/108209249