【C++】string 类 ( 上)

标准库中的string类

注意:

  1. string是表示字符串的字符串类

  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。 比特就业课

  3. string在底层实际是:basic_string模板类的别名,typedef basic_string string;

  4. 不能操作多字节或者变长字符的序列。 在使用string类时,必须包含#include头文件(#include<string>)以及using namespace std;

a. string类对象的常见构造

代码举例1

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1; // 相当于类对象的实例化
}

代码举例2

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1("hello world"); // 调用构造函数
	cout << t1 << endl;
	string t2 = "hello world"; //隐式类型转换(构造函数 + 拷贝构造 + 优化 -> 构造函数)
	cout << t2 << endl;
}

代码举例3

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1(10, 'a');  // 拷贝 10 个 a
	cout <<  t1 << endl;
}

运行结果:

代码举例4

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1("hello");
	string t2(t1); // 拷贝构造
	cout << t2 << endl;
}

b. string类对象的容量操作

  • size (返回字符串有效字符长度,没有 '\0 ')

代码举例1

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << t1.size() << endl;
}

运行结果:

  • capacity (返回字符串的总空间大小)

代码举例2

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << t1.capacity() << endl;
}

运行结果:

分析:

string 类里面的成员变量有两个可以存储空间,一个是数组,另一个是动态开辟的空间,当数组空间不足时,才会用动态开辟

  • reserve(扩大字符串容量,字符有效长度不变:即 size 不变)

代码举例3

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.reserve(100);
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
}

运行结果:

分析:

有些编译器在分配空间的时候,可能会对于开辟所需的空间再给大一点

  • resize (将有效字符的个数该成n个,多出的空间用字符c填充)

代码举例4

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(100);
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(10); //可以缩小有效长度,但总容量不会随意变动
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(20, '*'); //对于的空间可以初始化任意字符
	cout << t1 << endl;
}

运行结果:

c. string类对象的访问及遍历操作

  • operator[] (返回pos位置的字符,和 C 语言的用法一样,const string类对象调用)
  • begin + end (begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器)

代码举例1

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	string::iterator it = t1.begin();
	// it 相当于拿到 首元素的地址了
	while (it != t1.end())
	{
		cout << *it << endl;
		it++;
	}
}

运行结果:

分析:

  • rbegin + rend(rbegin获取最后一个字符的迭代器 + rend获取第一个字符前一个位置的迭代器)

代码举例2

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	string::reverse_iterator rit = t1.rbegin();
	// it 相当于拿到 首元素的地址了
	while (rit != t1.rend())
	{
		cout << *rit << endl;
		rit++;
	}
}

运行结果:

分析:

  • 范围for

代码举例3

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	for (auto i : t1)
	{
		cout << i;
	}
	cout << endl;
	for (int i = 0; i < t1.size(); i++)
	{
		cout << t1[i];
	}
}

运行结果:

相关推荐
感哥3 小时前
C++ STL 常用算法
c++
saltymilk13 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥13 小时前
C++ lambda 匿名函数
c++
沐怡旸19 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥20 小时前
C++ 内存管理
c++
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++
沐怡旸2 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4162 天前
Javer 学 c++(十三):引用篇
c++·后端