C string中的find 函式

2021-10-02 07:22:32 字數 2167 閱讀 6426

1.string中find()返回值是字母在母串中的位置(下標記錄),如果沒有找到,那麼會返回乙個特別的標記npos。(返回值可以看成是乙個int型的數)

#include#include#includeusing namespace std;

int main()

else

}

執行結果:

2.返回子串出現在母串中的首次出現的位置,和最後一次出現的位置。

#include#include#includeusing namespace std;

int main()

執行結果:

3.查詢某一給定位置後的子串的位置

#include#include#includeusing namespace std;

int main()

執行結果:

4.查詢所有子串在母串**現的位置

#include#include#includeusing namespace std;

int main()

}

執行結果:

5.反向查詢子串在母串**現的位置,通常我們可以這樣來使用,當正向查詢與反向查詢得到的位置不相同說明子串不唯一。

#include#include#includeusing namespace std;

int main()

執行結果:

例題:

1.給出乙個字串,串中會出現有人名,找到乙個只有乙個人名的字串。

2.你有n個字串。 每個字串由小寫英文本母組成。 重新排序給定的字串,使得對於每個字串,在它之前的所有字串都是它的子串。

3.查詢區間內子串在母串中的個數。

#include#include#include#include#includeusing namespace std;

int main()

for (i = 1; i <= q; i++)

}//printf("%d\n", counts);

cout << counts << endl;

}return 0;

}

C String中的find用法

includestring 是c 中乙個非常重要函式。在處理字串的時候經常用到。find是string中乙個查詢函式。示例 上 include includeusing namespace std int main st1.find a 1 後面的數字代表從什麼位置開始查詢。如果不加,預設從位置0 ...

MFC的CString的find的使用

名稱 cstring find 在乙個較大的 字元 串中查詢字元或子字串 int find tchar ch const int find lpctstr lpszsub const int find tchar ch,int nstart const int find lpctstr pstr,i...

matlab中的find函式

參考部落格 find函式返回非0元素的索引 返回元素在原矩陣 向量中的索引 矩陣中的元素是一列一列儲存的 numel a 返回矩陣或向量中的元素總數 影象分割 繪製超畫素邊界 完整 sx,sy vl grad double segments type forward 計算二維影象的梯度 s find...