<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          #C++ String# C++ string

          共 7575字,需瀏覽 16分鐘

           ·

          2022-11-17 16:47

           文章所涉及內(nèi)容更多來自網(wǎng)絡(luò),在此聲明,并感謝知識的貢獻(xiàn)者!


          初始


          String的初始方式

          1 string s1;

          初始值為空串

          2 string s2(s1);

          使用s1構(gòu)造s2

          3 string s2=s1;

          使用s1構(gòu)造s2

          4 string s3(“value”);

          使用字面值“value”構(gòu)造

          5 string s3=“value”;

          使用字面值“value”構(gòu)造

          6 string s4(n,’c’)

          把s4初始化為由連續(xù)n個(gè)字符c組成的字符串

          7 string s5 = string("value");

          調(diào)用string的構(gòu)造函數(shù)生成一個(gè)臨時(shí)的string類,再用臨時(shí)的string類初始化s5。

          8 string s6(string("value"));

          調(diào)用string的構(gòu)造函數(shù)生成一個(gè)臨時(shí)的string類,再用臨時(shí)的string類初始化s5。


          string的拷貝構(gòu)造

          形式:string s(cp,n)
          解釋:將cp所指的數(shù)組的前n個(gè)字符拷貝給string對象s,n為可選參數(shù)。

          const char *cp = "hello world";//最后有一個(gè)空字符

          char cp2[] = "hello world";//最后有一個(gè)空字符

          char cp3[] = { 'h', 'e' };//最后沒有空字符

          (1) string s1(cp);//s1為”hello world”,長度為11

          (2) string s2(cp2);//s2為”hello world”,長度為11

          (3) string s3(cp3);//因?yàn)閏p3不以空字符結(jié)尾,所以這是未定義行為

          (4) string s4(cp,5);//s4為”hello”,長度為5。將cp改為cp2一樣

          (5) string s5(cp,13);//s5為”hello world  ”,長度為13,后面有兩個(gè)空字符。將cp改為cp2一樣

          (6) string s6(cp3,2);//s6為”he”,長度為2

          形式:

          string s(s1,pos)

          string s(s1,pos,len)

          解釋:

          第一個(gè)將s1從下標(biāo)pos開始拷貝到結(jié)尾。當(dāng)pos>s1.size()時(shí),為未定義行為;當(dāng)pos=s1.size(),拷貝一個(gè)空字符

          第二個(gè)將s1從下標(biāo)pos開始拷貝,拷貝len個(gè)字符。當(dāng)pos>s1.size()時(shí),為未定義行為;當(dāng)pos=s2.size(),拷貝一個(gè)空字符

          string s1("value");

          (1) string s2(s1, 1);//s2為” alue”,長度為4

          (2) string s3(s1, 5);//s3為””,長度為0

          (3) string s8(s1, 6);// 錯(cuò)誤,未定義的行為,拋出異常

          (4) string s4(s1, 1,3);// s4為” alu”,長度為3

          (5) string s5(s1, 1,8);// 正確,s5為” alue”,長度為4

          (6) string s6(s1, 5,8);// s6為””,長度為0

          (7) string s7(s1, 6,1);// 錯(cuò)誤,未定義的行為,拋出異常


          賦值


          String的賦值

          初始化是生成對象的時(shí)候(也就是剛分配內(nèi)存空間時(shí))就給它值;賦值就是過了初始化后,給對象值。

          string st1, st2(2,'b');

          st1 = st2; //st1此時(shí)已近占據(jù)了一塊內(nèi)存


          輸入

          用cin獲取鍵盤輸入的值

          使用cin獲取輸入的字符串或使用cout輸出字符串時(shí),string對象會自動忽略開頭的空白(既空格、換行符、制表符等),并從第一個(gè)真正的字符開始讀入,直到遇到下一處空白


          用getline讀取一整行

          getline的作用是讀取一整行,直到遇到換行符才停止讀取,期間能讀取像空格、Tab等的空白符。實(shí)例如下:

          string s1;

          getline(cin, s1);

          cout << s1 << endl;


          比較

          比較string的大小

          string str = "Hello";

          string phrase = "Hello ";

          盡管兩者的前面對應(yīng)的字符都一樣,但是phrase長度長(多一個(gè)空格),所以phrase>str。

          string str2 = "Hello";

          string phrase2 = "Hi ";

          這種情況比較的是第一個(gè)相異字符,根據(jù)字符值比較大小,因?yàn)閕的字符值>e的字符值,所以phrase2> str2。


          基于compare函數(shù)string比較

          compare,它也可以比較字符串,并且有6種不同的參數(shù)形式,比較字符串時(shí)更加靈活。compare的參數(shù)形式如下:

          參數(shù)形式s.compare()  說明

          s2 比較s和s2

          pos1, n1, s2 將s中從pos1開始的n1個(gè)字符與s2比較

          pos1, n1, s2, pos2, n2 將s中從pos1開始的n1個(gè)字符與s2中從pos2開始的n2個(gè)字符比較

          cp 比較s與cp指向的以空字符結(jié)尾的數(shù)組

          pos1, n1, cp 將s中從pos1開始的n1個(gè)字符與cp指向的以空字符結(jié)尾的數(shù)組比較

          pos1, n1, cp,n2 將s中從pos1開始的n1個(gè)字符與cp指向的以空字符結(jié)尾的數(shù)組前n個(gè)字符比較


          拼接

          string對象間的拼接

          string str = "Hello,";

          string phrase = "world ";

          string s = str + phrase;

          string對象與字符(或字符串)字面值的拼接

          string str = "Hello";

          string phrase = "world";

          string s = str + “ , ”+ phrase+ '\n';

          string str = "Hello";

          (1)string s2 = str + "," + "world";

          (2)string s3 = "Hello" + "," + str;

          分析:(1)正確;(2)錯(cuò)誤

          (2)錯(cuò)誤的原因是:當(dāng)string對象和字符或字符串字面值相加時(shí),必須確保+號的兩側(cè)的運(yùn)算對象至少有一個(gè)string。


          string對象的尾部添加 append

          append是在string對象的末尾進(jìn)行插入操作。這一點(diǎn)使用+運(yùn)算符也能做到。

          string s("i love China!");

          s.append("forever");//執(zhí)行完后,s=” i love China! forever”

          string對象的子字符修改



          遍歷

          根據(jù)下標(biāo)獲取string單個(gè)字符的值

          string s = "Hello world!";

          cout << s[0] << endl;

          cout << s[s.size()-1] << endl;


          基于迭代器逐個(gè)讀取string中的字符

          string s = "Hello world!";

          for (auto i = s.begin(); i != s.end(); i++){

          cout << *i  << ",";

          }

          cout << endl;


          基于范圍的for逐個(gè)讀取string中的字符

          string str("some string");

          for (auto c : str)

          cout << c << ",";

          cout << endl;


          字符串獲取

          string獲取子字符串

          格式:s.substr(pos,n)
          解釋:返回一個(gè)string對象,返回的對象包含s從pos下標(biāo)開始的n個(gè)字符。pos和n均為可選參數(shù)。pos默認(rèn)為下標(biāo)0;n默認(rèn)為s.size()-pos。


          插入

          String插入字符

          基于迭代器的插入

          1 iterator insert( iterator pos, CharT ch )

          2 void insert( iterator pos, size_type count, CharT ch )

          3 void insert( iterator pos, InputIt first, InputIt last )

          4插入初始化列表

          string s1("value");

          s1.insert(s1.begin(), 's');//執(zhí)行后,s1為"svalue"

          s1.insert(s1.begin(), 1, 's');//執(zhí)行后,s1為"ssvalue"

          s1.insert(s1.begin(), s1.begin(), ++s1.begin());//執(zhí)行后,s1為"sssvalue"

          s1.insert(s1.end(), {'1','2'});//執(zhí)行后,s1為"sssvalue12"


          基于下標(biāo)的插入

          1 basic_string& insert( size_type index, size_type count, CharT ch )

          解釋:在下標(biāo)index前插入count個(gè)字符ch。

          2 basic_string& insert( size_type index, const CharT* s );

          basic_string& insert( size_type index, const basic_string& str );

          解釋:在下標(biāo)index前插入一個(gè)常量字符串或者string對象。

          3 basic_string& insert( size_type index, const basic_string& str,

          size_type index_str, size_type count );

          解釋:在下標(biāo)index前插入str中的從str[index_str]開始的count個(gè)字符

          4 basic_string& insert( size_type index, const CharT* s, size_type count );

          解釋:在index前插入常量字符串的count個(gè)字符


          刪除

          String的刪除子字符串

          1basic_string & erase(size_type pos=0, size_type n=npos)

          解釋:如果string對象s調(diào)用,它刪除s從pos下標(biāo)開始的n個(gè)字符,并返回刪除后的s。當(dāng)pos > s.size()時(shí),報(bào)錯(cuò)

          2iterator erase(const_iterator position)

          解釋:如果string對象s調(diào)用,它刪除s迭代器position位置的字符,并返回下一個(gè)字符的迭代器。

          3iterator erase(const_iterator first, const_iterator last)

          解釋:如果string對象s調(diào)用,它刪除s迭代器[first,last)區(qū)間的字符,并返回last字符的迭代器。

          string s1("value");

          string s2("value");

          string s3("value");

          string s4("value");

          s1.erase();//執(zhí)行后,s1為空

          s2.erase(0,2); //執(zhí)行后,s2為”lue”

          s3.erase(s3.begin());//執(zhí)行后,s3為”alue”

          s4.erase(s4.begin(),++s4.begin());//執(zhí)行后,s4為”alue”



          替換


          replace可看作是erase和insert的結(jié)合體,它刪除指定的字符,刪除后再插入指定的字符。

          和insert一樣,可以通過下標(biāo)或者是迭代器指定位置。

          1:下標(biāo)指定刪除的位置

          string s("i very love China!");

          const char* cp1 = "truly";

          const char* cp2 = "truly!!!";

          string str1 = "really";

          string str2 = "really!!!";

          //1.將s從下標(biāo)2開始刪除4個(gè)字符,刪除后在下標(biāo)2處插入cp1

          s.replace(2,4,cp1);//s=” i truly love China!”

          //2.將s從下標(biāo)2開始刪除5個(gè)字符,刪除后在下標(biāo)2插入cp2的前5個(gè)字符

          s.replace(2, 5, cp2,5); //s=” i truly love China!”

          //3.將s從下標(biāo)2開始刪除5個(gè)字符,刪除后在下標(biāo)2插入str1

          s.replace(2, 5, str1);//s=”i really love China!”

          //4.將s從下標(biāo)2開始刪除6個(gè)字符,刪除后在下標(biāo)2插入str2從下標(biāo)0開始的6個(gè)字符

          s.replace(2, 6, str2,0,6);//s=”i really love China!”

          //5.將s從下標(biāo)2開始刪除6個(gè)字符,刪除后在下標(biāo)2插入4個(gè)’*’字符

          s.replace(2, 6, 4, '*');//s=”i **** love China!”

          2:迭代器指定刪除的位置

          string s1("bad phrase");

          const char* cp3 = "sample";

          const char* cp4 = "sample!!!";

          string str3 = "useful";

          string str4 = "useful!!!";

          //1.刪除[s1.begin(),s1. begin()+3)區(qū)間字符,刪除后插入cp3

          s1.replace(s1.begin(),s1.begin()+3,cp3);//s1="sample phrase"

          //2.刪除[s1.begin(),s1. begin()+6)區(qū)間字符,刪除后插入cp4的前6個(gè)字符

          s1.replace(s1.begin(),s1.begin()+6,cp4,6);//s1="sample phrase"

          //3.刪除[s1.begin(),s1. begin()+6)區(qū)間字符,刪除后插入str3

          s1.replace(s1.begin(),s1.begin()+6, str3);//s1="useful phrase"

          //4.刪除[s1.begin(),s1. begin()+6)區(qū)間字符,刪除后插入str4[str4.begin(),str4. begin()+6)區(qū)間字符

          s1.replace(s1.begin(),s1.begin()+6, str4.begin(),str4.begin() + 6);//s1="useful phrase"

          //5. 刪除[s1.begin(),s1. begin()+6)區(qū)間字符,刪除后插入4個(gè)’*’字符

          s1.replace(s1.begin(),s1.begin()+6, 4, '*');//s1="**** phrase"

          //6. 刪除[s1.begin(),s1. begin()+4)區(qū)間字符,刪除后插入初始化列表

          s1.replace(s1.begin(), s1.begin() + 4, {'3','4','5'});//s1="345 phrase"


          string對象的重置

          assign方法可以理解為先將原字符串清空,然后賦予新的值作替換。

          格式如下,可以發(fā)現(xiàn),就輸入?yún)?shù)而言,和“總結(jié)insert和replace”這一節(jié)中的表的args參數(shù)是一樣的,這里不在多做說明,直接給出7個(gè)例子,在例子中說明。

          string& assign (const string& str);

          string& assign (const string& str, size_t subpos, size_t sublen);

          string& assign (const char* s);

          string& assign (const char* s, size_t n);

          string& assign (size_t n, char c);

          template string& assign (InputIterator first, InputIterator last);

          string& assign (initializer_list il);


          查找

          string對象的搜索

          string提供6個(gè)不同的搜索函數(shù),每個(gè)函數(shù)都有4個(gè)重載版本,所有函數(shù)的返回值都為string::size_type值,表示匹配發(fā)生位置的下標(biāo)。

          函數(shù)形式:

          string搜索函數(shù)    描述

          s.find(args) 在s中查找第一次出現(xiàn)args的下標(biāo)

          s.rfind(args) 在s中查找最后一次出現(xiàn)args的下標(biāo)

          s.find_first_of(args) 在s中查找第一個(gè)在args中出現(xiàn)的字符,返回其下標(biāo)

          s.find_first_not_of(args) 在s中查找第一個(gè)不在args中出現(xiàn)的字符,返回其下標(biāo)

          s.find_last_of(args) 在s中查找最后一個(gè)在args中出現(xiàn)的字符,返回其下標(biāo)

          s.find_last_not_of(args) 在s中查找最后一個(gè)不在args中出現(xiàn)的字符,返回其下標(biāo)

          其中args參數(shù)格式如下:

          args參數(shù)格式      描述

          c,pos 搜索單個(gè)字符。從s中位置pos開始查找字符c。pos可省略,默認(rèn)值為0

          s2,pos 搜索字符串。從s中位置pos開始查找字符串string對象s2。pos可省略,默認(rèn)值為0

          cp,pos 搜索字符串。從s中位置pos開始查找指針cp指向的以空字符結(jié)尾的C風(fēng)格字符串。pos可省略,默認(rèn)值為0

          cp,pos,n 從s中位置pos開始查找指針cp指向的數(shù)組的前n個(gè)字符。


          格式化

          String格式化

          #include <boost/format.hpp>

          boost::format f = boost::format("%.2f %s %d");

          f % 1.234 %"123" % 12;

          std::string s = f.str();


          參考資料


          C++ string使用介紹

          https://blog.csdn.net/weixin_43744293/article/details/118299233

          利用boost的format C++ string格式化輸出

          https://www.51sjk.com/b94b128189/



          瀏覽 76
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一区二区天堂 | 无码w台湾 | 天天综合综合大片 | 秋霞午夜视频 | 国产大学生一级A片 |