日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++之 C++ String(字符串)

發布時間:2024/10/14 c/c++ 86 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++之 C++ String(字符串) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Tips:
?1. 本人當初學習C/C++的記錄。
?2. 資源很多都是來自網上的,如有版權請及時告知!
?3. 可能會有些錯誤。如果看到,希望能指出,以此共勉!

要想使用標準C++中string類,必須要

#include <string> // 注意是<string>,不是<string.h>,帶.h的是C語言中的頭文件 using std::string;或using namespace std;

下面你就可以使用string了。

聲明一個C++字符串

??聲明一個字符串變量很簡單:
string str;
??這樣我們就聲明了一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把str初始化為一個空字符串。string類的構造函數和析構函數如下:

??當構造的string太長而無法表達時會拋出length_error異常

string對象的特性描述

1int capacity()const; // 返回當前容量(即string中不必增加內存即可存放的元素個數) 2int max_size()const; // 返回string對象中可存放的最大字符串的長度 3int size()const; // 返回當前字符串的大小 4int length()const; // 返回當前字符串的長度 5bool empty()const; // 當前字符串是否為空 6void resize(int len, char c); // 把字符串當前大小置為len,并用字符c填充不足的部分

??這里另一個需要指出的是reserve()函數,這個函數為string重新分配內存。重新分配的大小由其參數決定,默認參數為0,這時候會對string進行非強制性縮減。

string的賦值

1string &operator=(const string &s); // 把字符串s賦給當前字符串 2string &assign(const char *s); // 用C風格字符串s賦值 3string &assign(const char *s, int n); // 用C風格字符串s開始的n個字符賦值(注意:越界問題) 4string &assign(const string &s); // 把字符串s賦給當前字符串 5string &assign(int n, char c); // 用n個字符c賦值給當前字符串 6string &assign(const string &s, int start, int n); // 把字符串s中從start開始的n個字符賦給當前字符串(如果n 超過了s的長度,則返回,不夠n個也結束) 7string &assign(const_iterator first,const_itertor last);// 把first和last迭代器之間的部分賦給字符串

string的連接

1string &operator+=(const string &s); // 把字符串s連接到當前字符串的結尾 2string &append(const char *s); // 把c類型字符串s連接到當前字符串結尾 3string &append(const char *s, int n); // 把c類型字符串s的前n個字符連接到當前字符串結尾 4string &append(const string &s); // 同operator+=() 5string &append(const string &s, int pos, int n); // 把字符串s中從pos開始的n個字符連接到當前字符串的結尾 6string &append(int n,char c); // 在當前字符串結尾添加n個字符c 7string &append(const_iterator first,const_iterator last); // 把迭代器first和last之間的部分連接到當前字符串的結尾 8void push_back(char ch); // 在當前字符串后面拼接上字符ch

注意:
可以將string對象和字符串常量直接連接,但是,必須保證+兩側至少有一個收聽對象。例如:string str13 = “hello”+”+str; 是錯誤的

string的比較:

bool operator==(const string &s1,const string &s2)const; // 比較兩個字符串是否相等 //運算符">","<",">=","<=","!="均被重載用于字符串的比較; int compare(const string &s) const;//比較當前字符串和s的大小 int compare(int pos, int n,const string &s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小 int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小 int compare(const char *s) const; int compare(int pos, int n,const char *s) const; int compare(int pos, int n,const char *s, int pos2) const; //compare函數在>時返回1,<時返回-1,==時返回0

??C++字符串支持常見的比較操作符( >,>=,<,<=,==,!= ),甚至支持string與C-string的比較(如 str< “hello”)。在使用>,>=,<,<=這些操作符的時候是根據“當前字符特性”將字符按字典順序逐一進行比較。字典排序靠前的字符小,比較的順序是從前向后比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。舉例如下:

string s(“abcd”); s.compare(“abcd”); // 返回0 s.compare(“dcba”); // 返回一個小于0的值 s.compare(“ab”); // 返回大于0的值 s.compare(s); // 相等 s.compare(0,2,s,2,2); // 用”ab”和”cd”進行比較 小于零 s.compare(1,2,”bcx”,2); // 用”bc”和”bc”比較。

string的子串

??string substr(int pos = 0,int n = npos) const; // 返回pos開始的n個字符組成的字符串

string的交換

??void swap(string &s2); // 交換當前字符串與s2的值

string類的查找函數

int find(char c, int pos = 0const; //從pos開始查找字符c在當前字符串的位置 int find(const char *s, int pos = 0const;//從pos開始查找字符串s在當前串中的位置 int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置 int find(const string &s, int pos = 0const;//從pos開始查找字符串s在當前串中的位置 //查找成功時返回所在位置,失敗返回string::npos的值 int rfind(char c, int pos = npos) const;//從pos開始從后向前查找字符c在當前串中的位置 int rfind(const char *s, int pos = npos) const; int rfind(const char *s, int pos, int n = npos) const; int rfind(const string &s, int pos = npos) const; //從pos開始從后向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值 int find_first_of(char c, int pos = 0const;//從pos開始查找字符c第一次出現的位置 int find_first_of(const char *s, int pos = 0const; int find_first_of(const char *s, int pos, int n) const; int find_first_of(const string &s, int pos = 0const; //從pos開始查找當前串中第一個在s的前n個字符組成的數組里的字符的位置。查找失敗返回string::npos int find_first_not_of(char c, int pos = 0) const; int find_first_not_of(const char *s, int pos = 0) const; int find_first_not_of(const char *s, int pos,int n) const; int find_first_not_of(const string &s, int pos = 0) const; //從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos int find_last_of(char c, int pos = npos) const; int find_last_of(const char *s, int pos = npos) const; int find_last_of(const char *s, int pos , int n ) const; int find_last_of(const string &s, int pos = npos) const; int find_last_not_of(char c, int pos = npos) const; int find_last_not_of(const char *s, int pos = npos) const; int find_last_not_of(const char *s, int pos , int n ) const; int find_last_not_of(const string &s, int pos = npos) const; // find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過其是從后向前檢查

string類的替換函數

string &replace( int p0, int n0, const char * s ); // 刪除從p0開始的n0個字符,然后在p0處插入串s string &replace( int p0, int n0, const char * s, int n ); // 刪除從p0開始的n0個字符,然后在p0處插入串s的前n個字符 string &replace( int p0, int n0, const string & s ); // 刪除從p0開始的n0個字符,然后在p0處插入串s string &replace( int p0, int n0, const string & s ,int pos, int n ); // 刪除從p0開始的n0個字符,然后在p0處插入串s中從pos開始的n個字符 string &replace( int p0, int n0, int n, char c ); // 刪除從p0開始的n0個字符,然后在p0處插入n個字符c string &replace( iterator first0, iterator last0, const char *s ); // 把first0和 last0之間的部分替換為字符串s string &replace( iterator first0, iterator last0, const char *s, int n ); // 把first0和 last0之間的部分替換為字符串s的前n個字符 string &replace( iterator first0, iterator last0, const string &s ); // 把first0和 last0之間的部分替換為字符串s string &replace( iterator first0, iterator last0, int n, char c ); // 把first0和 last0之間的部分替換為n個字符c string &replace( iterator first0, iterator last0,const_iterator first, const_iterator last ); // 把first0和 last0之間的部分替換為first和 last之間的字符串

string類的插入函數:

string &insert( int p0, const char *s ); string &insert( int p0, const char *s , int n ); string &insert( int p0, const string &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_iterator last );// 在it處插first至last之間的字符 void insert( iterator it, int n, char c ); // 在it處插入n個字符c

string類的刪除函數:

iterator erase( iterator first, iterator last ); // 刪除first至last之間的所有字符,返回刪除后迭代器的位置 iterator erase( iterator it ); // 刪除it指向的字符,返回刪除后迭代器的位置 string &erase( int pos = 0, int n = npos ); // 刪除pos開始的n個字符,返回修改后的字符串 void pop_back(); // 刪除當前字符串的最后一個字符

string類的迭代器處理:

??string提供了向前和向后遍歷的迭代器iterator,迭代器提供了訪問各個字符的語法,類似于指針操作,迭代器不檢查范圍。
用string::iterator或string::const_iterator 聲明迭代器變量,const_iterator不允許改變迭代器的內容,但是其自身可以改變。
??通過設置迭代器string:: reverse_iterator, string::const_reverse_iterator實現從后向前
??string 迭代器可以使用解引用符(*)以及自增自減運算符(++/–)
迭代器可以用==或!=進行比較,指向同一元素則為相等

C++中string對象中的字符操作

??經常要對 string 對象中的單個字符進行處理,例如,通常需要知道某個特殊字符是否為空白字符、字母或數字。下表列出了各種字符操作函數,適用于 string 對象的字符(或其他任何 char 值)。這些函數都在cctype頭文件中定義。

函數原型作用ASCII碼值
int isalnum( int ch )若 ch 是字母或數字,則為 True。
int isalpha( int ch )若 ch 是字母,則返回非零
int iscntrl(int ch )若 ch 是控制字符,則返回非零0~0x1F
int isdigit(int ch )若 ch 是數字,則為 true。
int isgraph(int ch )若 ch 不是空格,但可打印,則返回非零0x21~0x7E
int islower(int ch )若 ch 是小寫字母,則為 true。
int isprint(int ch)若ch 是可打印的字符,則為 true。0x20~0x7E
int ispunct(int ch)若ch 是標點符號,則 true。
int isspace(int ch)若ch 是空白(空格、換行等)字符,則為 true。
int isupper(int ch)若ch 是大寫字母,則 true。
int isxdigit(int ch)若ch是 十六進制數0~9,A~F,a~f,則為 true。
int tolower(int ch)若ch 大寫字母,返回其小寫字母形式,否則直接返回 c。
int toupper(int ch )若ch 是小寫字母,則返回其大寫字母形式,否則直接返回 c。
int isascii( int ch )若ch是ASCII碼0~127,則返回非零

??可以使用下標操作符[]及at()函數對string對象元素進行索引
const char &operator[](int n)const; // 運算符的重載
const char &at()(int n)const; // 成員函數
char &operator[](int n);
char &at()(int n);
const char *data()const; // 返回一個非null終止的c風格的字符數組
operator[]和at()均返回當前字符串中第n個字符的位置,但at函數提供范圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問。

C++風格字符串和C風格字符串的轉換

??由C++風格字符串得到對應的C風格字符串的方法如下:

const char * data() const; // 以字符數組的形式返回字符串內容,但并不添加'\0'。(VS中也會添加'\0'??) const char * c_str() const; // 返回一個以'\0'結尾的字符數組 int copy(char *s, int n, int pos) const; // 將當前字符串從pos開始的n個字符拷貝到s中


??由C風格字符串得到對應的C++風格字符串的方法:直接賦值即可。

C++字符串并不以’\0’結尾。

字符串中的中文操作

??每個漢字為兩個字符。

C++11新增原始字符串

??所謂的原始字符串就是,字符串中每個字符都表示他自己,例如:\0不再是轉義字符,而是表示\和0。再例如:再也不用使用\”來輸出”了。
原始字符串使用()來界定字符串的范圍,并且使用前綴R來識別原始字符串。

??在支持C++11特性的編譯器中,上圖中的兩句效果是一樣的!VS2010不支持

總結

以上是生活随笔為你收集整理的C/C++之 C++ String(字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。