1 字元基礎
1.1 單位元組字符集(single-byte character set(SBCS))
顧名思義,在這種模式下,所有的字元都只用一個位元組表示,常見的如,ASCII
1.2 多位元組字符集(multi-byte character set(MBCS))
在Windows中MBCS包含兩種字元類型,單位元組字元和雙位元組字元.由於windows使用的多位元組字元絕大部分是兩個位元組長,所以MBCS常被DBCS代替。
1.3 unicode
Unicode是一種所有的字元都使用兩個位元組編碼的編碼模式。Unicode字元有時也被稱作寬字元,因為它比單子節字元寬(使用了更多的存儲空間)。
常見的為utf-8,還有一個比較少用的是utf-16
2 C/C++/VC++中的字元串
2.1 C中的字元串
C語言中, 沒有字元串的數據類型,使用一個以NULL(‘\0’)字元結尾的字元數組來保存字元串。
char* p = “Hello world”;
char* p = (char*)malloc (100 * sizeof(char));
char p[100] = “hello world”;
2.2 C++ 中的字元串
C++ 語言中有單獨字元串類型,在string頭文件中,還有對應的寬字元串wstring.
string iStr = “Hello world”;
wstring wStr = “Hello world”;
3 標準C++ 字元串操作
3.1 構造方法
string(const char *s); //用c字元串s初始化
string(int n,char c); //用n個字元c初始化
此外,string類還支持默認構造函數和複製構造函數,如string s1;string s2=”hello”;都是正確的寫法。當構造的string太長而無法表達時會拋出length_error異常
3.2 操作方法
3.2.1 字元操作
const char &operator[](int n)const; // 索引操作,取單個字元
const char &at(int n)const;// 同上
char &operator[](int n);//同上
char &at(int n);//同上
operator[]和at()均返回當前字元串中第n個字元的位置,但at函數提供範圍檢查,當越界時會拋出out_of_range異常,下標運算符[]不提供檢查訪問。
const char *data()const;//返回一個非null終止的c字元數組
const char *c_str()const;//返回一個以null終止的c字元串
int copy(char *s, int n, int pos = 0) const;//把當前串中以pos開始的n個字元拷貝到以s為起始位置的字元數組中,返回實際拷貝的數目
3.2.2 string的特性描述
int capacity()const; //返回當前容量(即string中不必增加內存即可存放的元素個數)
int max_size()const; //返回string對象中可存放的最大字元串的長度
int size()const; //返回當前字元串的大小
int length()const; //返回當前字元串的長度
bool empty()const; //當前字元串是否為空
void resize(int len,char c);//把字元串當前大小置為len,並用字元c填充不足的部分
3.2.3 string類的輸入輸出操作
string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作。
函數getline(istream &in,string &s);用於從輸入流in中讀取字元串到s中,以換行符’\n’分開。
3.2.4 string的賦值
string &operator=(const string &s);//把字元串s賦給當前字元串
string &assign(const char *s);//用c類型字元串s賦值
string &assign(const char *s,int n);//用c字元串s開始的n個字元賦值
string &assign(const string &s);//把字元串s賦給當前字元串
string &assign(int n,char c);//用n個字元c賦值給當前字元串
string &assign(const string &s,int start,int n);//把字元串s中從start開始的n個字元賦給當前字元串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字元串
3.2.5 string的連接
string &operator+=(const string &s);//把字元串s連接到當前字元串的結尾
string &append(const char *s); //把c類型字元串s連接到當前字元串結尾
string &append(const char *s,int n);//把c類型字元串s的前n個字元連接到當前字元串結尾
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos,int n);//把字元串s中從pos開始的n個字元連接到當前字元串的結尾
string &append(int n,char c); //在當前字元串結尾添加n個字元c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當前字元串的結尾
3.2.6 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
3.2.7 string的子串
string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字元組成的字元串
3.2.8 string的交換
void swap(string &s2); //交換當前字元串與s2的值
3.2.9 string類的查找函數
int find(char c, int pos = 0) const;//從pos開始查找字元c在當前字元串的位置
int find(const char *s, int pos = 0) const;//從pos開始查找字元串s在當前串中的位置
int find(const char *s, int pos, int n) const;//從pos開始查找字元串s中前n個字元在當前串中的位置
int find(const string &s, int pos = 0) const;//從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 = 0) const;//從pos開始查找字元c第一次出現的位置
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開始查找當前串中第一個在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 = npos) 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相似,只不過是從後向前查找
3.2.10 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)之間的字元串
3.2.11 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);
//前4個函數在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
3.2.12 string類的刪除函數
iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置
iterator erase(iterator it);//刪除it指向的字元,返回刪除後迭代器的位置
string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字元,返回修改後的字元串
3.2.13 string類的迭代器處理
string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字元的語法,類似於指針操作,迭代器不檢查範圍。
用string::iterator或string::const_iterator聲明迭代器變數,const_iterator不允許改變迭代的內容。常用迭代器函數有:
const_iterator begin()const;
iterator begin(); //返回string的起始位置
const_iterator end()const;
iterator end(); //返回string的最後一個字元後面的位置
const_iterator rbegin()const;
iterator rbegin(); //返回string的最後一個字元的位置
const_iterator rend()const;
iterator rend(); //返回string第一個字元位置的前面
rbegin和rend用於從後向前的迭代訪問,通過設置迭代器string::reverse_iterator,
string::const_reverse_iterator實現
需要學習的加C語言C++學習交流群 496926338
關於學習態度
學習嘛,在學習的過程中自然會遇到一些問題,遇到問題自然會想到求助。古人有云:禮下於人,必有所求,言下之意是有求於人,必要禮下於人。可是現在有些人不僅不禮下於人,反而還趾高氣揚,彷彿誰欠著他似的。我們老家有句俗話叫拜師學藝要吃二十四碗鼻涕,可見在學習過程中要受到多少的委屈。
吃得苦中苦,方為人上人,做技術尤是如此。我以前在學ASP的時候,同寢室有一哥們是學VB的,因為ASP也是用VBScript做為默認腳本,所以有很多問題請教他,他當是也是給了我不少的嘲笑,像「這些簡單的問題都還要問」之類的話我倒是聽了不少。但到後我們一起學Delphi的時候,我就已經超過他了。
新人們碰到問題的時候,第一個想法就是想找人解決,當你去幫他解決的時候,他卻去做其它事了,好像這個問題是需要你來解決的,下次出現同樣的問題他還得找你。遇到問題最好的辦法是先排查問題,然後到互聯網上查找問題的原因,這樣你才不會忘記,不要忽略了互聯網這個最好的老師。我以前在學ASP的時候,為了一個插入數據的問題,查了一個星期,但這個問題我永遠也忘記不了,再也不會范同樣的錯誤。
高手們一般都比較忙,雖然我不是高手,但我還是不原意在我寫代碼正酣的時候,有人打斷我的思路。先把問題記下來,趁機會問問高手,不要讓他為你解決問題,你要的是一種解決問題的思路。公司常常有新人問我,這個地方出問題了,怎麼辦?我便問他,你上網查了嗎?不是我不願意幫他,而是授人魚不如授人以漁,我直接告訴他怎麼做,那不是在幫他,那是我在害他,我只能是告訴他排錯的方法,解決問題的
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/223063.html
微信掃一掃
支付寶掃一掃