C 學習紀錄 string容器 查詢

2021-10-08 14:23:39 字數 1739 閱讀 1259

1、int find(const string &str, int pos = 0) const查詢str第一次出現位置,從pos開始查詢

2、int find(const char *s, int pos = 0) const 查詢s第一次出現位置,從pos開始查詢

3、int find(const char *s, int pos, int n) const 從pos位置查詢s的前n個字元第一次位置

4、int find(const char c, int pos = 0) const從pos位置查詢字元c第一次位置

5、int rfind(const string &str, int pos = npos) const查詢str位置,從pos開始查詢(從右往左查詢)

6、int rfind(const char *s, int pos = npos) const 查詢s出現位置,從pos開始查詢(從右往左查詢)

7、int rfind(const char *s, int pos, int n) const 從pos開始查詢s前n個字元位置(從右往左查詢)

8、int rfind(const char c, int pos = 0) const查詢字元c出現位置(從右往左查詢)

一、int find(const string &str, int pos = 0) const查詢str第一次出現位置,從pos開始查詢

#include

#include

using

namespace std;

intmain()

執行結果:

可以看出string字串起始元素的的下標是0。不論開始查詢的位置是幾,返回的結果依舊是以0為基準。

3

請按任意鍵繼續.

..

二、int find(const char *s, int pos = 0) const 查詢s第一次出現位置,從pos開始查詢

略三、int rfind(const string &str, int pos = npos) const查詢str最後乙個位置,從pos開始查詢

(從右邊開始找)

#include

#include

using

namespace std;

intmain()

執行結果:

雖然是從右往左查詢,但返回的結果依舊是以0為基準,要查詢的字串首元素的位置

6

請按任意鍵繼續.

..

四、int rfind(const char *s, int pos = npos) const 查詢s出現位置,從pos開始查詢(從右往左找)

略五、int find(const char *s, int pos, int n) const 從pos位置查詢s的前n個字元第一次位置

#include

#include

using

namespace std;

intmain()

執行結果:

6

請按任意鍵繼續.

..

C 學習紀錄 string容器 賦值操作

string賦值操作 1 string operator const char s char 型別字串賦值給當前字串 2 string operator const string s string型別字串賦值給當前字串 3 string operator char c 把乙個字元c賦值給當前字串 4...

C 學習紀錄 string容器 插入和刪除

1 string insert int pos,const char s 插入字串 2 string insert int pos,const string str 插入字串 3 string insert int pos,int n,char c 在指定位置插入n個字元c 4 string era...

C 學習紀錄 string容器 字串的拼接

一 string operator const char str 過載 操作符 include include using namespace std intmain 執行結果 hello world 請按任意鍵繼續.二 string operator const char c 過載 操作符 inc...