// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "string"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str = "Searches the string for the first occurrence of the sequence specified by its arguments.";
string find_str = "the";
size_t find_str_postion;
find_str_postion = str.find(find_str);
if (find_str_postion != string::npos)
{
cout << "found it.." << endl;
}
else
{
cout << "not found..." << endl;
}
return 0;
}
顺便来回忆一下find_first_of的用法:在父串中查找字串中的每个字符出现的首个位置,比方说测试字符为"te",父串为"zengraoli",先在父串中找t的位置,没找到,找e的位置,为1,那么此时返回1
[cpp] view plaincopyprint?
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "string"
#include "iostream"
using namespace std;
#include "fstream"
int _tmain(int argc, _TCHAR* argv[])
{
string str = "zengraoli.";
string find_str = "te";
size_t find_str_postion;
find_str_postion = str.find_first_of(find_str);
if (find_str_postion != string::npos)
{
cout << "pos : " << find_str_postion << endl;
cout << "found it.." << endl;
}
else
{
cout << "not found..." << endl;
}
return 0;
}