string類------新標準c++程式設計

來源:互聯網
上載者:User

標籤:演算法   檔案   iterator   字母   不同的   ret   需要   avatar   mes   

定義:

  string類是STL中basic_string模板執行個體化得到的模板類。其定義如下:

typedef basic_string<char>string;

建構函式: 

  string類有多個建構函式,但沒有接收一個整型參數或一個字元型參數的建構函式

string s1();                                   //s1=""string s2("hello");                            //s2="hello"string s3(4,‘k‘);                              //s3="kkkk"string s4("12345",1,3);                       //s4="234"  即“12345”的從下標1開始,長度為3的子串

string s5(‘k‘); //非法錯的
string s6(123); //非法錯的

賦值函數:

  可以用char*類型的變數、常量,以及char類型的變數、常量對string對象進行賦值。例如:

string s1;s1="hello";         //s1="hello"s2=‘k‘;               //s2="k"

  string類還有assign成員函數,可以用來對string對象賦值。assign成員函數返回對象自身的引用。例如:

string s1("12345"),s2;s3.assign(s1);                       //s3=s1s2.assign(s1,1,2);                   //s2="23",即s1的子串(1,2)s2.assign(4,‘k‘);                   //s2="kkkk"s2.assign("abcde",2,3);             //s2="cde",即“abcde”的子串(2,3)s2.assign("12345").assign("678");   //返回對自身的引用,可以繼續在後面調用assign成員函數 s2="678"                                         

 求字串長度:

  length成員函數返回字串的長度。size成員函數可以實現同樣的功能。

string對象中字串的串連:

  可以使用“+”和“+=”運算子對string對象執行字串的串連操作,string類還有append成員函數,也可以用來向字串後面新增內容。append成員函數返回對象自身的引用。例如:

string s1("123"),s2("abc");s1.append(s2);                            //s1="123abc"s1.append(s2,1,2);                       //s1="123abcbc"s1.append(3,‘k‘);                        //s1="123abcbckkk"s1.append("ABCDE",2,3);                 //s1="123abcbckkkCDE",添加“ABCDE“子串(2,3)

string對象的比較:

  可以用“<”、"<="、"=="、、“!=”、">="、">"運算子比較string對象。string類還有compare成員函數,可以用來比較字串.compare成員函數有以下傳回值:小於0表示當前的字串小;等於0表示兩個字串相等;大於0表示另一個字串小。例如:

string s1("hello"),s2("hello,world");int n=s1.compare(s2);n=s1.compare(1,2,s2,0,3);               //比較s1的子串(1,2)和s2的子串(0,3)n=s1.compare(0,2,s2);                   //比較s1的子串(0,2)和s2n=s1.compare("Hello"); n=s1.compare(1,2,"Hello");              //比較s1的子串(1,2)和"Hello"n=s1.compare(1,2,"Hello",1,2);          //比較s1的子串(1,2)和“Hello”的子串(1,2)

求string對象的子串:

  substr成員函數可以用來求子串(n,m)原型如下:

string substr(int n=0,int m=string::npos)const

  調用時,如果省略m或m超過了字串的長度,則求出來的子串就是從下標n開始一直到字串結束的部分。例如:

string s1="this is ok";string s2=s1.substr(2,4);                   //s2="is i"s2=s1.substr(2);                            //s2="is is ok"

交換兩個string對象的內容:

  swap成員函數可以交換兩個dtring對象的內容。例如:

string s1("west"),s2("east");s1.swap(s2);                                 //s1="east"  s2="west"

尋找子串和字元:

  string類有6種尋找子串和字元的成員函數,每種函數以不同形式的 find 命名。它們的傳回值都是子串或字元在string對象字串中的位置(即下標   string::size_type 類型的值)。如果查不到,則返回string::npos。string::npos是在string類中定義的一個靜態常量。string::nops  原意  no position(無位置),是 unsigned int的最大值 UINT_MAX=4294967295 ,即尋找到最後位置也沒有匹配。在int 下數值剛好為-1, 也可以理解為 返回不存在的下標。

  這些函數如下:

  find:從前往後尋找子串或字元出現的位置。

  rfind:從後往前尋找子串或字元出現的位置。

  find_first_of:從前往後尋找何處出現另一個字串中包含的字元。例如:

s1.find_first_of("abc");   //尋找s1zhong第一次出現“abc”中任一字元的位置

  find_last_of:從後往前尋找何處出現另一個字串中包含的字元。

  find_first_not_of:從前往後尋找何處出現另一個字串中沒有包含的字元。

  find_last_not_of:從後往前尋找何處出現另一個字串中沒有包含的字元。

  下面是string類的尋找成員函數的樣本程式:

#include<iostream>
#include<string>
using namespace std;
int main(){
  string s1("Source Code");
  int n;
  if((n=s1.find(‘u‘))!=string::npos)                                //尋找u出現的位置
  cout<<"1)"<<n<<","<<s1.substr(n)<<endl;              //輸出 1)2,urce Code

  if(n=s1.find("Source",3)==string::npos)                   //從下表3開始尋找"Source",找不到
  cout<<"2)"<<"Not Found"<<endl;                           //輸出 2)Not Found

  if((n=s1.find("Co"))!=string::npos)                           //尋找子串"Co"。能找到,返回"Co"的位置
  cout<<"3)"<<n<<","<<s1.substr(n)<<endl;             //輸出 3)7,Code

  if((n=s1.find_first_of("ceo"))!=string::npos)            //尋找第一次出現"c"、‘e‘或‘0‘的位置
  cout<<"4)"<<n<<","<<s1.substr(n)<<endl;          //輸出 4)1,ource Code

  if((n=s1.find_last_of(‘e‘))!=string::npos)                 //尋找最後一個‘e‘的位置
  cout<<"5)"<<n<<","<<s1.substr(n)<<endl;            //輸出 5)10,e

  if((n=s1.find_first_not_of("eou",1))!=string::npos) //從下標1開始尋找第一次出現非‘e‘,‘o‘或‘u’字元的位置
  cout<<"6)"<<n<<","<<s1.substr(n)<<endl;            //輸出 6)3,rec Code

  return 0;
}

替換子串:

  replace成員函數可以對string對象中的子串進行替換,傳回值為對象自身的引用。例如:

string s1("Real Steel");s1.replace(1,3,"123456",2,4);        //用“123456”的子串(2,4)替換s1的子串(1,3)cout<<s1<<endl;                      //輸出  R3456 Steel string s2("Harry Potter");s2.replace(2,3,5,‘0‘);               //用5個‘0‘替換子串(2,3)cout<<s2<<endl;                      //輸出  Ha00000 Potterint n=s2.find("00000");              //尋找子串“00000”的位置,n=2s2.replace(n,5,"XXX");               //將子串(n,5)替換為“XXX”cout<<s2<<endl;                      //輸出 HaXXX Potter

刪除子串:

  erase成員函數可以刪除string對象中的子串,傳回值為對象自身的引用。例如:

string s1("Real Steel");s1.erase(1,3);                      //刪除子串(1,3),此後s1=“R Steel”s1.erase(5);                        //刪除下標5及其後面的所有字元。此後s1=“R Ste”

插入字串:

  insert成員函數可以在string對象中插入另一個字串,傳回值為對象自身的引用。例如:

string s1("Limitless"),s2("00");s1.insert(2,"123");                  //在下標2處插入字串“123”,s1="Lil123mitless“s1.insert(3,s2);                     //在下標2處插入s2,s1="Lil0023mitless"s1.insert(3,5,‘x‘);                   //在下標3處插入5個‘x‘,s1=”Li1xxxxx0023mitless”

將string對象作為流處理:

  使用流對象istringstream和ostringstream,可以將string對象當作一個流進行輸入輸出。這兩個類需要包含標頭檔sstream。樣本程式如下:

#include <iostream>#include <sstream>#include <string>using namespace std;int main(){  string src("Avatar 123 5.2 Titanic K");  istringstream istrStream(src);                   //建立src到istrStream的聯絡  string s1, s2;  int n;  double d;  char c;  istrStream >> s1 >> n >> d >> s2 >> c;           //把src的內容當做輸入資料流進行讀取  ostringstream ostrStream;  ostrStream << s1 << endl << s2 << endl << n << endl << d << endl << c <<endl;  cout << ostrStream.str();  return 0;}

  該程式的輸出結果是:

AvatarTitanic1235.2K

  第十一行,從輸入資料流istrStream進行讀取,過程和從cin讀取一樣。

  第十二行,將變數的值輸出到流ostrStream。輸出結果並不會出現在螢幕上,而是被儲存在ostrStream對象管理的某處。用ostringstream類的str成員函數能將輸出到ostringstream對象中的內容提取出來。

用STL演算法操作string對象:

  string對象也可以看作一個順序容器,它支援隨機訪問迭代器,也有begin和end等成員函數。STL中的許多演算法也適用於string對象。下面是STl演算法操作string對象的程式樣本:

#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){    string s("afgcbed");    string::iterator p=find(s.begin(),s.end(),‘c‘);    if(p!=s.end())        cout<<p-s.begin()<<endl;                  //輸出3    sort(s.begin(),s.end());    cout<<s<<endl;                                //輸出abcdefg    next_permutation(s.begin(),s.end());    cout<<s<<endl;                                //輸出abcdegf    return 0;}

注意:

  1、string與字元數組不同的是,一個string對象的大小是固定的,即運算式sizeof(string)的值是固定的,和其中存放的字串的長度無關。但這個固定的值在不同編譯器中不相同。例如,在Dev C++中是4,在vs2010中是32,string對象中並不直接存放字串,字串會在別處開闢記憶體空間存放,string對象值存放該空間的地址,或者再加上其他一些資訊。

  2、用一般字元對string對象賦值,一般字元串的內容會被複製到string對象所管理的那片記憶體空間中。

  3、string對象在比較大小時是按詞典序比較的,而且是大小寫相關的。由於大寫字母的ASCII碼小於小寫字母的ASCII碼(‘A’~‘Z’的ASCII碼是0x41~0x5a,‘a’~‘z’的ASCII碼是0x61~0x7a),所以Zbc比abc小。

 

轉載請註明出處:http://www.cnblogs.com/goudanli/p/7664213.html

新標準c++程式設計

string類------新標準c++程式設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.