C/C++之 C++ String(字符串)
Tips:
?1. 本人當(dāng)初學(xué)習(xí)C/C++的記錄。
?2. 資源很多都是來自網(wǎng)上的,如有版權(quán)請及時告知!
?3. 可能會有些錯誤。如果看到,希望能指出,以此共勉!
要想使用標(biāo)準(zhǔn)C++中string類,必須要
#include <string> // 注意是<string>,不是<string.h>,帶.h的是C語言中的頭文件 using std::string;或using namespace std;下面你就可以使用string了。
聲明一個C++字符串
??聲明一個字符串變量很簡單:
string str;
??這樣我們就聲明了一個字符串變量,但既然是一個類,就有構(gòu)造函數(shù)和析構(gòu)函數(shù)。上面的聲明沒有傳入?yún)?shù),所以就直接使用了string的默認(rèn)的構(gòu)造函數(shù),這個函數(shù)所作的就是把str初始化為一個空字符串。string類的構(gòu)造函數(shù)和析構(gòu)函數(shù)如下:
??當(dāng)構(gòu)造的string太長而無法表達時會拋出length_error異常
string對象的特性描述
1、int capacity()const; // 返回當(dāng)前容量(即string中不必增加內(nèi)存即可存放的元素個數(shù)) 2、int max_size()const; // 返回string對象中可存放的最大字符串的長度 3、int size()const; // 返回當(dāng)前字符串的大小 4、int length()const; // 返回當(dāng)前字符串的長度 5、bool empty()const; // 當(dāng)前字符串是否為空 6、void resize(int len, char c); // 把字符串當(dāng)前大小置為len,并用字符c填充不足的部分??這里另一個需要指出的是reserve()函數(shù),這個函數(shù)為string重新分配內(nèi)存。重新分配的大小由其參數(shù)決定,默認(rèn)參數(shù)為0,這時候會對string進行非強制性縮減。
string的賦值
1、string &operator=(const string &s); // 把字符串s賦給當(dāng)前字符串 2、string &assign(const char *s); // 用C風(fēng)格字符串s賦值 3、string &assign(const char *s, int n); // 用C風(fēng)格字符串s開始的n個字符賦值(注意:越界問題) 4、string &assign(const string &s); // 把字符串s賦給當(dāng)前字符串 5、string &assign(int n, char c); // 用n個字符c賦值給當(dāng)前字符串 6、string &assign(const string &s, int start, int n); // 把字符串s中從start開始的n個字符賦給當(dāng)前字符串(如果n 超過了s的長度,則返回,不夠n個也結(jié)束) 7、string &assign(const_iterator first,const_itertor last);// 把first和last迭代器之間的部分賦給字符串string的連接
1、string &operator+=(const string &s); // 把字符串s連接到當(dāng)前字符串的結(jié)尾 2、string &append(const char *s); // 把c類型字符串s連接到當(dāng)前字符串結(jié)尾 3、string &append(const char *s, int n); // 把c類型字符串s的前n個字符連接到當(dāng)前字符串結(jié)尾 4、string &append(const string &s); // 同operator+=() 5、string &append(const string &s, int pos, int n); // 把字符串s中從pos開始的n個字符連接到當(dāng)前字符串的結(jié)尾 6、string &append(int n,char c); // 在當(dāng)前字符串結(jié)尾添加n個字符c 7、string &append(const_iterator first,const_iterator last); // 把迭代器first和last之間的部分連接到當(dāng)前字符串的結(jié)尾 8、void push_back(char ch); // 在當(dāng)前字符串后面拼接上字符ch注意:
可以將string對象和字符串常量直接連接,但是,必須保證+兩側(cè)至少有一個收聽對象。例如:string str13 = “hello”+”+str; 是錯誤的
string的比較:
bool operator==(const string &s1,const string &s2)const; // 比較兩個字符串是否相等 //運算符">","<",">=","<=","!="均被重載用于字符串的比較; int compare(const string &s) const;//比較當(dāng)前字符串和s的大小 int compare(int pos, int n,const string &s)const;//比較當(dāng)前字符串從pos開始的n個字符組成的字符串與s的大小 int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當(dāng)前字符串從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函數(shù)在>時返回1,<時返回-1,==時返回0??C++字符串支持常見的比較操作符( >,>=,<,<=,==,!= ),甚至支持string與C-string的比較(如 str< “hello”)。在使用>,>=,<,<=這些操作符的時候是根據(jù)“當(dāng)前字符特性”將字符按字典順序逐一進行比較。字典排序靠前的字符小,比較的順序是從前向后比較,遇到不相等的字符就按這個位置上的兩個字符的比較結(jié)果確定兩個字符串的大小。舉例如下:
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); // 交換當(dāng)前字符串與s2的值
string類的查找函數(shù)
int find(char c, int pos = 0) const; //從pos開始查找字符c在當(dāng)前字符串的位置 int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當(dāng)前串中的位置 int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當(dāng)前串中的位置 int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當(dāng)前串中的位置 //查找成功時返回所在位置,失敗返回string::npos的值 int rfind(char c, int pos = npos) const;//從pos開始從后向前查找字符c在當(dāng)前串中的位置 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個字符組成的字符串在當(dāng)前串中的位置,成功返回所在位置,失敗時返回string::npos的值 int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現(xiàn)的位置 int find_first_of(const char *s, int pos = 0) const; int find_first_of(const char *s, int pos, int n) const; int find_first_of(const string &s, int pos = 0) const; //從pos開始查找當(dāng)前串中第一個在s的前n個字符組成的數(shù)組里的字符的位置。查找失敗返回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; //從當(dāng)前串中查找第一個不在串s中的字符出現(xiàn)的位置,失敗返回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類的替換函數(shù)
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類的插入函數(shù):
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個字符cstring類的刪除函數(shù):
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(); // 刪除當(dāng)前字符串的最后一個字符string類的迭代器處理:
??string提供了向前和向后遍歷的迭代器iterator,迭代器提供了訪問各個字符的語法,類似于指針操作,迭代器不檢查范圍。
用string::iterator或string::const_iterator 聲明迭代器變量,const_iterator不允許改變迭代器的內(nèi)容,但是其自身可以改變。
??通過設(shè)置迭代器string:: reverse_iterator, string::const_reverse_iterator實現(xiàn)從后向前
??string 迭代器可以使用解引用符(*)以及自增自減運算符(++/–)
迭代器可以用==或!=進行比較,指向同一元素則為相等
C++中string對象中的字符操作
??經(jīng)常要對 string 對象中的單個字符進行處理,例如,通常需要知道某個特殊字符是否為空白字符、字母或數(shù)字。下表列出了各種字符操作函數(shù),適用于 string 對象的字符(或其他任何 char 值)。這些函數(shù)都在cctype頭文件中定義。
| int isalnum( int ch ) | 若 ch 是字母或數(shù)字,則為 True。 | |
| int isalpha( int ch ) | 若 ch 是字母,則返回非零 | |
| int iscntrl(int ch ) | 若 ch 是控制字符,則返回非零 | 0~0x1F |
| int isdigit(int ch ) | 若 ch 是數(shù)字,則為 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 是標(biāo)點符號,則 true。 | |
| int isspace(int ch) | 若ch 是空白(空格、換行等)字符,則為 true。 | |
| int isupper(int ch) | 若ch 是大寫字母,則 true。 | |
| int isxdigit(int ch) | 若ch是 十六進制數(shù)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,則返回非零 |
??可以使用下標(biāo)操作符[]及at()函數(shù)對string對象元素進行索引
const char &operator[](int n)const; // 運算符的重載
const char &at()(int n)const; // 成員函數(shù)
char &operator[](int n);
char &at()(int n);
const char *data()const; // 返回一個非null終止的c風(fēng)格的字符數(shù)組
operator[]和at()均返回當(dāng)前字符串中第n個字符的位置,但at函數(shù)提供范圍檢查,當(dāng)越界時會拋出out_of_range異常,下標(biāo)運算符[]不提供檢查訪問。
C++風(fēng)格字符串和C風(fēng)格字符串的轉(zhuǎn)換
??由C++風(fēng)格字符串得到對應(yīng)的C風(fēng)格字符串的方法如下:
const char * data() const; // 以字符數(shù)組的形式返回字符串內(nèi)容,但并不添加'\0'。(VS中也會添加'\0'??) const char * c_str() const; // 返回一個以'\0'結(jié)尾的字符數(shù)組 int copy(char *s, int n, int pos) const; // 將當(dāng)前字符串從pos開始的n個字符拷貝到s中
??由C風(fēng)格字符串得到對應(yīng)的C++風(fēng)格字符串的方法:直接賦值即可。
C++字符串并不以’\0’結(jié)尾。
字符串中的中文操作
??每個漢字為兩個字符。
C++11新增原始字符串
??所謂的原始字符串就是,字符串中每個字符都表示他自己,例如:\0不再是轉(zhuǎn)義字符,而是表示\和0。再例如:再也不用使用\”來輸出”了。
原始字符串使用()來界定字符串的范圍,并且使用前綴R來識別原始字符串。
??在支持C++11特性的編譯器中,上圖中的兩句效果是一樣的!VS2010不支持
總結(jié)
以上是生活随笔為你收集整理的C/C++之 C++ String(字符串)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++之函数
- 下一篇: Qt 之 Qt/Qt Lite 自编译详