.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 <iostream>
#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 <iostream>
#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只會查找子串內元素在母串第一次出現的位置,
.find_last_of
函式原型:
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const noexcept;
回傳改子串的最后一個字符的索引位置
http://www.cplusplus.com/reference/string/string/find_last_of/
使用樣例:
#include <iostream>
#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_last_of(b);//結果將輸出7
return 0;
}
從樣例示范的輸出結果可以得知.find_last_of與.find_first_of一樣,都不是查找完整字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82417.html
標籤:C++
上一篇:錯誤/警告型別總結——comparison between signed and unsigned integer expressions
下一篇:最火的開源 IDE介紹與安裝教程
