C++ string資料一
之所以拋棄char *的字符串而選用C++標準程序庫中的string類,是因爲他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作爲一個類出現,他集成的操作函數足以完成我們大多數情況下(甚至是100%)的需要。我們可以用 = 進行賦值操作,== 進行比較,+ 做串聯(是不是很簡單?)。我們儘可以把它看成是C++的基本數據類型。
首先,爲了在我們的程序中使用string類型,我們必須包含頭文件 <string>。如下:
#include <string> //注意這裏不是string.h。string.h是C字符串頭文件,如還有 #include <iostream.h>也要改寫成#include <iostream>
1.聲明一個C++字符串
聲明一個字符串變量很簡單:
string 變量名,如: string Str;
這樣我們就聲明瞭一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化爲一個空字符串。String類的構造函數和析構函數如下:
String類的構造函數和析構函數如下:
a) string s; //生成一個空字符串s。
b) string s(str) //拷貝構造函數 生成str的複製品。
c) string s(str,stridx) //將字符串str內“始於位置stridx”的部分當作字符串的初值。
d) string s(str,stridx,strlen) //將字符串str內“始於stridx且長度頂多strlen”的部分作爲字符串的初值。
e) string s(cstr) //將C字符串作爲s的初值。
f) string s(chars,chars_len) //將C字符串前chars_len個字符作爲字符串s的初值。
g) string s(num,c) //生成一個字符串,包含num個c字符。
h) string s(beg,end) //以區間beg;end(不包含end)內的字符作爲字符串s的初值。
i) s.~string() //銷燬所有字符,釋放內存
都很簡單,就不解釋了。
2.字符串操作函數
這裏是C++字符串的重點,先把各種操作函數羅列出來,不喜歡把所有函數都看完的人可以在這裏找自己喜歡的函數,再到後面看他的詳細解釋。
a) =,assign() //賦以新值
b) swap() //交換兩個字符串的內容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //刪除字符
f) clear() //刪除全部字符
g) replace() //替換字符
h) + //串聯字符串
i) ==,!=,<,<=,>,>=,compare() //比較字符串
j) size(),length() //返回字符數量
k) max_size() //返回字符的可能最大個數
l) empty() //判斷字符串是否爲空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量內存以容納一定數量的字符
o) [ ], at() //存取單一字符
p) >>,getline() //從stream讀取某值
q) << //將某值寫入stream
r) copy() //將某值賦值爲一個C_string
s) c_str() //將內容以C_string返回
t) data() //將內容以字符數組形式返回
u) substr() //返回某個子字符串
v)查找函數
w) begin() end() //提供類似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置器
下面詳細介紹:
2.1 C++字符串和C字符串的轉換
C++提供的由C++字符串得到對應的C字符串的方法是使用data()、c_str()和copy(),其中,data()以字符數組的形式返回字符串內容,但並不添加’\0’。c_str()返回一個以‘\0’結尾的字符數組,而copy()則把字符串的內容複製或寫入既有的c_string或字符數組內。C++字符串並不以’\0’結尾。我的建議是在程序中能使用C++字符串就使用,除非萬不得已不選用C字符串。
2.2 字符串大小和容量函數
一個C++字符串存在三種大小:
a)現有的字符數,函數是size()和length(),他們等效。
Empty()用來檢查字符串是否爲空。
b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關係。我們一般情況下不用關心他,應該大小足夠我們用的。但是不夠用的話,會拋出length_error異常。
c)capacity()重新分配內存之前 string所能包含的最大字符數。這裏另一個需要指出的是reserve()函數,這個函數爲string重新分配內存。重新分配的大小由其參數決定, 默認參數爲0,這時候會對string進行非強制性縮減。
2.3 字符串元素存取
我們可以使用下標操作符[]和函數at()對元素包含的字符進行訪問。但是應該注意的是操作符[]並不檢查索引是否有效(有效索引0~str.length ()),如果索引失效,會引起未定義的行爲。而at()會檢查,如果使用 at()的時候索引無效,會拋出out_of_range異常。
2.4 字符串比較函數
C++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C字符串的比較(如 str<”hello”)。在使用>,>=,<,<=這些操作符的時候是根據“當前字符特性”將字符按字典順序進行逐一比較。字典排序靠前的字符小,比較的順序是從前向後比較,遇到不相等的字符就按這個位置上的兩個字符的比較結果確定兩個字符串的大小。
另一個功能強大的比較函數是成員函數compare()。他支持多參數處理,支持用索引值和長度定位子串來進行比較。他返回一個整數來表示比較結果,返回值意義如下:0-相等; >0-大於; <0-小於。舉例如下:
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”比較。
2.5 字符串更改內容
這在字符串的操作中佔了很大一部分。
首先講賦值,第一個賦值方法當然是使用操作符=,新值可以是string(如:s=ns) 、c_string(如:s=”gaint”)甚至單一字符(如:s=’j’)。還可以使用成員函數assign(),這個成員函數可以使你更靈活的對字符串賦值。還是舉例說明吧:
s.assign(str);
s.assign(str,1,3);//如果str是”iamangel” 就是把”ama”賦給字符串。
s.assign(str,2,string::npos);//把字符串str從索引值2開始到結尾賦給s。
s.assign(“gaint”);
s.assign(“nico”,5);//把’n’ ‘I’ ‘c’ ‘o’ ‘\0’賦給字符串。
s.assign(5,’x’);//把五個x賦給字符串。
把字符串清空的方法有三個:
s=””;
s.clear();
s.erase();
string提供了很多函數用於插入(insert)、刪除(erase)、替換(replace)、增加字符。
先說增加字符(這裏說的增加是在尾巴上),函數有 +=、append()、push_back()。舉例如下:
s+=str;//加個字符串
s+=”my name is jiayp”;//加個C字符串。
s+=’a’;//加個字符
s.append(str);
s.append(str,1,3);//同前面的函數參數assign的解釋
s.append(str,2,string::npos)//不解釋了
s.append(“my name is jiayp”);
s.append(“nico”,5);
s.append(5,’x’);
s.push_back(‘a’);//這個函數只能增加單個字符
C++ string資料二
也許你需要在string中間的某個位置插入字符串,這時候你可以用insert()函數,這個函數需要你指定一個安插位置的索引,被插入的字符串將放在這個索引的後面。
s.insert(0,”my name”);
s.insert(1,str);
這種形式的insert()函數不支持傳入單個字符,這時的單個字符必須寫成字符串形式。
爲了插 入單個字符,insert()函數提供了兩個對插入單個字符操作的重載函數:
insert(size_type index,size_type num,chart c)和insert(iterator pos,size_type num,chart c)。
其中size_type是無符號整數,iterator是char*,所以,你這麼調用insert函數是不行的:insert(0,1, ’j’);
這時候第一個參數將轉換成哪一個呢?所以你必須這麼寫:insert((string::size_type)0,1,’j’)!第二種形式指出了使用迭代器安插字符的形式,在後面會提及。順便提一下,string有很多操作是使用STL的迭代器的,他也儘量做得和STL靠近。
刪除函數erase()的形式也有好幾種,替換函數replace()也有好幾個。舉例吧:
string s=”il8n”;
s.replace(1,2, ”nternationalizatio”);//從索引1開始的2個替換成後面的C_string
s.erase(13);//從索引13開始往後全刪除
s.erase(7,5);//從索引7開始往後刪5個
2.6提取子串和字符串連接
提取子串的函數是:substr(),形式如下:
s.substr();//返回s的全部內容
s.substr(11);//從索引11往後的子串
s.substr(5,6);//從索引5開始6個字符
把兩個字符串結合起來的函數是+。
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。
2.7輸入輸出操作
1.>> 從輸入流讀取一個string。
2.<< 把一個string寫入輸出流。
另一個函數就是getline(),他從輸入流讀取一行內容,直到遇到分行符或到了文件尾。
2.8搜索與查找
查找函數很多,功能也很強大,包括了:
find()
rfind()
find_first_of()
find_last_of()
find_first_not_of()
find_last_not_of()
這些函數返回符合搜索條件的字符區間內的第一個字符的索引,沒找到目標就返回npos。
npos的含義,string::npos的類型是string::size_type,所以,一旦需要把一個索引與npos相比,這個索引值必 須是string::size_type類型的,更多的情況下,我們可以直接把函數和npos進行比較(如:if(s.find("jia")== string::npos))。
int find(char c, int pos = 0)//從pos開始查找字符c在當前字符串的位置。
int find(const char *s, int pos = 0) //從pos開始查找字符串s在當前串中的位置。
int find(const char *s, int pos, int n) //從pos開始查找字符串s中前n個字符在當前串中的位置。
int find(const string &s, int pos = 0) //從pos開始查找字符串s在當前串中的位置。查找成功時返回所在位置,失敗返回string::npos的值。
int rfind(char c, int pos = npos) const;//從pos開始從後向前查找字符c在當前串中的位置。
int rfind(const char *s, int pos = npos)
int rfind(const char *s, int pos, int n = npos)
int rfind(const string &s,int pos = npos)
//從pos開始從後向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值。
int find_first_of(char c, int pos = 0) //從pos開始查找字符c第一次出現的位置。
int find_first_of(const char *s, int pos = 0)
int find_first_of(const char *s, int pos, int n)
int find_first_of(const string &s,int pos = 0)
//從pos開始查找當前串中第一個在s的前n個字符組成的數組裏的字符的位置。查找失敗返回string::npos 。
int find_first_not_of(char c, int pos = 0)
int find_first_not_of(const char *s, int pos = 0)
int find_first_not_of(const char *s, int pos,int n)
int find_first_not_of(const string &s,int pos = 0)
//從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos 。
int find_last_of(char c, int pos = npos)
int find_last_of(const char *s, int pos = npos)
int find_last_of(const char *s, int pos, int n = npos)
int find_last_of(const string &s,int pos = npos)
int find_last_not_of(char c, int pos = npos)
int find_last_not_of(const char *s, int pos = npos)
int find_last_not_of(const char *s, int pos, int n)
int find_last_not_of(const string &s,int pos = npos)
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從後向前查找 。
2.9 string的交換:
void swap(string &s2); //交換當前字符串與s2的值
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。