• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            T9的空間

            You will never walk alone!

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks
              1#include<iostream>
              2#include<string>
              3#include<algorithm>
              4using namespace std;
              5int main()
              6{
              7    string str1,str2;
              8    //string類的構(gòu)造函數(shù):
              9    //string(const char *s);    //用c字符串s初始化
             10    //string(int n,char c);     //用n個字符c初始化
             11    //此外,string類還支持默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù),如string s1;string s2="hello";都是正確的寫法。當(dāng)構(gòu)造的string太長而無法表達(dá)時會拋出length_error異常 
             12
             13
             14
             15    //string的賦值:
             16    //string &operator=(const string &s);//把字符串s賦給當(dāng)前字符串
             17    //string &assign(const char *s);//用c類型字符串s賦值
             18    //string &assign(const char *s,int n);//用c字符串s開始的n個字符賦值
             19    //string &assign(const string &s);//把字符串s賦給當(dāng)前字符串
             20    //string &assign(int n,char c);//用n個字符c賦值給當(dāng)前字符串
             21    //string &assign(const string &s,int start,int n);//把字符串s中從start開始的n個字符賦給當(dāng)前字符串
             22    //string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字符串
             23
             24    str1.assign("hell0");
             25    cout<<str1<<endl;
             26    cin>>str2;
             27    str1.assign(str2);
             28    cout<<str1<<endl;
             29
             30    string str3="word";
             31    str1.assign(str3,1,2);
             32    cout<<str1<<endl;
             33
             34    str1.assign(str3.begin()+1,str3.end());
             35    cout<<str1<<endl;
             36    //string的特性描述:
             37    //int capacity()const;    //返回當(dāng)前容量(即string中不必增加內(nèi)存即可存放的元素個數(shù))
             38    //int max_size()const;    //返回string對象中可存放的最大字符串的長度
             39    //int size()const;        //返回當(dāng)前字符串的大小
             40    //int length()const;       //返回當(dāng)前字符串的長度
             41    //bool empty()const;        //當(dāng)前字符串是否為空
             42    //void resize(int len,char c);//把字符串當(dāng)前大小置為len,并用字符c填充不足的部分 
             43    cout<<str2.size ()<<" "<<str2.length()<<" "<<str2.capacity()<<endl;
             44    cout<<str2.empty()<<endl;
             45    cout<<str2.max_size()<<endl;
             46    //string類的輸入輸出操作:
             47    //string類重載運(yùn)算符operator>>用于輸入,同樣重載運(yùn)算符operator<<用于輸出操作。
             48    //函數(shù)getline(istream &in,string &s);用于從輸入流in中讀取字符串到s中,以換行符'\n'分開。
             49    //實際上getline可以指定結(jié)束標(biāo)志
             50    getchar();
             51    
             52    string str4;
             53    getline(cin,str4,'\n');
             54    cout<<"str4:"<<str4<<endl;
             55    getchar();
             56
             57    getline(cin,str4,'a');
             58    cout<<"getline2: "<<str4<<endl;
             59
             60    //string的連接:
             61    //string &operator+=(const string &s);//把字符串s連接到當(dāng)前字符串的結(jié)尾 
             62    //string &append(const char *s);            //把c類型字符串s連接到當(dāng)前字符串結(jié)尾
             63    //string &append(const char *s,int n);//把c類型字符串s的前n個字符連接到當(dāng)前字符串結(jié)尾
             64    //string &append(const string &s);    //同operator+=()
             65    //string &append(const string &s,int pos,int n);//把字符串s中從pos開始的n個字符連接到當(dāng)前字符串的結(jié)尾
             66    //string &append(int n,char c);        //在當(dāng)前字符串結(jié)尾添加n個字符c
             67    //string &append(const_iterator first,const_iterator last);//把迭代器first和last之間的部分連接到當(dāng)前字符串的結(jié)尾 
             68
             69    cout<<"str2:"<<str2<<endl;
             70    str2.append(str4,0,str4.length());
             71    cout<<"str2: "<<str2<<endl;
             72    str2.append(3,'!');
             73    cout<<"str2: "<<str2<<endl;
             74    str2+=" "+str3;
             75    cout<<"str2: "<<str2<<endl;
             76
             77    //string的比較:
             78    //bool operator==(const string &s1,const string &s2)const;//比較兩個字符串是否相等
             79    //運(yùn)算符">","<",">=","<=","!="均被重載用于字符串的比較;
             80    //int compare(const string &s) const;//比較當(dāng)前字符串和s的大小
             81    //int compare(int pos, int n,const string &s)const;//比較當(dāng)前字符串從pos開始的n個字符組成的字符串與s的大小
             82    //int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當(dāng)前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小
             83    //int compare(const char *s) const;
             84    //int compare(int pos, int n,const char *s) const;
             85    //int compare(int pos, int n,const char *s, int pos2) const;
             86    //compare函數(shù)在>時返回1,<時返回-1,==時返回0 
             87    
             88    //string的子串:
             89    //string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串
             90
             91    str4=str2.substr(1,5);
             92    cout<<"str4: "<<str4<<endl;
             93/*  
             94    string類的查找函數(shù): 
             95
             96    int find(char c, int pos = 0) const;//從pos開始查找字符c在當(dāng)前字符串的位置
             97    int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當(dāng)前串中的位置
             98    int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當(dāng)前串中的位置
             99    int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當(dāng)前串中的位置
            100    //查找成功時返回所在位置,失敗返回string::npos的值 
            101
            102    int rfind(char c, int pos = npos) const;//從pos開始從后向前查找字符c在當(dāng)前串中的位置
            103    int rfind(const char *s, int pos = npos) const;
            104    int rfind(const char *s, int pos, int n = npos) const;
            105    int rfind(const string &s,int pos = npos) const;
            106    //從pos開始從后向前查找字符串s中前n個字符組成的字符串在當(dāng)前串中的位置,成功返回所在位置,失敗時返回string::npos的值 
            107
            108    int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現(xiàn)的位置
            109    int find_first_of(const char *s, int pos = 0) const;
            110    int find_first_of(const char *s, int pos, int n) const;
            111    int find_first_of(const string &s,int pos = 0) const;
            112    //從pos開始查找當(dāng)前串中第一個在s的前n個字符組成的數(shù)組里的字符的位置。查找失敗返回string::npos 
            113
            114    int find_first_not_of(char c, int pos = 0) const;
            115    int find_first_not_of(const char *s, int pos = 0) const;
            116    int find_first_not_of(const char *s, int pos,int n) const;
            117    int find_first_not_of(const string &s,int pos = 0) const;
            118    //從當(dāng)前串中查找第一個不在串s中的字符出現(xiàn)的位置,失敗返回string::npos 
            119
            120    int find_last_of(char c, int pos = npos) const;
            121    int find_last_of(const char *s, int pos = npos) const;
            122    int find_last_of(const char *s, int pos, int n = npos) const;
            123    int find_last_of(const string &s,int pos = npos) const; 
            124
            125    int find_last_not_of(char c, int pos = npos) const;
            126    int find_last_not_of(const char *s, int pos = npos) const;
            127    int find_last_not_of(const char *s, int pos,  int n) const;
            128    int find_last_not_of(const string &s,int pos = npos) const;
            129    //find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從后向前查找 
            130
            131  
            132
            133    string類的替換函數(shù): 
            134
            135    string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字符,然后在p0處插入串s
            136    string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個字符,然后在p0處插入字符串s的前n個字符
            137    string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字符,然后在p0處插入串s
            138    string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個字符,然后在p0處插入串s中從pos開始的n個字符
            139    string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個字符,然后在p0處插入n個字符c
            140    string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換為字符串s
            141    string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換為s的前n個字符
            142    string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換為串s
            143    string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換為n個字符c
            144    string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字符串 
            145
            146    string類的插入函數(shù): 
            147
            148    string &insert(int p0, const char *s);
            149    string &insert(int p0, const char *s, int n);
            150    string &insert(int p0,const string &s);
            151    string &insert(int p0,const string &s, int pos, int n);
            152    //前4個函數(shù)在p0位置插入字符串s中pos開始的前n個字符
            153    string &insert(int p0, int n, char c);//此函數(shù)在p0處插入n個字符c
            154    iterator insert(iterator it, char c);//在it處插入字符c,返回插入后迭代器的位置
            155    void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字符
            156    void insert(iterator it, int n, char c);//在it處插入n個字符c
            157  
            158
            159    string類的刪除函數(shù) 
            160
            161    iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除后迭代器的位置
            162    iterator erase(iterator it);//刪除it指向的字符,返回刪除后迭代器的位置
            163    string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字符,返回修改后的字符串 
            164
            165  
            166
            167    string類的迭代器處理: 
            168
            169    string類提供了向前和向后遍歷的迭代器iterator,迭代器提供了訪問各個字符的語法,類似于指針操作,迭代器不檢查范圍。
            170    用string::iterator或string::const_iterator聲明迭代器變量,const_iterator不允許改變迭代的內(nèi)容。常用迭代器函數(shù)有:
            171    const_iterator begin()const;
            172    iterator begin();                //返回string的起始位置
            173    const_iterator end()const;
            174    iterator end();                    //返回string的最后一個字符后面的位置
            175    const_iterator rbegin()const;
            176    iterator rbegin();                //返回string的最后一個字符的位置
            177    const_iterator rend()const;
            178    iterator rend();                    //返回string第一個字符位置的前面
            179    rbegin和rend用于從后向前的迭代訪問,通過設(shè)置迭代器string::reverse_iterator,string::const_reverse_iterator實現(xiàn) 
            180
            181  
            182
            183    字符串流處理: 
            184
            185    通過定義ostringstream和istringstream變量實現(xiàn),<sstream>頭文件中
            186    例如:
            187        string input("hello,this is a test");
            188        istringstream is(input);
            189        string s1,s2,s3,s4;
            190        is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
            191        ostringstream os;
            192        os<<s1<<s2<<s3<<s4;
            193        cout<<os.str(); 
            194*/

            195    return 0;
            196}
            posted on 2008-08-19 17:16 Torres 閱讀(270) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲国产成人久久综合区| 久久毛片一区二区| 国产精品99久久久久久猫咪| 精品久久久久久久久久中文字幕| 亚洲人成网站999久久久综合| 亚洲乱码精品久久久久..| 日本精品久久久中文字幕| 久久久受www免费人成| 狼狼综合久久久久综合网| 久久久久久久久久久免费精品| 亚洲国产一成人久久精品| 国产精品亚洲综合专区片高清久久久| 久久天天躁狠狠躁夜夜不卡| 国产精品成人99久久久久91gav | 久久久久久久人妻无码中文字幕爆| 欧美一区二区精品久久| 亚洲国产精品无码久久SM| 久久精品成人欧美大片| 精品国产福利久久久| 久久人爽人人爽人人片AV| 久久久久久精品久久久久| 久久夜色精品国产| 99精品久久久久久久婷婷| 久久国产乱子伦免费精品| 一本色道久久综合亚洲精品| 久久亚洲天堂| 国内精品久久久久久久涩爱 | 18禁黄久久久AAA片| 久久99精品国产99久久6| 国内精品久久久久久野外| 欧美黑人激情性久久| 亚洲国产精品无码久久久不卡| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久精品亚洲日本波多野结衣| 久久久久se色偷偷亚洲精品av| 久久国产精品无码一区二区三区| 久久精品无码一区二区WWW| 久久这里都是精品| 久久综合狠狠综合久久| 18岁日韩内射颜射午夜久久成人| 国产精品久久久久久久久软件|