【C++ 十一】C++ string容器

string 容器



前言

本文包含string基本概念、string构造函数、string赋值操作、string字符串拼接、string查找和替换、string字符串比较、string字符串存取、string插入和删除、string子串。


1 string 基本概念

本质: string 是 C++ 风格的字符串,而 string 本质上是一个类

string 和 char * 区别:

(1)、char * 是一个指针

(2)、string 是一个类,类内部封装了 char*,管理这个字符串,是一个 char* 型的容器

特点:

(1)、string 类内部封装了很多成员方法。例如:查找 find、拷贝 copy、删除 delete、替换 replace、插入 insert

(2)、string 管理 char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

2 string 构造函数

构造函数原型:

(1)、string(); 创建一个空的字符串。例如:string str;

(2)、string(const char* s); 使用字符串 s 初始化

(3)、string(const string& str); 使用一个 string 对象初始化另一个 string 对象 (拷贝构造)

(4)、string(int n, char c); 使用 n 个字符 c 初始化

// string构造函数

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	// 1、创建空字符串,调用无参构造函数
	string str1;
	cout << "str1 = " << str1 << endl;

	// 2、c 语言风格的字符串
	const char* c = "Hello";

	// 把c_string转换成了string
	string str2(c);
	cout << "str2 = " << c << endl;

	// 3、调用拷贝构造函数
	string str3(str2);
	cout << "str3 = " << str3 << endl;

	// 4、10个a
	string str4(10, 'a');
	cout << "str4 = " << str4 << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

3 string 赋值操作

功能描述: 给 string 字符串进行赋值

赋值的函数原型:

(1)、string& operator=(const char* s); char* 类型字符串赋值给当前的字符串

(2)、string& operator=(const string &s); 把字符串 s 赋给当前的字符串 (拷贝构造)

(3)、string& operator=(char c); 字符赋值给当前的字符串

(4)、string& assign(const char *s); 把字符串 s 赋给当前的字符串 (assign一个成员函数)

(5)、string& assign(const char *s, int n); 把字符串 s 的前 n 个字符赋给当前的字符串

(6)、string& assign(const string &s); 把字符串 s 赋给当前字符串

(7)、string& assign(int n, char c); 用 n 个字符 c 赋给当前字符串

// string赋值操作

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	// 1、char*类型字符串"张三",赋值给str1
	string str1 = "张三";
	cout << "str1 = " << str1 << endl;

	// 2、将字符串str1赋值给str2;相当于拷贝构造
	string str2 = str1;
	cout << "str2 = " << str2 << endl;

	// 3、字符'a',赋值给str3
	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	// 4、字符串"张三",使用assign()函数,赋值给str4
	string str4;
	str4.assign("张三");
	cout << "str4 = " << str4 << endl;

	// 5、把字符串"Hello World",前五个,赋值给str5
	string str5;
	str5.assign("Hello World", 5);
	cout << "str5 = " << str5 << endl;

	// 6、把字符串str5,通过assign()函数,赋值给str6
	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	// 7、使用assign()函数,将10个a赋值给str7
	string str7;
	str7.assign(10, 'a');
	cout << "str7 = " << str7 << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

4 string 字符串拼接

功能描述: 实现在字符串末尾拼接字符串

函数原型:

(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 连接到当前字符串结尾 (append一个成员函数)

(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 个字符连接到字符串结尾

// string字符串拼接

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	// 1、重载 += 操作符
	string str = "学";
	str += "习 C++";
	cout << "str = " << str << endl;

	// 2、重载 += 操作符
	str += '!';
	cout << "str = " << str << endl;

	// 3、重载 += 操作符
	str += " 月薪";
	cout << "str = " << str << endl;

	// 4、使用append()函数,将字符串"过万!"连接到str结尾
	str.append("过万!");
	cout << "str = " << str << endl;

	// 5、使用append()函数,将字符串" 不可能!"前9个字符(一个汉字两个字符),连接到str结尾
	str.append(" 不可能!", 9);
	cout << "str = " << str << endl;

	// 6、使用append()函数,将字符串str1,连接到str结尾
	string str1 = "绝对";
	str.append(str1);
	cout << "str = " << str << endl;

	// 7、使用append()函数,从将字符串str2的下标5位置开始(一个汉字占两个字节),截取8个字符,连接到str结尾
	string str2 = "..........";
	str.append(str2, 4, 6);
	cout << "str = " << str << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

5 string 查找和替换

功能描述:

(1)、查找:查找指定字符串是否存在

(2)、替换:在指定的位置替换字符串

函数原型:

(1)、int find(const string& str, int pos = 0) const; 查找 str 第一次出现位置,从 pos 开始查找

(2)、int find(const char* s, int pos = 0) const; 查找 s 第一次出现位置,从 pos 开始查找

(3)、int find(const char* s, int pos, int n) const; 从 pos 位置查找 s 的前 n 个字符第一次位置

(4)、int find(const char c, int pos = 0) const; 查找字符 c 第一次出现位置

(5)、int rfind(const string& str, int pos = npos) const; 查找 str 最后一次位置,从 pos 开始查找

(6)、int rfind(const char* s, int pos = npos) const; 查找 s 最后一次出现位置,从 pos 开始查找

(7)、int rfind(const char* s, int pos, int n) const; 从 pos 查找 s 的前 n 个字符最后一次位置

(8)、int rfind(const char c, int pos = 0) const; 查找字符 c 最后一次出现位置

// string查找

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "abcdefgcdhicd";  // 下标从0开始
	string str1 = "cd";

	// 1、查找字符串str第一次出现字符串str1的位置,从下标0开始查找
	int pos1 = str.find(str1, 0);  // 第二个参数:int pos = 0;默认值为0,可以不写

	if (pos1 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos1 = " << pos1 << endl;  // 2
	}

	// 2、查找字符串str第一次出现"cd"的位置,从下标0开始查找
	int pos2 = str.find("cd", 0);  // 第二个参数:int pos = 0;默认值为0,可以不写

	if (pos2 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos2 = " << pos2 << endl;  // 2
	}

	// 3、从下标7开始查找,查找"cd"的第1个字符c,第一次出现的位置
	int pos3 = str.find("cd", 7, 1);

	if (pos3 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos3 = " << pos3 << endl;  // 7
	}


	// 4、从下标5开始查找,查找字符'c',第一次出现的位置
	int pos4 = str.find('c', 5);  // 第二个参数:int pos = 0;不写则默认为0

	if (pos4 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos4 = " << pos4 << endl;  // 7
	}

	// 5、从下标0开始查找,查找字符串str1,最后一次出现的位置
	// rfind和find区别:rfind从右往左查找,find从左往右查找
	int pos5 = str.rfind(str1);  // 第二个参数:int pos = 0;不写则默认为0

	if (pos5 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos5 = " << pos5 << endl;  // 11
	}

	// 6、从下标10开始查找,查找"cd",最后一次出现的位置
	// rfind和find区别:rfind从右往左查找,find从左往右查找
	int pos6 = str.rfind("cd", 10);  // 第二个参数:int pos = 0;不写则默认为0

	if (pos6 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos6 = " << pos6 << endl;  // 7
	}

	// 7、从下标3开始查找,查找"cd"前两个字符cd,最后一次出现的位置
	int pos7 = str.rfind("cdef", 3, 2);

	if (pos7 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos7 = " << pos7 << endl;  // 2
	}

	// 8、从下标3开始查找,查找字符'c',最后一次出现的位置
	int pos8 = str.rfind('c', 3);  // 第二个参数:int pos = 0;不写则默认为0

	if (pos8 == -1) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到了! pos8 = " << pos8 << endl;  // 2
	}
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

(9)、string& replace(int pos, int n, const string& str); 替换从 pos 开始 n 个字符为字符串 str

(10)、string& replace(int pos, int n,const char* s); 替换从 pos 开始的 n 个字符为字符串 s

// string替换

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "abcdefgcdhicd";  // 下标从0开始
	string str1 = "000";

	// 1、从下标2开始,3个字符,替换为str1
	str.replace(2, 3, str1);
	cout << "str = " << str << endl;

	// 2、从下标7开始,3个字符,替换为"000"
	str.replace(7, 3, "000");
	cout << "str = " << str << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

6 string 字符串比较

功能描述: 字符串之间的比较

比较方式:字符串比较是按字符的 ASCII 码进行对比

(1)、= 返回 0

(2)、> 返回 1

(3)、< 返回 -1

函数原型:

(1)、int compare(const string &s) const; 与字符串 s 比较

(2)、int compare(const char *s) const; 与字符串 s 比较

// string字符串比较

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "C ++";
	string str1 = "C ++";

	// 1、str调用compare()函数,与str1做对比
	int res = str.compare(str1);

	if (res == 0) {
		cout << "str 等于 str1" << endl;
	}
	else if (res > 0) {  // 大于0,可以写成大于0
		cout << "str 大于 str1" << endl;
	}
	else {
		cout << "str 小于 str1" << endl;
	}


	// 2、str调用compare()函数,与字符串"C ++"做对比
	res = str.compare("C --");

	if (res == 0) {
		cout << "str 等于 str1" << endl;
	}
	else if (res > 0) {  // 大于0,可以写成大于0
		cout << "str 大于 str1" << endl;
	}
	else {
		cout << "str 小于 str1" << endl;
	}
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

7 string 字符存取

string 中单个字符存取方式有两种:

(1)、char& operator[](int n); 通过 [] 方式取字符

(2)、char& at(int n); 通过 at 方法获取字符

// string字符存取

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "Hello";  // 下标从0开始

	// 1、通过[]访问单个字符
	// str.size()返回str字符串的长度
	for (int i = 0; i < str.size(); i++) {
		cout << str[i] << " ";
	}
	cout << endl;

	// 字符修改
	str[4] = '0';  // 修改下标为4的字符为“0”
	cout << str << endl;

	cout << endl;

	// 2、通过at()函数方式访问单个字符
	for (int i = 0; i < str.size(); i++) {
		cout << str.at(i) << " ";
	}
	cout << endl;

	// 字符修改
	str.at(4) = 'o';  // 修改下标为4的字符为“o”
	cout << str << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

8 string 插入和删除

功能描述: 对 string 字符串进行插入和删除字符操作

函数原型:

(1)、string& insert(int pos, const char* s); 从 pos 开始插入字符串

(2)、string& insert(int pos, const string& str); 从 pos 开始插入字符串变量

(3)、string& insert(int pos, int n, char c); 在指定位置插入 n 个字符 c

(4)、string& erase(int pos, int n = npos); 删除从 pos 开始的 n 个字符

// string插入和删除

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "Hello";  // 下标从0开始

	// 1、从字符串str下标5的位置,插入" C++"
	str.insert(5, " C++");
	cout << "str = " << str << endl;

	// 2、从字符串str下标5的位置,插入str1
	string str1 = " Java";
	str.insert(9, str1);
	cout << "str = " << str << endl;

	// 3、从字符串str下标5的位置,插入1个字符'c'
	str.insert(14, 1, '!');
	cout << "str = " << str << endl;

	// 4、从字符串str下标5的位置,插入1个字符'c'
	str.erase(9, 6);
	cout << "str = " << str << endl;
}

int main() {

	test();
	
	cout << endl;

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述

9 string 子串

功能描述: 从字符串中获取想要的子串

函数原型:

string substr(int pos = 0, int n = npos) const; 返回由 pos 开始的 n 个字符组成的字符串

// string子串

#include <iostream>  // 包含标准输入输出流头文件
using namespace std;  // 使用标准命名空间

void test() {

	string str = "Hello C++";

	// 从字符串str下标6的位置,返回3个字符,类型为string
	string sub = str.substr(6, 3);
	cout << "sub = " << sub << endl;

	// string字符串求子串,可以用于获取Email的用户名
	string email = "hello@163.com";
	int pos = email.find("@");  // find()函数,返回所在下标,从1开始

	// 从字符串email下标0的位置,返回pos个字符,类型为string
	string userName = email.substr(0, pos);
	cout << "userName = " << userName << endl;
}

int main() {

	test();

	system("pause");  // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果

	return 0;  // 程序正常退出
}

在这里插入图片描述


总结

(1)、string 的多种构造方式没有可比性,灵活使用即可;

(2)、string 的赋值方式很多,operator= 这种方式是比较实用的

(3)、字符串拼接的重载版本很多,初学阶段记住几种即可;

(4)、find 查找是从左往后,rfind 从右往左;

(5)、find 找到字符串后返回查找的第一个字符位置,找不到返回 -1;

(6)、replace 在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串;

(7)、string 字符串中单个字符存取有两种方式,利用 [ ] 或 at;

(8)、string 字符串插入和删除的起始下标都是从 0 开始;

(9)、灵活的运用求子串功能,可以在实际开发中获取有效的信息。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鹿快跑~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值