c++中stl裡提供了許多字串操作的函式,下面是字串查詢方面的部分函式用法簡介:
1.find()
查詢第一次出現的目標字串:
#include#includeusing namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2) ; //在s1中查詢子串s2
cout《說明:如果查詢成功則輸出查詢到的第乙個位置,否則返回-1
查詢從指定位置開始的第一次出現的目標字串:
#include#includeusing namespace std;
int main(){
string s1 = "abcdef";
string s2 = "de";
int ans = s1.find(s2, 2) ; //從s1的第二個字元開始查詢子串s2
cout<2.find_first_of()
查詢子串中的某個字元最先出現的位置。find_first_of()不是全匹配,而find()是全匹配
#include#includeusing namespace std;
int main(){
string s1 = "adedef";
string s2 = "dek";
int ans = s1.find_first_of(s2) ; //在s1中查詢子串s2
cout<
其中find_first_of()也可以約定初始查詢的位置:s1.find_first_of(s2, 2) ;
3.find_last_of()
這個函式與find_first_of()功能差不多,只不過find_first_of()是從字串的前面往後面搜尋,而find_last_of()是從字串的後面往前面搜尋。
4.rfind()
反向查詢字串,即找到最後乙個與子串匹配的位置
5.find_first_not_of()
找到第乙個不與子串匹配的位置
c 中find函式的用法
1,返回字元 字串 在原來字串的中首次出現的下標位置 例 string s 1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i position s.find jk 2,返回flag 中任意字元 在s 中第一次出現的下標位置 flag c position s.find fi...
c 中find函式的用法
find函式主要實現的是在容器內查詢指定的元素,並且這個元素必須是基本資料型別的。查詢成功返回乙個指向指定元素的迭代器,即元素在容器中的下標,查詢失敗返回end迭代器。標頭檔案 include 函式實現 templateinputiterator find inputiterator first,i...
Matlab中find函式用法
matlab中find函式用法 1.對官網上用法做一下簡單說明。m,n,v find logical expression,amount,direction m表示返回的行標,n表示返回的列下標,v表示在原判斷矩陣滿足條件位置數值,logical epression代表邏輯判斷表示式,amount表...