在string中查找字符或字符串
程序清单16.4
#include<string>
#include<iostream>
using namespace std;
int main(){
string s = "You can do it!";
//find substring "it" from position 0
int p1 = s.find("it", 0);
//use string::npos check if substring was founded
if(p1 != string::npos)
cout << "its position is " << p1 << endl;
else
cout << "not found" << endl;
//find character
int p2 = s.find('Y', 0);
if(p2 != string::npos)
cout << "its position is " << p2 << endl;
else
cout << "not found" << endl;
//多次查找 o
int p3 = s.find('o', 0);
if(p3 != string::npos)
do{
cout << "its position is " << p3 << endl;
p3 = s.find('o', p3+1);
}while(p3 != string::npos);
else
cout << "not found" << endl;
}