【C++STL精讲】string类的基本使用与常用接口

举报
花想云 发表于 2023/05/30 23:08:08 2023/05/30
【摘要】 本章我们将学习STL中的string类。学会string类的基本使用以及常用的函数接口。学会使用迭代器与范围for循环。

image.png

💐文章导读

本章我们将学习STL中的string类。学会string类的基本使用以及常用的函数接口。学会使用迭代器范围for循环

🌷为什么要学习string类?

有的小伙伴会有这样的疑问——C语言中可以使用字符串吗,C++为什么还要引入string类?

首先我们得认识到,C语言中是不存在字符串类型的。在C语言中,字符串是使用字符数组表示的,这种方式比较容易出现错误,如数组越界、缓冲区溢出等。

C++中有string类是因为它提供了一种更方便和安全的处理字符串的方式。C++的string类则是一个标准库中的类,它是一个容器,可以存储字符串,同时提供了许多方便的方法来操作字符串,如查找、替换、拼接等。

使用string类,可以避免手动处理字符串时出现的错误,如内存泄漏、越界、缓冲区溢出等问题,同时也减少了代码量,提高了代码的可读性和可维护性。string类还支持重载运算符,使得对字符串的操作更加直观和方便。

因此,C++中引入string类是为了更加方便、安全地处理字符串,提高代码的可读性和可维护性。

OJ中,有关字符串的题目基本以string类的形式出现,而且在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数。

🌷string类的基本使用

  • 使用string类前需要包含头文件< string >;
#include<string>
  • 创建一个string类对象;
	string s1;
  • 创建并初始化对象;
	string s2("hello world");
	string s3 = "hello 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'
  • 使用cinstring类对象中输入数据(遇到空格读取结束);
	string str;
	cin >> str;
  • 使用getline函数向string类对象中输入数据(遇到换行符读取结束);
	string str;
	str.getline();
  • 使用cout输出string类对象的内容;
	string str;
	cout << str << endl;

🌷string类的常用接口

🌺数据访问函数

  • operator[]:返回当前字符串中指定位置的字符;
  • at(size_t pos):返回当前字符串中指定位置的字符,并进行边界检查;
	string str = "hello world";
	cout << str[0] << endl;
	cout << str.at(1) << endl;
  • front():返回当前字符串中的第一个字符。;
  • back():返回当前字符串中的最后一个字符;
	string str = "hello world";
	cout << str.front() << endl;
	cout << str.back() << endl;
  • c_str():返回一个指向当前字符串内容的C风格字符串;
	string str = "hello world";
	cout << str.c_str() << endl;

🌺容量相关函数

  • empty():判断当前字符串是否为空;
	string str = "hello world";
	cout << str.empty() << endl;
  • size():返回当前字符串的字符数,不包含'\0'
	string str = "hello world";
	cout << str.size() << endl;
  • length():返回当前字符串的字符数,不包含'\0'
	string str = "hello world";
	cout << str.length() << endl;
  • capacity():返回当前字符串容量,即可以存储的字符数;
	string str = "hello world";
	cout << str.capacity() << endl;
  • reserve():为当前字符串分配指定的容量,即扩容;
	string str = "hello world";
	str.reserve(100);
  • resize():扩容并初始化;
	string str = "hello world";
	str.resize(100, 'a');

🌺操作函数

  • operator+:将两个字符串拼接起来;
  • append():将一个字符串添加到另一个字符串的末尾;
	string str1 = "hello";
	string str2 = "world";
	string str3 = str1 + str2;          // 将str1和str2拼接起来
	string str4 = str1.append(str2);    // 将str2添加到str1的末尾
  • replace():用一个字符串替换另一个字符串中的指定部分;
	//在下标为0处,替换1个字符'a',长度为1
	string str = "hello world";
	cout << str.replace(0, 1, 1, 'a');
  • 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():在当前字符串中从后往前查找指定子串的位置;
	string str = "hello world";
	//从位置0处开始寻找字串"world",若找到就返回字串的起始位置
	cout << str.find("world",0) << endl;
  • compare():将当前字符串与另一个字符串进行比较;
	string str1 = "hello";
	string str2 = "world";
	str1.compare(str2);

关于string类中的函数接口我们就简单认识这些。库中string类的接口有一百多个,但是我们平时高平率使用的也就几个到十几个而已。在以后的工作当中,我们应该注重官方文档的使用,多查询文档能使我们对接口的使用更加准确和规范。这里是文档的入口:
文档

🌷迭代器与范围for的使用

🌺迭代器

迭代器是什么

迭代器是一种通用的概念,它提供了一种方式来遍历容器中的元素,不必关心容器的具体类型和实现方式。在C++中,迭代器被广泛地应用于STL(标准模板库)中,包括vectorlistmap等容器类,使得程序员可以方便地访问和操作容器中的元素。

迭代器的实现原理是基于指针,它本质上是一个类似于指针的对象,它指向容器中的元素,并提供了一组操作方法,使得程序员可以通过迭代器来遍历容器中的元素。迭代器可以像指针一样进行自增、自减操作,以及支持解引用操作来获取指向的元素值。

在目前阶段,我们不对迭代器做过多的讲解,我们可以粗浅的把它看作指针一样的东西来使用。

🍁迭代器的使用

在C++中,字符串类string也支持迭代器的使用,可以使用迭代器来访问字符串中的每一个元素。

string类的迭代器提供了begin()end()方法,begin()返回一个指向字符串第一个元素的迭代器,end()返回一个指向字符串最后一个元素的下一个位置的迭代器。这样,我们就可以使用迭代器来遍历整个字符串。

	string str = "hello world";

	// 使用迭代器遍历字符串
	for (string::iterator it = str.begin(); it != str.end(); ++it) {
		cout << *it << " ";
	}

image.png

🍁反向迭代器

顾名思义,反向迭代器就是和迭代器逆序。利用反向迭代器来遍历字符串:

	string str = "hello world";
	
	//反向迭代器的使用
	//string::const_reverse_iterator rit = s.rbegin();
	for (auto rit = str.rbegin(); rit != str.rend(); ++rit) {
		cout << *rit << " ";
	}

image.png

🌺范围for

范围for循环C++11新增的一种语法结构,用于遍历容器类中的元素。它可以遍历数组、容器类等可迭代的对象,使得程序员可以更加简洁地遍历容器中的元素,而不必关心迭代器的细节。

范围for是基于迭代器实现的,也就是说有了迭代器我们就可以使用范围for了。

	string str = "hello world";
	
	for (auto c : str)
	{
		cout << c << ' ';
	}
	cout << endl;

image.png

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。