全國(guó)統(tǒng)一學(xué)習(xí)專線 8:30-21:00
位置:魔方網(wǎng) > 外語類 > 公共英語 > c中string的用法  正文

c中string的用法

2023-09-22 15:13:36來源:魔方格

摘要:c中string的用法的用法你知道嗎?下面小編就跟你們?cè)敿?xì)介紹下c中string的用法的用法,希望對(duì)你們有用。c中string的用法的用法如下:為了在我們


(資料圖)

  c中string的用法的用法你知道嗎?下面小編就跟你們?cè)敿?xì)介紹下c中string的用法的用法,希望對(duì)你們有用。

  c中string的用法的用法如下:

  為了在我們的程序中使用string類型,我們必須包含頭文件 <string>。

  如下:

  #include <string> //注意這里不是string.h string.h是C字符串頭文件

  #include <string>

  using namespace std;

  1.聲明一個(gè)C++字符串

  聲明一個(gè)字符串變量很簡(jiǎn)單:

  string Str;

  這樣我們就聲明了一個(gè)字符串變量,但既然是一個(gè)類,就有構(gòu)造函數(shù)和析構(gòu)函數(shù)。上面的聲明沒有傳入?yún)?shù),所以就直接使用了string的默認(rèn)的構(gòu)造函數(shù),這個(gè)函數(shù)所作的就是把Str初始化為一個(gè)空字符串。String類的構(gòu)造函數(shù)和析構(gòu)函數(shù)如下:

  a) string s; //生成一個(gè)空字符串s

  b) string s(str) //拷貝構(gòu)造函數(shù) 生成str的復(fù)制品

  c) string s(str,stridx) //將字符串str內(nèi)“始于位置stridx”的部分當(dāng)作字符串的初值

  d) string s(str,stridx,strlen) //將字符串str內(nèi)“始于stridx且長(zhǎng)度頂多strlen”的部分作為字符串的初值

  e) string s(cstr) //將C字符串作為s的初值

  f) string s(chars,chars_len) //將C字符串前chars_len個(gè)字符作為字符串s的初值。

  g) string s(num,c) //生成一個(gè)字符串,包含num個(gè)c字符

  h) string s(beg,end) //以區(qū)間beg;end(不包含end)內(nèi)的字符作為字符串s的初值

  i) s.~string() //銷毀所有字符,釋放內(nèi)存

  都很簡(jiǎn)單,我就不解釋了。

  2.字符串操作函數(shù)

  這里是C++字符串的重點(diǎn),我先把各種操作函數(shù)羅列出來,不喜歡把所有函數(shù)都看完的人可以在這里找自己喜歡的函數(shù),再到后面看他的詳細(xì)解釋。

  a) =,assign() //賦以新值

  b) swap() //交換兩個(gè)字符串的內(nèi)容

  c) +=,append(),push_back() //在尾部添加字符

  d) insert() //插入字符

  e) erase() //刪除字符

  f) clear() //刪除全部字符

  g) replace() //替換字符

  h) + //串聯(lián)字符串

  i) ==,!=,<,<=,>,>=,compare() //比較字符串

  j) size(),length() //返回字符數(shù)量

  k) max_size() //返回字符的可能最大個(gè)數(shù)

  l) empty() //判斷字符串是否為空

  m) capacity() //返回重新分配之前的字符容量

  n) reserve() //保留一定量?jī)?nèi)存以容納一定數(shù)量的字符

  o) [ ], at() //存取單一字符

  p) >>,getline() //從stream讀取某值

  q) << //將謀值寫入stream

  r) copy() //將某值賦值為一個(gè)C_string

  s) c_str() //將內(nèi)容以C_string返回

  t) data() //將內(nèi)容以字符數(shù)組形式返回

  u) substr() //返回某個(gè)子字符串

  v)查找函數(shù)

  w)begin() end() //提供類似STL的迭代器支持

  x) rbegin() rend() //逆向迭代器

  y) get_allocator() //返回配置器

  下面詳細(xì)介紹:

  2.1 C++字符串和C字符串的轉(zhuǎn)換

  C ++提供的由C++字符串得到對(duì)應(yīng)的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符數(shù)組的形式返回字符串內(nèi)容,但并不添加'/0'。c_str()返回一個(gè)以‘/0'結(jié)尾的字符數(shù)組,而copy()則把字符串的內(nèi)容復(fù)制或?qū)懭爰扔械腸_string或 字符數(shù)組內(nèi)。C++字符串并不以'/0'結(jié)尾。我的建議是在程序中能使用C++字符串就使用,除非萬不得已不選用c_string。由于只是簡(jiǎn)單介紹,詳細(xì)介紹掠過,誰想進(jìn)一步了解使用中的注意事項(xiàng)可以給我留言(到我的收件箱)。我詳細(xì)解釋。

  2.2 大小和容量函數(shù)

  一個(gè)C++字符串存在三種大?。篴)現(xiàn)有的字符數(shù),函數(shù)是size()和length(),他們等效。Empty()用來檢查字符串是否為空。b)max_size() 這個(gè)大小是指當(dāng)前C++字符串最多能包含的字符數(shù),很可能和機(jī)器本身的限制或者字符串所在位置連續(xù)內(nèi)存的大小有關(guān)系。我們一般情況下不用關(guān)心他,應(yīng)該大小足夠我們用的。但是不夠用的話,會(huì)拋出length_error異常c)capacity()重新分配內(nèi)存之前 string所能包含的最大字符數(shù)。這里另一個(gè)需要指出的是reserve()函數(shù),這個(gè)函數(shù)為string重新分配內(nèi)存。重新分配的大小由其參數(shù)決定, 默認(rèn)參數(shù)為0,這時(shí)候會(huì)對(duì)string進(jìn)行非強(qiáng)制性縮減。

  還有必要再重復(fù)一下C++字符串和C字符串轉(zhuǎn)換的問 題,許多人會(huì)遇到這樣的問題,自己做的程序要調(diào)用別人的函數(shù)、類什么的(比如數(shù)據(jù)庫(kù)連接函數(shù)Connect(char*,char*)),但別人的函數(shù)參 數(shù)用的是char*形式的,而我們知道,c_str()、data()返回的字符數(shù)組由該字符串擁有,所以是一種const char*,要想作為上面提及的函數(shù)的參數(shù),還必須拷貝到一個(gè)char*,而我們的原則是能不使用C字符串就不使用。那么,這時(shí)候我們的處理方式是:如果 此函數(shù)對(duì)參數(shù)(也就是char*)的內(nèi)容不修改的話,我們可以這樣Connect((char*)UserID.c_str(), (char*)PassWD.c_str()),但是這時(shí)候是存在危險(xiǎn)的,因?yàn)檫@樣轉(zhuǎn)換后的字符串其實(shí)是可以修改的(有興趣地可以自己試一試),所以我強(qiáng)調(diào)除非函數(shù)調(diào)用的時(shí)候不對(duì)參數(shù)進(jìn)行修改,否則必須拷貝到一個(gè)char*上去。當(dāng)然,更穩(wěn)妥的辦法是無論什么情況都拷貝到一個(gè)char*上去。同時(shí)我們也祈禱現(xiàn)在仍然使用C字符串進(jìn)行編程的高手們(說他們是高手一點(diǎn)兒也不為過,也許在我們還穿開襠褲的時(shí)候他們就開始編程了,哈哈…)寫的函數(shù)都比較規(guī)范,那樣我們就不必進(jìn)行強(qiáng)制轉(zhuǎn)換了。

  2.3元素存取

  我們可以使用下標(biāo)操作符[]和函數(shù)at()對(duì)元素包含的字符進(jìn)行訪問。但是應(yīng)該注意的是操作符[]并不檢查索引是否有效(有效索引0~str.length()),如果索引失效,會(huì)引起未定義的行為。而at()會(huì)檢查,如果使用 at()的時(shí)候索引無效,會(huì)拋出out_of_range異常。

  有一個(gè)例外不得不說,const string a;的操作符[]對(duì)索引值是a.length()仍然有效,其返回值是'/0'。其他的各種情況,a.length()索引都是無效的。舉例如下:

  const string Cstr(“const string”);

  string Str(“string”);

  Str[3]; //ok

  Str.at(3); //ok

  Str[100]; //未定義的行為

  Str.at(100); //throw out_of_range

  Str[Str.length()] //未定義行為

  Cstr[Cstr.length()] //返回 ‘/0'

  Str.at(Str.length());//throw out_of_range

  Cstr.at(Cstr.length()) ////throw out_of_range

  我不贊成類似于下面的引用或指針賦值:

  char& r=s[2];

  char* p= &s[3];

  因?yàn)橐坏┌l(fā)生重新分配,r,p立即失效。避免的方法就是不使用。

  2.4比較函數(shù)

  C ++字符串支持常見的比較操作符(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”)。在使用>,>=,<,<=這些操作符的時(shí)候是根據(jù)“當(dāng)前字符特性”將字符按字典順序進(jìn)行逐一得 比較。字典排序靠前的字符小,比較的順序是從前向后比較,遇到不相等的字符就按這個(gè)位置上的兩個(gè)字符的比較結(jié)果確定兩個(gè)字符串的大小。同時(shí),string (“aaaa”) <string(aaaaa)。

  另一個(gè)功能強(qiáng)大的比較函數(shù)是成員函數(shù)compare()。他支持多參數(shù)處理,支持用索引值和長(zhǎng)度定位子串來進(jìn)行比較。他返回一個(gè)整數(shù)來表示比較結(jié)果,返回值意義如下:0-相等 〉0-大于 <0-小于。舉例如下:

  string s(“abcd”);

  s.compare(“abcd”); //返回0

  s.compare(“dcba”); //返回一個(gè)小于0的值

  s.compare(“ab”); //返回大于0的值

  s.compare(s); //相等

  s.compare(0,2,s,2,2); //用”ab”和”cd”進(jìn)行比較 小于零

  s.compare(1,2,”bcx”,2); //用”bc”和”bc”比較。

  怎么樣?功能夠全的吧!什么?還不能滿足你的胃口?好吧,那等著,后面有更個(gè)性化的比較算法。先給個(gè)提示,使用的是STL的比較算法。什么?對(duì)STL一竅不通?靠,你重修吧!

  2.5 更改內(nèi)容

  這在字符串的操作中占了很大一部分。

  首先講賦值,第一個(gè)賦值方法當(dāng)然是使用操作符=,新值可以是string(如:s=ns) 、c_string(如:s=”gaint”)甚至單一字符(如:s='j')。還可以使用成員函數(shù)assign(),這個(gè)成員函數(shù)可以使你更靈活的對(duì)字符串賦值。還是舉例說明吧:

  s.assign(str); //不說

  s.assign(str,1,3);//如果str是”iamangel” 就是把”ama”賦給字符串

  s.assign(str,2,string::npos);//把字符串str從索引值2開始到結(jié)尾賦給s

  s.assign(“gaint”); //不說

  s.assign(“nico”,5);//把'n' ‘I' ‘c' ‘o' ‘/0'賦給字符串

  s.assign(5,'x');//把五個(gè)x賦給字符串

  把字符串清空的方法有三個(gè):s=””;s.clear();s.erase();(我越來越覺得舉例比說話讓別人容易懂!)。

  string提供了很多函數(shù)用于插入(insert)、刪除(erase)、替換(replace)、增加字符。

  先說增加字符(這里說的增加是在尾巴上),函數(shù)有 +=、append()、push_back()。

  舉例如下:

  s+=str;//加個(gè)字符串

  s+=”my name is jiayp”;//加個(gè)C字符串

  s+='a';//加個(gè)字符

  s.append(str);

  s.append(str,1,3);//不解釋了 同前面的函數(shù)參數(shù)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');//這個(gè)函數(shù)只能增加單個(gè)字符對(duì)STL熟悉的理解起來很簡(jiǎn)單

  也許你需要在string中間的某個(gè)位置插入字符串,這時(shí)候你可以用insert()函數(shù),這個(gè)函數(shù)需要你指定一個(gè)安插位置的索引,被插入的字符串將放在這個(gè)索引的后面。

  s.insert(0,”my name”);

  s.insert(1,str);

  這種形式的insert()函數(shù)不支持傳入單個(gè)字符,這時(shí)的單個(gè)字符必須寫成字符串形式(讓人惡心)。既然你覺得惡心,那就不得不繼續(xù)讀下面一段話:為了插 入單個(gè)字符,insert()函數(shù)提供了兩個(gè)對(duì)插入單個(gè)字符操作的重載函數(shù):insert(size_type index,size_type num,chart c)和insert(iterator pos,size_type num,chart c)。其中size_type是無符號(hào)整數(shù),iterator是char*,所以,你這么調(diào)用insert函數(shù)是不行的:insert(0,1, 'j');這時(shí)候第一個(gè)參數(shù)將轉(zhuǎn)換成哪一個(gè)呢?所以你必須這么寫:insert((string::size_type)0,1,'j')!第二種形式指 出了使用迭代器安插字符的形式,在后面會(huì)提及。順便提一下,string有很多操作是使用STL的迭代器的,他也盡量做得和STL靠近。

  刪除函數(shù)erase()的形式也有好幾種(真煩!),替換函數(shù)replace()也有好幾個(gè)。

  舉例吧:

  string s=”il8n”;

  s.replace(1,2,”nternationalizatio”);//從索引1開始的2個(gè)替換成后面的C_string

  s.erase(13);//從索引13開始往后全刪除

  s.erase(7,5);//從索引7開始往后刪5個(gè)

  2.6提取子串和字符串連接

  題取子串的函數(shù)是:substr(),形式如下:

  s.substr();//返回s的全部?jī)?nèi)容

  s.substr(11);//從索引11往后的子串

  s.substr(5,6);//從索引5開始6個(gè)字符

  把兩個(gè)字符串結(jié)合起來的函數(shù)是+。(誰不明白請(qǐng)致電120)

  2.7輸入輸出操作

  1.>> 從輸入流讀取一個(gè)string。

  2.<< 把一個(gè)string寫入輸出流。

  另一個(gè)函數(shù)就是getline(),他從輸入流讀取一行內(nèi)容,直到遇到分行符或到了文件尾。

  2.8搜索與查找

  查找函數(shù)很多,功能也很強(qiáng)大,包括了:

  find()

  rfind()

  find_first_of()

  find_last_of()

  find_first_not_of()

  find_last_not_of()

  這些函數(shù)返回符合搜索條件的字符區(qū)間內(nèi)的第一個(gè)字符的索引,沒找到目標(biāo)就返回npos。所有的函數(shù)的參數(shù)說明如下:

  第一個(gè)參數(shù)是被搜尋的對(duì)象。第二個(gè)參數(shù)(可有可無)指出string內(nèi)的搜尋起點(diǎn)索引,第三個(gè)參數(shù)(可有可無)指出搜尋的字符個(gè)數(shù)。比較簡(jiǎn)單,不多說不理解的可以向我提出,我再仔細(xì)的解答。當(dāng)然,更加強(qiáng)大的STL搜尋在后面會(huì)有提及。

  最后再說說npos的含義,string::npos的類型是string::size_type,所以,一旦需要把一個(gè)索引與npos相比,這個(gè)索引值必須是string::size)type類型的,更多的情況下,我們可以直接把函數(shù)和npos進(jìn)行比較(如:if(s.find(“jia”)== string::npos))。

  string類的構(gòu)造函數(shù):

  string(const char *s); //用c字符串s初始化

  string(int n,char c); //用n個(gè)字符c初始化

  此外,string類還支持默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù),如string s1;string s2="hello";都是正確的寫法。當(dāng)構(gòu)造的string太長(zhǎng)而無法表達(dá)時(shí)會(huì)拋出length_error異常

  string類的字符操作:

  const char &operator[](int n)const;

  const char &at(int n)const;

  char &operator[](int n);

  char &at(int n);

  operator[]和at()均返回當(dāng)前字符串中第n個(gè)字符的位置,但at函數(shù)提供范圍檢查,當(dāng)越界時(shí)會(huì)拋出out_of_range異常,下標(biāo)運(yùn)算符[]不提供檢查訪問。

  const char *data()const;//返回一個(gè)非null終止的c字符數(shù)組

  const char *c_str()const;//返回一個(gè)以null終止的c字符串

  int copy(char *s, int n, int pos = 0) const;//把當(dāng)前串中以pos開始的n個(gè)字符拷貝到以s為起始位置的字符數(shù)組中,返回實(shí)際拷貝的數(shù)目

  string的特性描述:

  int capacity()const; //返回當(dāng)前容量(即string中不必增加內(nèi)存即可存放的元素個(gè)數(shù))

  int max_size()const; //返回string對(duì)象中可存放的最大字符串的長(zhǎng)度

  int size()const; //返回當(dāng)前字符串的大小

  int length()const; //返回當(dāng)前字符串的長(zhǎng)度

  bool empty()const; //當(dāng)前字符串是否為空

  void resize(int len,char c);//把字符串當(dāng)前大小置為len,并用字符c填充不足的部分

  string類的輸入輸出操作:

  string類重載運(yùn)算符operator>>用于輸入,同樣重載運(yùn)算符operator<<用于輸出操作。

  函數(shù)getline(istream &in,string &s);用于從輸入流in中讀取字符串到s中,以換行符'\n'分開。

  string的賦值:

  string &operator=(const string &s);//把字符串s賦給當(dāng)前字符串

  string &assign(const char *s);//用c類型字符串s賦值

  string &assign(const char *s,int n);//用c字符串s開始的n個(gè)字符賦值

  string &assign(const string &s);//把字符串s賦給當(dāng)前字符串

  string &assign(int n,char c);//用n個(gè)字符c賦值給當(dāng)前字符串

  string &assign(const string &s,int start,int n);//把字符串s中從start開始的n個(gè)字符賦給當(dāng)前字符串

  string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字符串

  string的連接:

  string &operator+=(const string &s);//把字符串s連接到當(dāng)前字符串的結(jié)尾

  string &append(const char *s); //把c類型字符串s連接到當(dāng)前字符串結(jié)尾

  string &append(const char *s,int n);//把c類型字符串s的前n個(gè)字符連接到當(dāng)前字符串結(jié)尾

  string &append(const string &s); //同operator+=()

  string &append(const string &s,int pos,int n);//把字符串s中從pos開始的n個(gè)字符連接到當(dāng)前字符串的結(jié)尾

  string &append(int n,char c); //在當(dāng)前字符串結(jié)尾添加n個(gè)字符c

  string &append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當(dāng)前字符串的結(jié)尾

  string的比較:

  bool operator==(const string &s1,const string &s2)const;//比較兩個(gè)字符串是否相等

  運(yùn)算符">","<",">=","<=","!="均被重載用于字符串的比較;

  int compare(const string &s) const;//比較當(dāng)前字符串和s的大小

  int compare(int pos, int n,const string &s)const;//比較當(dāng)前字符串從pos開始的n個(gè)字符組成的字符串與s的大小

  int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當(dāng)前字符串從pos開始的n個(gè)字符組成的字符串與s中pos2開始的n2個(gè)字符組成的字符串的大小

  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ù)在>時(shí)返回1,<時(shí)返回-1,==時(shí)返回0

  string的子串:

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

  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個(gè)字符在當(dāng)前串中的位置

  int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當(dāng)前串中的位置

  //查找成功時(shí)返回所在位置,失敗返回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個(gè)字符組成的字符串在當(dāng)前串中的位置,成功返回所在位置,失敗時(shí)返回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)前串中第一個(gè)在s的前n個(gè)字符組成的數(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)前串中查找第一個(gè)不在串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 = 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相似,只不過是從后向前查找

  string類的替換函數(shù):

  string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個(gè)字符,然后在p0處插入串s

  string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個(gè)字符,然后在p0處插入字符串s的前n個(gè)字符

  string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個(gè)字符,然后在p0處插入串s

  string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個(gè)字符,然后在p0處插入串s中從pos開始的n個(gè)字符

  string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個(gè)字符,然后在p0處插入n個(gè)字符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個(gè)字符

  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個(gè)字符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);

  //前4個(gè)函數(shù)在p0位置插入字符串s中pos開始的前n個(gè)字符

  string &insert(int p0, int n, char c);//此函數(shù)在p0處插入n個(gè)字符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個(gè)字符c

  string類的刪除函數(shù)

  iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除后迭代器的位置

  iterator erase(iterator it);//刪除it指向的字符,返回刪除后迭代器的位置

  string &erase(int pos = 0, int n = npos);//刪除pos開始的n個(gè)字符,返回修改后的字符串

  string類的迭代器處理:

  string類提供了向前和向后遍歷的迭代器iterator,迭代器提供了訪問各個(gè)字符的語法,類似于指針操作,迭代器不檢查范圍。

  用string::iterator或string::const_iterator聲明迭代器變量,const_iterator不允許改變迭代的內(nèi)容。常用迭代器函數(shù)有:

  const_iterator begin()const;

  iterator begin(); //返回string的起始位置

  const_iterator end()const;

  iterator end(); //返回string的最后一個(gè)字符后面的位置

  const_iterator rbegin()const;

  iterator rbegin(); //返回string的最后一個(gè)字符的位置

  const_iterator rend()const;

  iterator rend(); //返回string第一個(gè)字符位置的前面

  rbegin和rend用于從后向前的迭代訪問,通過設(shè)置迭代器string::reverse_iterator,string::const_reverse_iterator實(shí)現(xiàn)

  字符串流處理:

  通過定義ostringstream和istringstream變量實(shí)現(xiàn),<sstream>頭文件中

  例如:

  string input("hello,this is a test");

  istringstream is(input);

  string s1,s2,s3,s4;

  is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"

  ostringstream os;

  os<<s1<<s2<<s3<<s4;

  cout<<os.str();

同類文章