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

运行结果:

相关推荐
夜晚中的人海10 分钟前
【C++】位运算算法习题
开发语言·c++·算法
枫叶丹440 分钟前
【Qt开发】布局管理器(一)-> QVBoxLayout垂直布局
开发语言·c++·qt
superior tigre1 小时前
(huawei)5.最长回文子串
c++·算法
Qt程序员1 小时前
C++ 虚函数的使用开销以及替代方案
c++·c++设计模式·c/c++·c++虚函数
feng_blog66881 小时前
环形缓冲区实现共享内存
linux·c++
Larry_Yanan1 小时前
QML学习笔记(四十七)QML与C++交互:上下文对象
c++·笔记·qt·学习·ui
黑菜钟2 小时前
代码随想录第53天 | 图论二三题
c++·图论
西哥写代码2 小时前
基于dcmtk的dicom工具 第十二章 响应鼠标消息实现图像的调窗、缩放、移动
c++·mfc·dicom·dcmtk·vs2017
头发还没掉光光2 小时前
Linux多线程之生产消费模型,日志版线程池
linux·运维·开发语言·数据结构·c++
lkx097883 小时前
笔记C++语言,太焦虑了
前端·c++·笔记