<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 簡介 string封裝了`char*`,管理這個字符串,是一個`char*`型的容器 # 常用操作 ## string構造函數 ~~~ string();//創建一個空的字符串 例如: string str; string(conststring& str);//使用一個string對象初始化另一個string對象 string(constchar* s);//使用字符串s初始化 string(int n, char c);//使用n個字符c初始化 ~~~ ~~~ string s1; string s2(10, 'a'); string s3(s2); string s4("hello"); cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; ~~~ ## 基本賦值操作 ~~~ string&operator=(constchar* s);//char*類型字符串 賦值給當前的字符串 string&operator=(conststring&s);//把字符串s賦給當前的字符串 string&operator=(char c);//字符賦值給當前的字符串 string& assign(constchar *s);//把字符串s賦給當前的字符串 string& assign(constchar *s, int n);//把字符串s的前n個字符賦給當前的字符串 string& assign(conststring&s);//把字符串s賦給當前字符串 string& assign(int n, char c);//用n個字符c賦給當前字符串 string& assign(conststring&s, int start, int n);//將s從start開始n個字符賦值給字符串,如s=hello,那么n=3,start=1,那么是hel中從e開始賦值3-1個字符 ~~~ ~~~ string s1; s1 = "hello"; cout << s1 << endl; string s2; s2.assign("world", 2); cout << s2 << endl; ~~~ ## string存取字符操作 []和at的區別:[]訪問元素時,越界不拋異常,直接掛,at越界,會拋異常 ~~~ char&operator[](int n);//通過[]方式取字符 char& at(int n);//通過at方法獲取字符 ~~~ ~~~ string s = "hello world"; for (int i = 0; i < s.size(); ++i) { cout << s[i] << " "; } cout << endl; for (int j = 0; j < s.size(); ++j) { cout << s.at(j) << endl; } ~~~ ~~~ //[]和at的區別:[]訪問元素時,越界不拋異常,直接掛,at越界,會拋異常 try { //cout << s[100] << endl; cout << s.at(100) << endl; } catch (out_of_range &ex) { cout << ex.what() << endl; cout << "at越界" << endl; } ~~~ ## 拼接操作 ~~~ string&operator+=(conststring& str);//重載+=操作符 string&operator+=(constchar* str);//重載+=操作符 string&operator+=(constchar c);//重載+=操作符 string& append(constchar *s);//把字符串s連接到當前字符串結尾 string& append(constchar *s, int n);//把字符串s的前n個字符連接到當前字符串結尾 string& append(conststring&s);//同operator+=() string& append(conststring&s, int pos, int n);//把字符串s中從pos開始的n個字符連接到當前字符串結尾 string& append(int n, char c);//在當前字符串結尾添加n個字符c ~~~ ~~~ string s1 = "aaa"; s1 += "bbb"; s1 += 'c'; cout << s1 << endl; s1.append("ddddddd", 3); cout << s1 << endl; ~~~ ## 查找和替換 ~~~ int find(const string& str, int pos = 0) const; //查找str第一次出現位置,從pos開始查找 int find(constchar* s, int pos = 0) const; //查找s第一次出現位置,從pos開始查找 int find(constchar* s, int pos, int n) const; //從pos位置查找s的前n個字符第一次位置 int find(constchar c, int pos = 0) const; //查找字符c第一次出現位置 int rfind(conststring& str, int pos = npos) const;//查找str最后一次位置,從pos開始查找 int rfind(constchar* s, int pos = npos) const;//查找s最后一次出現位置,從pos開始查找 int rfind(constchar* s, int pos, int n) const;//從pos查找s的前n個字符最后一次位置 int rfind(constchar c, int pos = 0) const; //查找字符c最后一次出現位置 string& replace(int pos, int n, const string& str); //替換從pos開始n個字符為字符串str string& replace(int pos, int n, const char* s); //替換從pos開始的n個字符為字符串s ~~~ ~~~ string s = "abcdefgd"; cout << s.find('d') << endl;//3 cout << s.rfind('d') << endl;//7 cout << s.find("kkk") << endl; s.replace(2, 4, "AAA"); cout << s << endl; ~~~ ## 比較操作 ~~~ /* compare函數在>時返回 1,<時返回 -1,==時返回 0。 比較區分大小寫,比較時參考字典順序,排越前面的越小。 大寫的A比小寫的a小。 */ int compare(conststring&s) const;//與字符串s比較 int compare(constchar *s) const;//與字符串s比較 ~~~ ~~~ string s1 = "hello"; string s2 = "hello"; const char* str = "world"; if (s1.compare(s2)==0) { cout << "s1==s2" << endl; } ~~~ ## 子串 ~~~ string substr(int pos = 0, int n = npos) const;//返回由pos開始的n個字符組成的字符串 ~~~ ~~~ string email = "hello world@itcast.com"; int pos = email.find('@'); string username = email.substr(0, pos); cout << username << endl; string prex = email.substr(pos + 1); cout << prex << endl; ~~~ ## 插入和刪除 ~~~ string& insert(int pos, constchar* s); //插入字符串 string& insert(int pos, conststring& str); //插入字符串 string& insert(int pos, int n, char c);//在指定位置插入n個字符c string& erase(int pos, int n = npos);//刪除從Pos開始的n個字符 ~~~ ~~~ string s = "aaaa"; s.insert(3, "AAA"); cout << s << endl; s.insert(3, 5, 'M'); cout << s << endl; s.erase(2, 3); cout << s << endl; ~~~ ## string和c-style字符串轉換 ~~~ const char *str = "hello"; string s=string(str); cout << s << endl; const char *str2=s.c_str(); cout << str2 << endl; ~~~ 在c++中存在一個從const char\*到string的隱式類型轉換,卻不存在從一個string對象到C\_string的自動類型轉換。對于string類型的字符串,可以通過c\_str()函數返回string對象對應的C\_string. 通常,程序員在整個程序中應堅持使用string類對象,直到必須將內容轉化為char\*時才將其轉換為C\_string. ## 迭代器 ~~~ string s = "hello"; for (string::iterator it = s.begin(); it != s.end(); ++it) { cout << *it << " "; } cout << endl; //反向遍歷 for (string::reverse_iterator it = s.rbegin(); it != s.rend(); ++it) { cout << *it << " "; } cout << endl; ~~~ ## 注意 為了修改string字符串的內容,下標操作符\[\]和at都會返回字符的引用。但當字符串的內存被重新分配之后,可能發生錯誤. 原空間被釋放,但是a還是被釋放的s[2]空間的別名,如果操作非法的空間,會出錯 ~~~ string s = "abcde"; char &a = s[2]; char &b = s[3]; a = '1'; b = '2'; cout << "a:" << a << endl; cout << "b:" << b << endl; cout << s << endl; cout << "字符串的原空間地址:" << (int*)s.c_str() << endl; s = "fdasfdasfdsafdasherewrkewhsaferew"; cout << "字符串的空間地址:" << (int*)s.c_str() << endl; ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看