【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];
	}
}

运行结果:

相关推荐
小菜鸡桃蛋狗1 小时前
C++——类和对象(下)
开发语言·c++
crescent_悦1 小时前
C++:Highest Price in Supply Chain
开发语言·c++
feng_you_ying_li1 小时前
底层实现map和set的第一步,AVL树的学习
c++
垫脚摸太阳1 小时前
第 36 场 蓝桥·算法挑战赛·百校联赛---赛后复盘
数据结构·c++·算法
Aaswk1 小时前
刷题笔记(回溯算法)
数据结构·c++·笔记·算法·leetcode·深度优先·剪枝
zhooyu2 小时前
GLM中lerp实现线性插值
c++·opengl
我不是懒洋洋2 小时前
预处理详解
c语言·开发语言·c++·windows·microsoft·青少年编程·visual studio
计算机安禾2 小时前
【数据结构与算法】第14篇:队列(一):循环队列(顺序存储
c语言·开发语言·数据结构·c++·算法·visual studio
IT从业者张某某2 小时前
基于EGE19.01完成恐龙跳跃游戏-V00-C++使用EGE19.01这个轮子
c++·游戏
-许平安-3 小时前
MCP项目笔记六(PluginsLoader)
c++·笔记·raii·plugin system