string类型的查找

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1("xiaocui");
	string::size_type pos1 = s1.find("cui");   // 查找的是下标,这是精确的查找,
	cout << pos1 << endl; // 输出的是4     

	s1 = "xiao1 cui2 ai3 ni ";
	string s2("1234567890");
	//string::size_type pos = s1.find_first_of(s2);  // find_first_of找第一个想重复的,
	//if (pos == string::npos)
	//	cout << "没有找到?" << endl;
	//else
	//	cout << "找到了,下标!" << pos << endl; // 输出是找到了,下标!2,

	string::size_type pos = 0;
	while ((pos = s1.find_first_of(s2, pos)) != string::npos)  // npos是没找到,
	{
		cout << s1[pos] << endl;  // 输出的是123  find_first_not_of 是找的不相等的,
		pos++;  
	}

	pos = 0;
	while ((pos = s1.find_last_of(s2, pos)) != string::npos)  //find_last_of从后边开始找,
	{
		cout << s1[pos] << endl;
		++pos;
	}



	string s3("jiaoshihsjs");
	string::size_type fist = s3.rfind("sh");  // rfind是从后边开始找,输出的还是从前边开始数的下标,
	cout << fist << endl;

	


	return 0;
}

你可能感兴趣的:(string类型的查找)