【本节目标】
1. 为什么要学习string类
2. 标准库中的string类
3. string类的模拟实现
4. 扩展阅读
1.为什么学习string****类?
1.1 C****语言中的字符串
C 语言中,字符串是以 '\0' 结尾的一些字符的集合,为了操作方便, C 标准库中提供了一些 str 系列的库函数, 但是这些库函数与字符串是分离开的,不太符合OOP 的思想,而且底层空间需要用户自己管理,稍不留神可 能还会越界访问。
2.标准库中的string****类
2.1 string类(了解)
string类的文档**:**cplusplus.com/reference/string/string/?kw=string
string 类的文档介绍
- 字符串是表示字符序列的类
- 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作 单字节字符字符串的设计特性。
- string 类是使用 char( 即作为它的字符类型,使用它的默认 char_traits 和分配器类型 ( 关于模板的更多信 息,请参阅basic_string) 。
- string 类是 basic_string 模板类的一个实例,它使用 char 来实例化 basic_string 模板类,并用 char_traits 和allocator 作为 basic_string 的默认参数 ( 根于更多的模板信息请参考 basic_string) 。
- 注意,这个类独立于所使用的编码来处理字节 : 如果用来处理多字节或变长字符 ( 如 UTF-8) 的序列,这个 类的所有成员( 如长度或大小 ) 以及它的迭代器,将仍然按照字节 ( 而不是实际编码的字符 ) 来操作。
总结: - string 是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作 string 的常规操作。
- string 在底层实际是: basic_string 模板类的别名, typedef basic_string<char, char_traits, allocator> string;
- 不能操作多字节或者变长字符的序列。
在 使用 string 类时,必须包含 #include 头文件以及 using namespace std ;
2.2 string****类的常用接口说明(注意下面我只讲解最常用的接口)
- string****类对象的常见构造

string构造函数的使用:
cpp
string s1;

cpp
string s2("hello");

cpp
string s4(s2);

cpp
string s3(10,'*');

cpp
string s2("hello");
string s5(s2,2,2);
cout << s2 << endl;
cout << s5 << endl;


npos这个值是一个静态成员变量后面还有一个-1的缺省值,很多人可能好奇为什么缺省值给-1大家别忘了这是一个无符号类型无符号类型-1就是整型的最大值。意味着如果你不写第三个参数,那么这个函数会把pos位置后面的字符全部拿走。
当然这里能用流插入的原因是string这个库的非成员函数重载了流插入符号。

同时这个文档里也有对其函数的讲解
这里我们可以再看三个函数
push_back只能尾插单个字符,这时候我们就能使用append函数,它既可以尾插单个字符也能尾插字符串。
当然我们也能用一个运算符重载
cpp
int main()
{
string s1("hello");
s1.push_back(' ');
s1.append("world");
cout << s1 << endl;
string s2("hello");
s2 += ' ';
s2 += "world";
cout << s2 << endl;
return 0;
}
2. string 类对象的容量操作
注意:
- size() 与 length() 方法底层实现原理完全相同,引入 size() 的原因是为了与其他容器的接口保持一
致,一般情况下基本都是用 size() 。 - clear() 只是将 string 中有效字符清空,不改变底层空间大小。
- resize(size_t n) 与 resize(size_t n, char c) 都是将字符串中有效字符个数改变到 n 个,不同的是当字 符个数增多时:resize(n) 用 0 来填充多出的元素空间, resize(size_t n, char c) 用字符 c 来填充多出的 元素空间。注意:resize 在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变(但是会删除里面的数据,例如:resize(20),就只要前20的数据,但是不会改变空间大小)。
- reserve(size_t res_arg=0) :为 string 预留空间,不改变有效元素个数,当 reserve 的参数小于
string 的底层空间总大小时, reserver 不会改变容量大小,如果用clear进行的数据清除,那么当给的reserve参数小于已经扩容空间的大小,就会进行缩容,简单来说就是有数据就不会缩容,没数据就可能会缩容。在vs2019下,会比你指定的空间大一些。在Linux下一般指定多少给多少。
5.capacity()会扩容,扩容方式会不一样,在vs2019先是2倍扩容,后面是1.5倍,在Linux下一直是2倍扩容。 - string 类对象的访问及遍历操作
注:operator[ ]有断言检查数组越界的错误。 - string 类对象的修改操作
注意: - 在 string 尾部追加字符时, s.push_back(c) / s.append(1, c) / s += 'c' 三种的实现方式差不多,一般情况下string 类的 += 操作用的比较多, += 操作不仅可以连接单个字符,还可以连接字符串。
- 对 string 操作时,如果能够大概预估到放多少字符,可以先通过 reserve 把空间预留好。
- string 类非成员函数
- vs 和 g++ 下 string 结构的说明
注意:下述结构是在 32 位平台下进行验证, 32 位平台下指针占 4 个字节。
vs 下 string 的结构
string 总共占 28 个字节 ,内部结构稍微复杂一点,先是 有一个联合体,联合体用来定义 string 中字
符串的存储空间 :
当字符串长度小于 16 时,使用内部固定的字符数组来存放
当字符串长度大于等于 16 时,从堆上开辟空间
cpp
union _Bxty
{ // storage for small buffer or pointer to larger one
value_type _Buf[_BUF_SIZE];
pointer _Ptr;
char _Alias[_BUF_SIZE]; // to permit aliasing
} _Bx;
这种设计也是有一定道理的,大多数情况下字符串的长度都小于 16 ,那 string 对象创建好之后,内
部已经有了 16 个字符数组的固定空间,不需要通过堆创建,效率高。
其次:还有 一个 size_t 字段保存字符串长度,一个 size_t 字段保存从堆上开辟空间总的容量
最后:还 有一个指针 做一些其他事情。
故总共占 16+4+4+4=28 个字节。
g++ 下 string 的结构
G++ 下, string 是通过写时拷贝实现的, string 对象总共占 4 个字节,内部只包含了一个指针,该指
针将来指向一块堆空间,内部包含了如下字段:
空间总大小
字符串有效长度
引用计数
cpp
struct _Rep_base
{
size_type _M_length;
size_type _M_capacity;
_Atomic_word _M_refcount;
};
指向堆空间的指针,用来存储字符串。
迭代器的了解:
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello world");
//迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
return 0;
}
可以看到迭代器与指针十分相像。
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello world");
//迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
(*it)--;
++it;
}
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
return 0;
}
同时它也能通过解引用修改里面的数据。
当然迭代器的意义不止于此
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("hello world");
//迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
(*it)--;
++it;
}
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//范围for
//底层替换成迭代器
for (auto& ch : s1)
{
ch++;
}
for (auto ch : s1)
{
cout << ch << " ";
}
cout << endl;
return 0;
}
范围for的底层就会替换成迭代器,想要支持范围for就要先支持迭代器。
cpp
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
int main()
{
string s1("hello world");
//迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
(*it)--;
++it;
}
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//范围for
//底层替换成迭代器
for (auto& ch : s1)
{
ch++;
}
for (auto ch : s1)
{
cout << ch << " ";
}
cout << endl;
//迭代器还支持容器
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator vit = v.begin();
while (vit != v.end())
{
cout << *vit << " ";
++vit;
}
cout << endl;
list<int> lt;
lt.push_back(10);
lt.push_back(20);
lt.push_back(3);
lt.push_back(4);
list<int>::iterator lit = lt.begin();
while (lit != lt.end())
{
cout << *lit << " ";
++lit;
}
cout << endl;
return 0;
}

3. string****类的模拟实现
3.1经典的string****类问题
上面已经对 string 类进行了简单的介绍,大家只要能够正常使用即可。在面试中,面试官总喜欢让学生自己 来模拟实现string 类,最主要是实现 string 类的构造、拷贝构造、赋值运算符重载以及析构函数。大家看下以 下string 类的实现是否有问题?
cpp
// 为了和标准库区分,此处使用String
class String
{
public:
/*String()
:_str(new char[1])
{*_str = '\0';}
*/
//String(const char* str = "\0") 错误示范
//String(const char* str = nullptr) 错误示范
String(const char* str = "")
{
// 构造String类对象时,如果传递nullptr指针,可以认为程序非
if (nullptr == str)
{
assert(false);
return;
}
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
// 测试
void TestString()
{
String s1("hello bit!!!");
String s2(s1);
}
说明:上述 String 类没有显式定义其拷贝构造函数与赋值运算符重载,此时编译器会合成默认的,当用 s1 构 造 s2 时,编译器会调用默认的拷贝构造。最终导致的问题是, s1 、 s2 共用同一块内存空间,在释放时同一块 空间被释放多次而引起程序崩溃 ,这种拷贝方式,称为浅拷贝。
3.2****浅拷贝
浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来 。如果 对象中管理资源 ,最后就会 导致多个对象共 享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为 还有效,所以当继续对资源进项操作时,就会发生发生了访问违规 。
就像一个家庭中有两个孩子,但父母只买了一份玩具,两个孩子愿意一块玩,则万事大吉,万一不想分享就 你争我夺,玩具损坏。
可以采用深拷贝解决浅拷贝问题,即: 每个对象都有一份独立的资源,不要和其他对象共享 。父母给每个孩 子都买一份玩具,各自玩各自的就不会有问题了
3.3****深拷贝
如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情 况都是按照深拷贝方式提供。
3.3.1传统版写法的String****类
cpp
class String
{
public:
String(const char* str = "")
{
// 构造String类对象时,如果传递nullptr指针,可以认为程序非
if (nullptr == str)
{
assert(false);
return;
}
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
String(const String& s)
: _str(new char[strlen(s._str) + 1])
{
strcpy(_str, s._str);
}
String& operator=(const String& s)
{
if (this != &s)
{
char* pStr = new char[strlen(s._str) + 1];
strcpy(pStr, s._str);
delete[] _str;
_str = pStr;
}
return *this;
}
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
3.3.2现代版写法的String****类
cpp
class String
{
public:
String(const char* str = "")
{
if (nullptr == str)
{
assert(false);
return;
}
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
String(const String& s)
: _str(nullptr)
{
String strTmp(s._str);
swap(_str, strTmp._str);
}
// 对比下和上面的赋值那个实现比较好?
String& operator=(String s)
{
swap(_str, s._str);
return *this;
}
/*
String& operator=(const String& s)
{
if(this != &s)
{
String strTmp(s);
swap(_str, strTmp._str);
}
return *this;
}
*/
~String()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
}
3.3写时拷贝(了解)
写时拷贝就是一种拖延症,是在浅拷贝的基础之上增加了引用计数的方式来实现的。
引用计数:用来记录资源使用者的个数。在构造时,将资源的计数给成 1 ,每增加一个对象使用该资源,就给 计数增加1 ,当某个对象被销毁时,先给该计数减 1 ,然后再检查是否需要释放资源,如果计数为 1 ,说明该 对象时资源的最后一个使用者,将该资源释放;否则就不能释放,因为还有其他对象在使用该资源。
string类的实现
cpp
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace xyl
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
public:
iterator begin()
{
return _str;
}
iterator end()
{
return (_str + _size);
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return (_str + _size);
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
//strcpy(tmp, _str);
memcpy(tmp, _str, _size + 1);
_capacity = n;
delete[] _str;
_str = tmp;
}
}
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char [_capacity+1];
//strcpy(_str, str);
memcpy(_str, str, _size + 1);
}
string(const string& s)
{
_size = s._size;
_capacity = s._capacity;
_str = new char[s._capacity + 1];
// strcpy(_str, s._str);
memcpy(_str, s._str, s._size + 1);
}
string& operator= (const string& s)
{
if (*this != s)
{
_size = s._size;
_capacity = s._capacity;
_str = new char[s._capacity + 1];
//strcpy(_str, s._str);
memcpy(_str, s._str, s._size + 1);
}
return *this;
}
~string()
{
_size = 0;
_capacity = _size;
delete[]_str;
}
void push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? _capacity = 4 : _capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
_capacity = _size;
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
//strcpy(_size + _str, str);
memcpy(_size + _str, str, len+1);
_size += len;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(string& s)
{
string tmp(s);
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
const char* c_str()const
{
return _str;
}
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t n, char c = '\0')
{
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
reserve(n);
for (size_t i = _size;i < n;i++)
{
_str[i] = c;
}
_str[n] = '\0';
_size = n;
}
}
access
char& operator[](size_t index)
{
return _str[index];
}
const char& operator[](size_t index)const
{
return _str[index];
}
//relational operators
bool operator<(const string& s)
{
size_t i1= 0;
size_t i2 = 0;
while (i1 < _size && i2 < s._size)
{
if (_str[i1] < s._str[i2])
{
return true;
}
else if(_str[i1]>s._str[i2])
{
return false;
}
else
{
i1++;
i2++;
}
}
// "hello" "hello" false
// "helloxx" "hello" false
// "hello" "helloxx" true
if (i1 == _size && i2 != s._size)
{
return true;
}
else
{
return false;
}
}
bool operator==(const string& s)
{
return _size == s._size
&& memcmp(_str, s._str, _size) == 0;
}
bool operator<=(const string& s)
{
return *this < s || *this == s;
}
bool operator>(const string& s)
{
return !(*this <= s);
}
bool operator>=(const string& s)
{
return *this > s || *this == s;
}
bool operator!=(const string& s)
{
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return npos;
}
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const
{
assert(pos < _size);
const char* ptr = strstr(_str + pos, s);
if (ptr)
{
return ptr - _str;
}
else
{
return npos;
}
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
void insert(size_t pos, size_t n, char ch)
{
assert(pos <= _size);
if (_size + n > _capacity)
{
// 至少扩容到_size + len
reserve(_size + n);
}
// 挪动数据
/*int end = _size;
while (end >= (int)pos)
{
_str[end + n] = _str[end];
--end;
}*/
// 添加注释最好
size_t end = _size;
while (end >= pos && end != npos)
{
_str[end + n] = _str[end];
--end;
}
for (size_t i = 0; i < n; i++)
{
_str[pos + i] = ch;
}
_size += n;
}
string& insert(size_t pos, char c)
{
assert(pos<=_size);
if (_size + 1 > _capacity)
{
reserve(_capacity + 1);
}
size_t end = _size;
while (end >= pos && end != npos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = c;
_size += 1;
return *this;
}
string& insert(size_t pos, const char* str)
{
size_t n = strlen(str);
assert(pos <= _size);
if (_size + n > _capacity)
{
reserve(_size + n);
}
size_t end = _size;
while (end>=pos&&end!=npos)
{
_str[end + n] = _str[end];
end--;
}
for (int i = 0;i < n;i++)
{
_str[pos + i] = str[i];
}
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len=npos)
{
assert(pos <= _size);
if (len == npos || len + pos >= _size)
{
_str[pos] = '\0';
}
else
{
size_t end = pos + len;
while (end <= _size)
{
_str[pos++] = _str[end++];
}
_size -= len;
}
}
private:
char* _str;
size_t _capacity;
size_t _size;
public:
const static size_t npos;
};
const size_t string::npos = -1;
};
ostream& operator<<(ostream& out, const xyl::string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, xyl::string& s)
{
s.clear();
char ch = in.get();
while (ch == ' ' || ch == '\n')
{
ch = in.get();
}
char buff[128] = {'\0'};
int i = 0;
while (buff[i] != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == 127)
{
buff[i] = '\0';
s += buff;
int i = 0;
}
ch = in.get();
}
if (i != 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}