C 關於字串的一些常見用法

2021-08-22 04:59:09 字數 1295 閱讀 8848

關於字串由於平時題目用的很多所以整理一下

string是c++標準庫的乙個重要的部分,主要用於字串處理。

#include using namespace std;

int main()

看了這個**才知道,string也可以使用迭代器

#include using namespace std;

int main()

#include #include int main ()

#include #include #include using namespace std;

int main()

find和rfind查詢函式:find函式主要是查詢乙個字串是否在呼叫的字串中出現過,區分大小寫。 

#include using namespace std;

int main()

rfind函式就是找最後乙個出現的匹配字串,返回的位置仍然是從前往後數的。(貌似用kmp實現的)

#include using namespace std;

int main()

{ ios::sync_with_stdio(false);

std::string str ("the sixth sick sheik's sixth sheep's sick.");

std::string key ("sixth");// ^

//rfind是找最後乙個出現的匹配字串

std::size_t found = str.rfind(key);

if (found!=std::string::npos)

{cout<

​​​​​​​如果兩個字串相等,那麼返回0,呼叫物件大於引數返回1,小於返回-1。 在compare當中還支援部分比較,裡面有6個引數可以設定。

#include using namespace std;

int main() {

ios::sync_with_stdio(false);//消除同步流

string s1="123",s2="123";

cout知道字串也可以用迭代器,可以消除同步流加速cin。

關於字串的一些。。。

首先呼叫這些函式需要使用標頭檔案include 1 des src abcd xyz abcdxyz char mystrcat char des,const char src 字串連線 2 字串比較 0,0,0 int mystrcmp const char str1,const char str...

字串的一些用法總結

1.兩個用於訪問字串中特定字元 charat 和 charcodeat 兩者都只有乙個引數,不同的是前者返回字元,後者返回字元的字元編碼。var strin alue hello world console.log strin alue.charat 1 e var strin alue hello...

一些關於字串的函式

函式名 stpcpy 功 能 拷貝乙個字串到另乙個 用 法 char stpcpy char destin,char source 程式例 include include int main void 函式名 strcat 功 能 字串拼接函式 用 法 char strcat char destin,...