在C语言中我们常使用char*字符串进行相关的操作,在使用字符串时,在学习C++时,标准库为我们提供了专门的string类的相关接口。抛弃了char*字符串的相关用法,是因为和标准库相较,我们在使用时不必考虑内存是否足够、字符串长度等等,并且作为了一个类出现,它所提供的相关函数的操作满足了我们大多数情况下的需求,我们可以简单的理解为string类是一种C++的基本数据类型。
标准库的String类
- 使用时包含在头文件
<string>中; - string是表示字符串的字符串类;
- 标准的字符串类提供了对此类对象的支持,其接口类似于字符容器的接口,但其中专门设置了操作字符串的常规操作;
- string在底层实际上是:
basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>string;
在string类中,我们又将这些操作字符串的常规操作分为以下几类:
①string类型对象的常见构造;
②容量操作;
③元素的访问以及遍历操作;
④string类对象的修改操作;
⑤一些特殊操作
因为string类中,包含了多种的函数操作,我们在使用时如果忘记了该如何定义,我们可以在手册中进行查询使用
点击这里,就可以查看string类中提供的各类函数
string类的常见接口介绍
1.string类型对象的常见构造
| 函数名称 | 功能说明 |
|---|---|
| string() | 构造空的字符串 |
| string(const char* s) | 构造C格式的字符串 |
| string(const string &s) | 拷贝构造函数 |
| string(size_t n,char c) | 用n个字符构造一个string类型的对象 |
void StringTest1()
{
string s1;
string s2("No pains,no gains");
string s3(10, 'a');
string s4(s2);
string s5(s2, 0, 3);
string s6("hello", 3);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
return 0;
}

2.容量操作
| 函数名称 | 功能说明 |
|---|---|
| size | 字符串的有效长度 |
| length | 字符串的有效长度 |
| capacity | 获取string对象底层空间的实际大小 |
| empty | 检测字符串释放为空串,是返回true,否则返回false |
| clear | 清空有效字符 |
| reserve | 对字符串进行扩容操作 |
| resize | 将有效字符的个数改成n个,多出的空间用字符c填充 |
void StringTest2()
{
string s("No pains,no gains");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
if (s.empty())
{
cout << "string is empty!" << endl;
}
else
cout << "string is not empty!" << endl;
s.clear();
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
return 0;
}

- resize
void resize (size_t n);
void resize (size_t n, char c);
resize函数主要有两个功能,将字符个数进行扩充和在扩充的同时用字符进行填充,如果我们不指定填充的字符,系统将会默认采用\0进行填充
void StringTest3()
{
string s("hello");
s.resize(10,'h');//将字符s的有效字符扩增至10个,多余部分用字符h进行填充
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(20);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(30);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(20);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(10);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(5);
cout << s.length() << endl;
cout << s.capacity() << endl;
}

当将有效字符减少时,我们发现:

我们可以从运行的结果清楚的看的字符的填充情况:
-
当我们减少有效字符的个数时,并不会改变底层大小空间,仅仅只是将有效个数进行减少;
-
将有效元素个数增多时:①newsize>oldcapacity,newsize<=oldcapacity,直接将字符追加至有效字符后;
②newsize>oldcapacity,底层空间会按照顺序表的方式进行扩容; -
reserve
void reserve (size_t n = 0);
void StringTest4()
{
string s("hello");
s.reserve(10);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(15);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(20);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(30);
cout << s.length() << endl;
cout << s.capacity() << endl;
//将底层空间进行缩小
s.reserve(25);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(15);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(10);
cout << s.length() << endl;
cout << s.capacity() << endl;
}
运行结果如下图:

- 当缩小底层空间大小时(也不会更改底层空间的大小):
①newcapacity>15 ,直接忽略不进行改变;
②newcapacity<=15,将底层空间容量缩小至15个; - 当扩大底层空间容量时:
将底层空间容量扩大至newcapacity个;
为什么15是一个临界值呢?
在string类中,为了提高性能在其内部设置管理了一个固定的数组,该数组的空间大小为最大为16,当string类在进行构造时,当容量小于16时将直接使用静态的数组,容量不够时,将采取动态开辟容量。
3.元素的访问以及遍历操作
| 函数名称 | 功能说明 |
|---|---|
| operator[] | 返回pos位置的字符,const string类对象调用 |
| begin+end | begin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器 |
| rbegin+rend | rbegin |
| 范围for | C++支持更简洁的范围for的新遍历方式 |
void StringTest5()
{
string s("No pains,No gains.");
//1.for+operator[]
for (size_t i = 0; i < s.length(); ++i){
cout << s[i] << endl;
}
//2.迭代器
string::iterator it = s.begin();
while (it != s.end()){
cout << *it << endl;
++*it;
}
//3.基于范围for
for (auto ch : s){
cout << ch << endl;
}
}
4.string类对象的修改操作
| 函数名称 | 功能说明 |
|---|---|
| push_back | 在字符串后尾插字符c |
| append | 在字符串后追加一个字符串 |
| operator+= | 在字符串后追加字符串str |
| c_str | 返回C格式的字符串 |
| find+npos | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
| rfind | 从字符串pos位置开始向前找字符c,返回该字符串在字符串中的位置 |
void StringTest6()
{
string s("hello");
s += " ";//在s后追加空格
s += 'w';//在s后追加一个字符'w'
s += "orl";//在s后追加字符串"orl"
s.push_back('d');//在s后插入字符'd'
}
5.一些特殊操作
| 函数名称 | 功能说明 |
|---|---|
| operator+ | 尽量少用,因为传值返回,导致深拷贝效率低 |
| operator>> | 输入运算符重载 |
| operator<< | 输出运算符重载 |
| getline | 获取一行字符串 |
| relational operators | 大小比较 |
本文详细介绍了C++标准库中的string类,包括string类型的构造、容量操作、元素访问及遍历、对象修改等常见操作,以及一些特殊操作。
1026

被折叠的 条评论
为什么被折叠?



