查找字符串

string类查找子串
.find
函数原型:

size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
返回子串的起始索引位置

http://www.cplusplus.com/reference/string/string/find/

使用样例:

include <bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(false);
string a;
string b;
a = "qweqweqweq";
b = "we";
cout << a.find(b);//结果将输出1
return 0;
}
.find_first_of
函数原型:

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;
返回子串的起始索引位置

http://www.cplusplus.com/reference/string/string/find_first_of/

使用样例:

include <bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(false);
string a;
string b;
a = "123w456o";
b = "wo";
cout << a.find_first_of(b);//结果将输出3
return 0;
}
与.find的区别: .find将会查找完整子串,而.find_first_of只会查找子串内元素在母串第一次出现的位置。

咱们山顶见。
原文地址:https://www.cnblogs.com/NKshen/p/14040436.html