
 
目录
🌞专栏导读
🌛为什么学习string类?
⭐C语言中的字符串
🌛标准库中的string类
⭐基本使用string
⭐string类的常用接口
⭐总结:
🌛范围for的使用
🌞专栏导读
🌟作者简介:日出等日落,在读本科生一枚,致力于 C/C++、Linux 学习。
🌟本文收录于 C++系列,本专栏主要内容为 C++ 初阶、C++ 进阶、STL 详解等,持续更新!
🌟相关专栏推荐:C语言系列 、Linux系列 、数据结构与算法
🌛为什么学习string类?
⭐C语言中的字符串
有的小伙伴会有这样的疑问——C语言中可以使用字符串吗,C++为什么还要引入string类?
首先我们得认识到,C语言中是不存在字符串类型的。在C语言中,字符串是使用字符数组表示的,这种方式比较容易出现错误,如数组越界、缓冲区溢出等。
C++中有string类是因为它提供了一种更方便和安全的处理字符串的方式。C++的string类则是一个标准库中的类,它是一个容器,可以存储字符串,同时提供了许多方便的方法来操作字符串,如查找、替换、拼接等。
使用string类,可以避免手动处理字符串时出现的错误,如内存泄漏、越界、缓冲区溢出等问题,同时也减少了代码量,提高了代码的可读性和可维护性。string类还支持重载运算符,使得对字符串的操作更加直观和方便。
因此,C++中引入string类是为了更加方便、安全地处理字符串,提高代码的可读性和可维护性。
在OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数。
🌛标准库中的string类
⭐基本使用string
//使用string类前需要包含头文件< string >;
#include<string>
//创建一个string类对象;
string str;
//创建并初始化对象;
string s1("hello world");
string s2 = "hellow world";\
//使用[]或at()函数来访问字符串中的单个字符。但是at()函数会进行边界检查,避免越界访问;
	string str = "hello world";
	cout << str[0] << endl;
	cout << str.at(1) << endl;
//可以使用加号运算符+将两个字符串拼接起来,也可以使用append()函数将一个字符串添加到另一个字符串的末尾;
string str1 = "hello";
	string str2 = "world";
	string str3 = str1 + str2;          // 将str1和str2拼接起来
	string str4 = str1.append(str2);    // 将str2添加到str1的末尾
	str+='a';                           // 向str末尾添加一个字符'a'
//使用cin向string类对象中输入数据(遇到空格读取结束);
   	string str;
	cin >> str;
//使用getline函数向string类对象中输入数据(遇到换行符读取结束);
    string str;
	str.getline();            //或者可以这样写___getline(cin,str);
//使用cout输出string类对象的内容;
	string str;
	cout << str << endl; 
⭐string类的常用接口
operator[]:返回当前字符串中指定位置的字符;at(size_t pos):返回当前字符串中指定位置的字符,并进行边界检查;front():返回当前字符串中的第一个字符。;back():返回当前字符串中的最后一个字符;c_str():c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同。这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
	string str = "hello world";
	cout << str[0] << endl;
	cout << str.at(1) << endl;
	string str = "hello world";
	cout << str.front() << endl;
	cout << str.back() << endl;
	string str = "hello world";
	cout << str.c_str() << endl;
 
empty():判断当前字符串是否为空;size():返回当前字符串的字符数,不包含'\0';length():返回当前字符串的字符数,不包含'\0';capacity():返回当前字符串容量,即可以存储的字符数;reserve():为当前字符串分配指定的容量,即扩容;resize():扩容并初始化;operator+:将两个字符串拼接起来;append():将一个字符串添加到另一个字符串的末尾;
	string str1 = "hello";
	string str2 = "world";
	string str3 = str1 + str2;          // 将str1和str2拼接起来
	string str4 = str1.append(str2);    // 将str2添加到str1的末尾
 
insert():在指定位置插入一个字符串;
	string str = "hello world";
	cout << str.insert(0, "aaaa") << endl;  //在位置0处插入字符串
	cout << str.insert(0, 5, 'a') << endl;  //在位置0处插入5个字符'a'
 
erase():删除指定位置的一个字符或一段字符;
	string str = "hello world";
	cout << str.erase(0,5) << endl;  //删除从位置0开始的5个字符
	cout << str.erase() << endl;  //清空字符串
 
substr():返回一个子串,包含从指定位置开始的指定数量的字符;
	string str = "hello world";
	//返回字符串中从位置0处开始的长度为3的字串
	string substr = str.substr(0, 3);  
 
find():在当前字符串中查找指定子串的位置;rfind():在当前字符串中从后往前查找指定子串的位置;compare():将当前字符串与另一个字符串进行比较;
关于string类中的函数接口我们就简单认识这些。库中
string类的接口有一百多个,但是我们平时高平率使用的也就几个到十几个而已。在以后的工作当中,我们应该注重官方文档的使用,多查询文档能使我们对接口的使用更加准确和规范。这里是网站的入口:
文档
⭐总结:
🌛范围for的使用
范围for循环是C++11新增的一种语法结构,用于遍历容器类中的元素。它可以遍历数组、容器类等可迭代的对象,使得程序员可以更加简洁地遍历容器中的元素,而不必关心迭代器的细节。
	string str = "hello world";
	
	for (auto ch : str)
	{
		cout << ch << ' ';
	}
	cout << endl;
 
可以发现,它比我们之前所用的for循环更加方便。










![[语义分割] ASPP不同版本对比(DeepLab、DeepLab v1、DeepLab v2、DeepLab v3、DeepLab v3+、LR-ASPP)](https://img-blog.csdnimg.cn/08d74f7c54de4c45b4344eab71438481.png#pic_center)











