C string小貼士總結

2021-09-29 08:08:38 字數 1992 閱讀 4310

標頭檔案#include 從c++11開始引入以下函式

標頭檔案#include

下面的pos就是size_t型別的位置。p是size_t型別的,其值會被修改數值後第乙個字元所在位置(第乙個字元為0)亦即數值部分的結束位置

[外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳(img-6r09xfnl-1572874547858)(g:\mypdf\markdown\2018041022234933.jpg)]

示例:string s = 「123abc」;

size_t p;

stoi(「123abc」,&p);

str2 = s.substr§; // 從p位置開始,輸出"abc"

s.find(s1) 查詢s中第一次出現s1的位置,並返回(包括0)

s.find(s1)         查詢s中第一次出現s1的位置,並返回(包括0)

s.rfind(s1) 查詢s中最後次出現s1的位置,並返回(包括0)

s.find_first_of(s1) 查詢在s1中任意乙個字元在s中第一次出現的位置,並返回(包括0)

s.find_last_of(s1) 查詢在s1中任意乙個字元在s中最後一次出現的位置,並返回(包括0)

s.find_first_not_of(s1) 查詢s中第乙個不屬於s1中的字元的位置,並返回(包括0)

s.find_last_not_of(s1) 查詢s中最後乙個不屬於s1中的字元的位置,並返回(包括0)

s.find()查詢不到時,返回乙個特別的靜態常量,在c++中被定義為std::string::npos;經測試npos=4294967295,該值為unsigned int的理論最大值,轉化為有符號即-1;

擷取子串:

s.substr(pos, n) 擷取s中從pos開始(包括0)的n個字元的子串,並返回

s.substr(pos) 擷取s中從從pos開始(包括0)到末尾的所有字元的子串,並返回

替換子串:

s.replace(pos, n, s1) 用s1替換s中從pos開始(包括0)的n個字元的子串

string內建s.c_str(),返回乙個const char*指標

示例:string s = 「123」;

​ const char* p = s.c_str();

string型別的輸入只能用cin,而輸出可以使用cout和printf(「%s」,s.c_str())

string &insert(int p0, const char *s);——在p0位置插入字串s

string &insert(int p0, const char *s, int n);——在p0位置插入字串s的前n個字元

string &insert(int p0,const string &s);——在p0位置插入字串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字串s從pos開始的連續n個字元

string &insert(int p0, int n, char c);//在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it處插入從first開始至last-1的所有字元

void insert(iterator it, int n, char c);//在it處插入n個字元c

另外string還有一種最簡單的在string末尾插入字元的方法,即「+」,在string中過載了「+、>、<、==」運算子。

示例: string s = 「 」;

​ s = s+『a』+『b』; //正確

​ s += 『a』+『b』; //錯誤

c string函式總結

總結一些string函式 string a qwe string b asd a.insert 2,b cout 輸出 qwasde string a qwe a.erase 0,1 刪除第乙個字元 cout 結果 we string a qwe a.replace 2,3,dfg 將a的第2個位置...

c string互換char總結

string有2個函式可以用來轉換。c str data 這倆個函式返回的都是const char 轉換成char 需要使用strcpy函式。直接賦值 直接賦值。這樣輸出時只能使用std cout來執行吧。可以直接賦值。但是也會出現上面的問題。需要同樣的處理。不可直接賦值,單個取出,賦給char 附...

版本小貼士

major 具有相同名稱但不同主版本號的程式集不可互換。例如,這適用於對產品的大量重寫,這些重寫使得無法實現向後相容性。minor 如果兩個程式集的名稱和主版本號相同,而次版本號不同,這指示顯著增強,但照顧到了向後相容性。例如,這適用於產品的修正版或完全向後相容的新版本。revision 名稱 主版...