【C++】STL 容器 - string 字符串操作 ④ ( string 字符串连接 | 字符串重载函数 - operator+= 函数 | append 函数 )






一、string 字符串连接



1、字符串重载函数 - operator+= 函数


下面函数的作用是 将 字符串 s 连接到 当前 string 类型字符串对象 的结尾 , char* 类型字符串 和 string 类型字符串都可 ;

// 返回的是本字符串 , 方便链式调用
string& operator+=(const string &s);  
string& operator+=(const char *s);

string& operator+=(const string &s); 函数原型 分析 : 该函数 是 string 类中用于重载 += 运算符的成员函数原型 ;

operator+= 函数的主要目的是将一个 string 对象追加到另一个 string 对象的末尾 , 并返回修改后的 string 对象的引用 ;

string& operator+=(const string &s); 函数返回一个对 string 对象的引用 , 这意味着当你使用这个运算符时 , 你实际上是在操作原始对象 , 而不是创建一个新的对象 ; 通过返回引用 , 可以实现链式操作 , 如 : str1 += str2 += str3 ;


代码示例 :

	string s1 = "123456789";
	string s2 = "abcdefghi";

	// 使用 重载运算符 += 连接 s1 与 s2 字符串
	s1 += s2;
	cout << "s1 = " << s1 << endl;

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	s2 += "ABCD";
	cout << "s2 = " << s2 << endl;

2、字符串重载函数 - operator+ 函数


operator+ 函数 是 string 类中的 成员函数 , 该函数接受一个 const string 类字符串 或 const char* 字符串 , 作为参数,返回一个新的 string 类型的值 ;

// 返回的是新字符串
string operator+(const string& s);  
string operator+(const char* s);

特别注意 : 该函数不能进行链式调用 ;


代码示例 :

	string s1 = "123456789";
	string s2 = "abcdefghi";

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	string s3 = s1 + s2;
	cout << "s3 = " << s3 << endl;

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	string s4 = s2 + "EFG";
	cout << "s4 = " << s4 << endl;

3、append 函数


append 函数 是 C++ 语言 中的 标准库中 std::string 类的一个成员函数 , 用于向字符串的末尾添加内容 ;

append 函数原型 :

// 将 字符串 s 连接到当前字符串结尾
string& append(const char* s); 
string& append(const string& s);   

// 将 字符串 s 的前 n 个字符连接到当前字符串结尾
string& append(const char* s,int n);

// 将 字符串 s 中从 pos 开始的 n 个字符连接到当前字符串结尾
string& append(const string& s, int pos, int n);

// 将 n 个字符 c 添加到 字符串 结尾
string& append(int n, char c);

string& append(const char* s)string& append(const string& s)string 类中的一个成员函数 , 用于向字符串的末尾追加 char* 或 string 字符串 ;

  • string& 返回值 : 函数返回一个对string对象的引用 , 意味着可以 链式调用 其他 string 成员函数 ;
  • const char* s 参数 : 表示要追加到字符串末尾的 C 风格字符串 ;
  • const string& s 参数 : 末尾追加 string 字符串 ;

代码示例 :

	// II. append 函数


	// 使用 append 函数 尾部追加 string 字符串
	s1.append(s2);
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 char* 字符串
	s1.append(" Hello ");
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 char* 字符串 前 9 字符
	s1.append("Tom And Jerry", 9);
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 3 个 X
	s1.append(3, 'X');
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 string 字符串 第 7 个字符处的 3 个字符
	s1.append(string("Son Of Beach"), 7, 3);
	cout << "s1 = " << s1 << endl;

4、代码示例 - string 字符串连接


代码示例 :

#include "iostream"
using namespace std;
#include "string"

int main() {
    
    

	string s1 = "123456789";
	string s2 = "abcdefghi";

	// I. 运算符重载 

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	string s3 = s1 + s2;
	cout << "s3 = " << s3 << endl;

	// 使用 重载运算符 += 连接 s1 与 s2 字符串
	s1 += s2;
	cout << "s1 = " << s1 << endl;

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	s2 += "ABCD";
	cout << "s2 = " << s2 << endl;

	// 使用 重载运算符 + 连接 s1 与 s2 字符串
	string s4 = s2 + "EFG";
	cout << "s4 = " << s4 << endl;


	// II. append 函数


	// 使用 append 函数 尾部追加 string 字符串
	s1.append(s2);
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 char* 字符串
	s1.append(" Hello ");
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 char* 字符串 前 9 字符
	s1.append("Tom And Jerry", 9);
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 尾部追加 3 个 X
	s1.append(3, 'X');
	cout << "s1 = " << s1 << endl;

	// 使用 append 函数 string 字符串 第 7 个字符处的 3 个字符
	s1.append(string("Son Of Beach"), 7, 3);
	cout << "s1 = " << s1 << endl;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

s3 = 123456789abcdefghi
s1 = 123456789abcdefghi
s2 = abcdefghiABCD
s4 = abcdefghiABCDEFG
s1 = 123456789abcdefghiabcdefghiABCD
s1 = 123456789abcdefghiabcdefghiABCD Hello
s1 = 123456789abcdefghiabcdefghiABCD Hello Tom And J
s1 = 123456789abcdefghiabcdefghiABCD Hello Tom And JXXX
s1 = 123456789abcdefghiabcdefghiABCD Hello Tom And JXXXBea
请按任意键继续. . .

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/135038879