小工具知识网

当前位置: 首页 > 行业知识 > 【C++】string类的使用③(修改器Modifiers || 非成员函数重载Non-member function overloads)

行业知识

【C++】string类的使用③(修改器Modifiers || 非成员函数重载Non-member function overloads)

2026-10-31 23:01:57 来源:互联网转载

个人主页: Forcible Bug Maker

专栏: STL || C++

目录

  • 前言
  • 修改器(Modifiers)
    • ==**operator+=**==
    • ==append==
    • ==push_back和pop_back==
    • ==assign==
    • ==insert==
    • ==erase==
    • ==replace==
    • ==swap==
    • 非成员函数重载(Non-member function overloads)
      • ==operator+==
      • ==relation operators(string)==
      • ==swap(string)==
      • ==流插入和流提取重载==
      • ==getline==
      • 结语

        前言

        本篇博客主要内容:STL库中string的修改器(Modifiers)和非成员函数重载(Non-member function overloads)。

        来到string类的使用第三篇,继续我们的内容,本篇博客将介绍如何使用STL库中string的成员函数修改串,以及重载给string的几个非成员函数。

        修改器(Modifiers)

        顾名思义,就是一批能改动string串中内容的成员函数。

        operator+=

        这是一个成员函数的运算符重载。

        简单说就是在串的末尾追加字符或字符串。

        (1) string string对象

        string& operator+= (const string& str);

        (2) c-string 字符串指针

        string& operator+= (const char* s);

        (3) character 字符

        string& operator+= (char c);

        共同的返回值:当前对象的引用(string&)。

        使用样例:

        // string::operator+=#include #include using namespace std;int main(){string name("John");string family("Smith");name += " K. ";         // c-stringname += family;         // stringname += '\n';           // charactercout string str;string str2 = "Writing ";string str3 = "print 10 and then 5 more";// used in the same order as described above:str.append(str2);                       // "Writing "str.append(str3, 6, 3);                   // "10 "str.append("dots are cool", 5);          // "dots "str.append("here: ");                   // "here: "str.append(10, '.');                    // ".........."str.append(str3.begin() + 8, str3.end());  // " and then 5 more"cout string str("hello world");str.push_back('!');cout string str;string base = "The quick brown fox jumps over a lazy dog.";// used in the same order as described above:str.assign(base);cout string str = "to be question";string str2 = "the ";string str3 = "or not to be";string::iterator it;// used in the same order as described above:str.insert(6, str2);                 // to be (the )questionstr.insert(6, str3, 3, 4);             // to be (not )the questionstr.insert(10, "that is cool", 8);    // to be not (that is )the questionstr.insert(10, "to be ");            // to be not (to be )that is the questionstr.insert(15, 1, ':');               // to be not to be(:) that is the questionit = str.insert(str.begin() + 5, ','); // to be(,) not to be: that is the questionstr.insert(str.end(), 3, '.');       // to be, not to be: that is the question(...)str.insert(it + 2, str3.begin(), str3.begin() + 3); // (or )cout     std::string str("This is an example sentence.");    cout string base = "this is a test string.";string str2 = "n example";string str3 = "sample phrase";string str4 = "useful.";// replace signatures used in the same order as described above:// Using positions:                 0123456789*123456789*12345string str = base;           // "this is a test string."str.replace(9, 5, str2);          // "this is an example string." (1)str.replace(19, 6, str3, 7, 6);     // "this is an example phrase." (2)str.replace(8, 10, "just a");     // "this is just a phrase."     (3)str.replace(8, 6, "a shorty", 7);  // "this is a short phrase."    (4)str.replace(22, 1, 3, '!');        // "this is a short phrase!!!"  (5)// Using iterators:0123456789*123456789*str.replace(str.begin(), str.end() - 3, str3);                    // "sample phrase!!!"      (1)str.replace(str.begin(), str.begin() + 6, "replace");             // "replace phrase!!!"     (3)str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7);    // "replace is cool!!!"    (4)str.replace(str.begin() + 12, str.end() - 4, 4, 'o');                // "replace is cooool!!!"  (5)str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end());// "replace is useful."    (6)cout string str1("hello");string str2("world");cout string firstlevel("com");string secondlevel("cplusplus");string scheme("http://");string hostname;string url;hostname = "www." + secondlevel + '.' + firstlevel;url = scheme + hostname;cout string foo = "alpha";string bar = "beta";if (foo == bar) cout string str1("hello");string str2("world");cout string str;cin  str;cout string name;cout 

上一篇:怎么关闭eclipse服务器?

下一篇:怎么看域名服务器价格?