功能描述:

    • 实现在字符串末尾拼接字符串

    函数原型:

    1. string& operator+=(const char* str); //重载+=操作符
    2. string& operator+=(const char c); //重载+=操作符
    3. string& operator+=(const string& str); //重载+=操作符
    4. string& append(const char *s); //把字符串s链接到当前字符串结尾
    5. string& append(const char *s,int n); //把字符串s的前n个字符串连接到当前字符串结尾
    6. string& append(const string &s); //同operator+=(const string &str)
    7. string& append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. //string的拼接操作
    5. /*
    6. string& operator+=(const char* str); //重载+=操作符
    7. string& operator+=(const char c); //重载+=操作符
    8. string& operator+=(const string& str); //重载+=操作符
    9. string& append(const char *s); //把字符串s链接到当前字符串结尾
    10. string& append(const char *s,int n); //把字符串s的前n个字符串连接到当前字符串结尾
    11. string& append(const string &s); //同operator+=(const string &str)
    12. string& append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
    13. */
    14. void test01()
    15. {
    16. string str1 = "我";
    17. str1 += "爱玩游戏";
    18. cout << "str1 = " << str1 << endl;
    19. str1 += ":";
    20. cout << "str1 = " << str1 << endl;
    21. string str2 = "LOL DNF";
    22. str1 += str2;
    23. cout << "str1 = " << str1 << endl;
    24. string str3 = "I";
    25. str3.append("Love");
    26. cout << "str3 = " << str3 << endl;
    27. str3.append("game aaa", 4);
    28. cout << "str3 = " << str3 << endl;
    29. str3.append(str2);
    30. cout << "str3 = " << str3 << endl;
    31. str3.append(str2, 4, 3);
    32. cout << "str3 = " << str3 << endl;
    33. }
    34. int main(void)
    35. {
    36. test01();
    37. system("pause");
    38. return 0;
    39. }