这是STL里的吧? string里包含了basic_string类, 调用find方法: basic_string::find size_type find(E c, size_type pos = 0) const; size_type find(const E *s, size_type pos = 0) const; size_type find(const E *s, size_type pos, size_type n) const; size_type find(const basic_string& str, size_type pos = 0) const;If it succeeds, it returns the position where the matching subsequence begins. Otherwise, the function returns npos. (basic_string::npos static const size_type npos = -1;) 如果成功找到返回位置,否则返回-1.
This sample code searches for every instance of the string "cat" in a given string and counts the total number of instances:string input; int i = 0; int cat_appearances = 0;getline(cin, input, '\n');i = input.find("cat", 0); for(; i != string::npos; i = input.find("cat", i)) { cat_appearances++; i++; // Move past the last discovered instance to avoid finding same // string } cout<<cat_appearances;
string str; str=L"sdfsadf"; int pos =str.find(L"sa"); pos=-1 没有找到,pos大于0就是找到的位置
string里包含了basic_string类,
调用find方法:
basic_string::find
size_type find(E c, size_type pos = 0) const;
size_type find(const E *s, size_type pos = 0) const;
size_type find(const E *s, size_type pos, size_type n) const;
size_type find(const basic_string& str, size_type pos = 0) const;If it succeeds, it returns the position where the matching subsequence begins. Otherwise, the function returns npos.
(basic_string::npos
static const size_type npos = -1;)
如果成功找到返回位置,否则返回-1.
int i = 0;
int cat_appearances = 0;getline(cin, input, '\n');i = input.find("cat", 0);
for(; i != string::npos; i = input.find("cat", i))
{
cat_appearances++;
i++; // Move past the last discovered instance to avoid finding same
// string
}
cout<<cat_appearances;
str=L"sdfsadf";
int pos =str.find(L"sa");
pos=-1 没有找到,pos大于0就是找到的位置