当前位置:   article > 正文

从C语言到C++_13(string的模拟实现)深浅拷贝+传统/现代写法_c++13

c++13

目录

1.  string默认成员函数

1.1 构造和析构

1.2 深浅拷贝介绍

1.3 拷贝构造的实现

1.4 赋值的实现

1.5 写时拷贝(了解)

2. string 的部分函数实现

2.1 完整默认成员函数代码:

2.2 c_str() 的实现

2.3 全缺省构造函数的实现

2.4 size() 和 operator[] 的实现

3. string的迭代器

3.1 string迭代器的实现

3.2 迭代器和范围for再思考

4. string的增删查改函数实现

4.1 reserve() 的实现

4.2 push_back() 的实现

4.3 append() 的实现

4.4 operator+= 的实现

4.5 insert() 的实现

4.6 resize() 的实现

4.7 find() 的实现

4.8 erase() 的实现

5. 传统写法和现代写法

5.1 拷贝构造的现代写法

5.2 赋值重载的现代写法

6. operator 运算符重载

6.1 六个比较运算符重载

6.2 流插入和流提取重载

7. 完整代码:

string.h:

Test.c:

本章完。


前两篇博客已经对string类进行了简单的介绍和应用,大家只要能够正常使用即可。

在面试中,面试官总喜欢让学生自己来模拟实现string类,

最主要是实现string类的构造、拷贝构造、赋值运算符重载以及析构函数。

为了更深入学习STL,下面我们就自己来模拟实现一下string的常用接口函数:


1.  string默认成员函数

1.1 构造和析构

我们先试着来实现 string 的构造和析构:

整体框架:

string.h

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. #include<assert.h>
  5. using namespace std;
  6. namespace rtx
  7. {
  8. class string
  9. {
  10. public:
  11. string(const char* s)
  12. {
  13. }
  14. ~string()
  15. {
  16. }
  17. private:
  18. char* _str;
  19. };
  20. void test_string1()
  21. {
  22. string s1("hello world");
  23. }
  24. }

Test.c:

  1. #include "string.h"
  2. int main()
  3. {
  4. try
  5. {
  6. rtx::test_string1();
  7. }
  8. catch (const exception& e)
  9. {
  10. cout << e.what() << endl;
  11. }
  12. return 0;
  13. }

这里为了和原有的 string 进行区分,我们搞一个命名空间给它们括起来。

我们的测试就放在简单提到的try catch上,然后该序号就能测试了。

构造函数是这样写吗?这样写的话拷贝构造能直接用默认生成的吗

  1. string(const char* s)
  2. : _str(new char[strlen(s) + 1])// 开strlen大小的空间(多开一个放\0)
  3. {
  4. strcpy(_str, str);
  5. }

然后我们先实现析构,用 new[] 对应的 delete[] 来析构:

  1. ~string()
  2. {
  3. delete[] _str;
  4. _str = nullptr;
  5. }

放到上面的框架:编译通过

此时我们改一下测试用例 test_string1,如果我们要用 s1 拷贝构造一下 s2:

 详细解析:

说明:上述string类没有显式定义其拷贝构造函数与赋值运算符重载,此时编译器会合成默认的,当用 s1 s2 时,编译器会调用默认的拷贝构造。最终导致的问题是, s1 s2 共用同一块内存空间,在释放时同一块 空间被释放多次而引起程序崩溃 这种拷贝方式,称为浅拷贝

1.2 深浅拷贝介绍

如何解决这样的问题呢?

我们 s2 拷贝构造你 s1,本意并不是想跟你指向一块空间!

我们的本意是想让 s2 有一块自己的空间,并且能使其内容是 s1 里的 hello world

这就是深拷贝。

 所以这里就涉及到了深浅拷贝的问题,我们下面就来探讨一下深浅拷贝的问题。

浅拷贝:(直接把内存无脑指过去) 也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为还有效,所以当继续对资源进项操作时,就会发生发生了访问违规。

深拷贝(开一块一样大的空间,再把数据拷贝下来,指向我自己开的空间) 如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。

1.3 拷贝构造的实现

 我们之前实现日期类的时候,用自动生成的拷贝构造(浅拷贝)是可以的,

所以当时我们不用自己实现拷贝构造,让它默认生成就足够了。

但是像 string 这样的类,它的拷贝构造我们不得不亲自写:

  1. string(const string& s)
  2. :_str(new char[s._capacity + 1])
  3. {
  4. strcpy(_str, s._str);
  5. }

 这就实现了深拷贝。

1.4 赋值的实现

 现在有一个 s3,如果我们想把 s3 赋值给 s1:

  1. void test_string1()
  2. {
  3. string s1("hello world");
  4. string s2(s1);
  5. string s3("!!!");
  6. s1 = s3;
  7. }

如果你不自己实现赋值,就和之前一样,会是浅拷贝,也会造成崩溃。

所以,我们仍然需要自己实现一个 operator= ,首先思路如下:

  1. string& operator=(const string& s)
  2. {
  3. if (this != &s)
  4. {
  5. delete[] _str;// 释放原有空间
  6. _str = new char[s._capacity + 1];// 开辟新的空间
  7. strcpy(_str, s._str);// 赋值
  8. _size = s._size;
  9. _capacity = s._capacity;
  10. }
  11. return *this;
  12. }

根据我们的实现思路,首先释放原有空间,然后开辟新的空间,

最后把 s3 的值赋值给 s1。为了防止自己给自己赋值,我们可以判断一下。

这时我们还要考虑一个难以发现的问题,如果 new 失败了怎么办?

抛异常!失败了没问题,也不会走到 strcpy,但问题是我们已经把原有的空间释放掉了,

走到析构那里二次释放可能会崩,所以我们得解决这个问题。

可以试着把释放原有空间的步骤放到后面:

  1. string& operator=(const string& s)
  2. {
  3. if (this != &s)
  4. {
  5. char* tmp = new char[s._capacity + 1];// 开辟新的空间
  6. strcpy(tmp, s._str);// 赋值到tmp
  7. delete[] _str;// 释放原有空间
  8. _str = tmp;// tmp赋值到想要的地方,出去tmp就销毁了
  9. _size = s._size;
  10. _capacity = s._capacity;
  11. }
  12. return *this;
  13. }

这样一来,就算是动态内存开辟失败了,我们也不用担心出问题了。

这是更标准的实现方式,我们先去开辟空间,放到临时变量 tmp 中,tmp 翻车就不会执行下面的代码,tmp 没有翻车,再去释放原有的空间,最后再把 tmp 的值交付给 s1,

这是非常保险的,有效避免了空间没开成还把 s1 空间释放掉的 "偷鸡不成蚀把米" 的事发生。

1.5 写时拷贝(了解)

写时拷贝就是一种拖延症,是在浅拷贝的基础之上增加了引用计数的方式来实现的。
引用计数:用来记录资源使用者的个数。在构造时,将资源的计数给成 1 ,每增加一个对象使用该资源,就给计数增加1 ,当某个对象被销毁时,先给该计数减 1 ,然后再检查是否需要释放资源,如果计数为 1 ,说明该对象时资源的最后一个使用者,将该资源释放;否则就不能释放,因为还有其它对象在使用该资源。

写时拷贝技术实际上是运用了一个 “引用计数” 的概念来实现的。在开辟的空间中多维护四个字节来存储引用计数。
有两种方法:
①:多开辟四个字节(pCount)的空间,用来记录有多少个指针指向这片空间。
②:在开辟空间的头部预留四个字节的空间来记录有多少个指针指向这片空间。
  当我们多开辟一份空间时,让引用计数+1,如果有释放空间,那就让计数-1,但是此时不是真正的释放,是假释放,等到引用计数变为 0 时,才会真正的释放空间。如果有修改或写的操作,那么也让原空间的引用计数-1,并且真正开辟新的空间。

写时拷贝涉及多线程等不好的问题,所以了解一下就行。

2. string 的部分函数实现

刚才我们为了方便讲解深浅拷贝的问题,有些地方所以没有写全。

我们知道string有这几个接口函数:

我们实现只是实现常用的,且length和size是一样的,我们现在增加一些成员:

  1. private:
  2. char* _str;
  3. size_t _size;
  4. size_t _capacity; // 有效字符的空间数,不算\0

2.1 完整默认成员函数代码:

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. #include<assert.h>
  5. using namespace std;
  6. namespace rtx
  7. {
  8. class string
  9. {
  10. public:
  11. string(const char* s)
  12. {
  13. _size =strlen(s);// 因为要算多次strlen 效率低 且放在初始化列表关联到声明顺序 所以不用初始化列表
  14. _capacity = _size;
  15. _str = new char[_size + 1];// 开_size+1大小的空间(多开一个放\0)
  16. strcpy(_str, s);
  17. }
  18. string(const string& s)
  19. :_str(new char[s._capacity + 1])
  20. , _size(s._size)
  21. , _capacity(s._capacity)
  22. {
  23. strcpy(_str, s._str);
  24. }
  25. string& operator=(const string& s)
  26. {
  27. if (this != &s)
  28. {
  29. char* tmp = new char[s._capacity + 1];// 开辟新的空间
  30. strcpy(tmp, s._str);// 赋值到tmp
  31. delete[] _str;// 释放原有空间
  32. _str = tmp;// tmp赋值到想要的地方,出去tmp就销毁了
  33. _size = s.size();
  34. _capacity = s._capacity;
  35. }
  36. return *this;
  37. }
  38. ~string()
  39. {
  40. delete[] _str;
  41. _str = nullptr;
  42. }
  43. private:
  44. char* _str;
  45. size_t _size;
  46. size_t _capacity;
  47. };
  48. void test_string1()
  49. {
  50. string s1("hello world");
  51. string s2(s1);
  52. string s3("!!!");
  53. s1 = s3;
  54. }
  55. }

2.2 c_str() 的实现

 c_str() 返回的是C语言字符串的指针常量,是可读不写的:

  1. const char* c_str() const
  2. {
  3. return _str;
  4. }

返回const char*,因为是可读不可写的,所以我们需要用 const 修饰。

c_str 返回的是当前字符串的首字符地址,这里我们直接 return _str 即可实现。

测试一下:

  1. void test_string1()
  2. {
  3. string s1("hello world");
  4. string s2(s1);
  5. string s3("!!!");
  6. s1 = s3;
  7. cout << s1.c_str() << endl;
  8. cout << s2.c_str() << endl;
  9. cout << s3.c_str() << endl;
  10. }

2.3 全缺省构造函数的实现

还要考虑不带参的情况,比如下面的 s4:

  1. void test_string1()
  2. {
  3. string s1("hello world");
  4. string s2(s1);
  5. string s3("!!!");
  6. s1 = s3;
  7. cout << s1.c_str() << endl;
  8. cout << s2.c_str() << endl;
  9. cout << s3.c_str() << endl;
  10. string s4;
  11. }

无参构造函数:

  1. string()
  2. : _str(new char[1])
  3. , _size(0)
  4. , _capacity(0)
  5. {
  6. _str[0] = '\0';
  7. }

一般的类都是提供全缺省的,值得注意的是,这里缺省值给的是 " "

有人看到指针 char* 可能给缺省值一个空指针 nullptr:

string(const char* str = nullptr)

也就相当于直接对这个字符串进行解引用了,这里的字符串又是空,所以会引发空指针问题。

所以我们这里给的是一个空的字符串 " ",常量字符串默认就带有 \0,这样就不会出问题:

  1. string(const char* s = "")
  2. {
  3. _size =strlen(s);// 因为要算多次strlen 效率低 且放在初始化列表关联到声明顺序 所以不用初始化列表
  4. _capacity = _size;
  5. _str = new char[_size + 1];// 开_size+1大小的空间(多开一个放\0)
  6. strcpy(_str, s);
  7. }

这样达到的效果和无参构造函数是一样的,且无参编译器不知道调用哪个,

所以我们就需把无参构造函数删了。

2.4 size() 和 operator[] 的实现

 size()的实现:

  1. size_t size() const
  2. {
  3. return _size;
  4. }

size() 只需要返回成员 _size 即可,考虑到不需要修改,我们加上 const。

operator[] 的实现:

  1. char& operator[](size_t pos)
  2. {
  3. assert(pos < _size);
  4. return _str[pos];
  5. }

直接返回字符串对应下标位置的元素,

因为返回的是一个字符,所以我们这里引用返回 char。

我们来测试一下,遍历整个字符串,这样既可以测试到 size() 也可以测试到 operator[] :

  1. void test_string2()
  2. {
  3. string s1("hello world");
  4. string s2;
  5. for (size_t i = 0; i < s1.size(); i++)
  6. {
  7. cout << s1[i] << " ";
  8. }
  9. cout << endl;
  10. s1[0] = 'x';
  11. for (size_t i = 0; i < s1.size(); i++)
  12. {
  13. cout << s1[i] << " ";
  14. }
  15. cout << endl;
  16. }

 普通对象可以调用,但是 const 对象呢?所以我们还要考虑一下 const 对象。

我们写一个 const 对象的重载版本:

  1. const char& operator[](size_t pos) const
  2. {
  3. assert(pos < _size);
  4. return _str[pos];
  5. }

因为返回的是 pos 位置字符的 const 引用,所以可读但不可写。

3. string的迭代器

在上上篇中,我们首次讲解迭代器,为了方便理解,我们当时解释其为像指针一样的类型。

实际上,有没有一种可能,它就是一种指针呢?

遗憾的是,迭代器并非指针,而是类模板。 只是它表现地像指针,模拟了指针的部分功能。

string迭代器的实现非常简单,它就是一个 char* 的指针罢了。

后面我们讲解 list 的时候它的迭代器又不是指针了,又是自定义类型了。

所以迭代器是一个像指针的东西,有可能是指针有可能不是指针。

3.1 string迭代器的实现

实现迭代器的 begin() 和 end() :

  1. typedef char* iterator;
  2. iterator begin()
  3. {
  4. return _str;
  5. }
  6. iterator end()
  7. {
  8. return _str + _size;
  9. }

 const 迭代器就是可以读但是不可以写的迭代器,实现一下 const 迭代器:

  1. typedef const char* const_iterator;
  2. const_iterator begin() const
  3. {
  4. return _str;
  5. }
  6. const_iterator end() const
  7. {
  8. return _str + _size;
  9. }

3.2 迭代器和范围for再思考

迭代器的底层是连续的物理空间,给原生指针++解引用能正好贴合迭代器的行为,能做到遍历。

但是对于链表和树型结构来说,迭代器的实现就没有这么简单了。

但是,强大的迭代器通过统一的封装,无论是树、链表还是数组……

它都能用统一的方式遍历,这就是迭代器的优势,也是它的强大之处。

我们上一章提到过范围for遍历string,我们能不能直接用在我们写的迭代器上:

  1. void test_string3()
  2. {
  3. string s1("hello world");
  4. string::iterator it = s1.begin();
  5. while (it != s1.end())
  6. {
  7. cout << *it << " ";// 读
  8. it++;
  9. }
  10. cout << endl;
  11. it = s1.begin();
  12. while (it != s1.end())
  13. {
  14. (*it)++;// 写,++的优先级比*高,+=就低,可以 *it += 1;
  15. cout << *it << " ";
  16. it++;
  17. }
  18. cout << endl;
  19. for (auto& e : s1)
  20. {
  21. cout << e << " ";
  22. }
  23. cout << endl;
  24. }

 我们也妹写范围 for 啊,怎么就能直接用了?

所以范围 for 根本就不需要自己实现,你只要把迭代器实现好,范围 for 直接就可以用。

范围 for 的本质是由迭代器支持的,编译时范围 for 会被替换成迭代器。

这么一看,又是自动加加,又是自动判断结束的范围 for,好像也没那么回事儿。

4. string的增删查改函数实现

4.1 reserve() 的实现

我们先实现一下 reserve 增容:

这里可以检查一下是否真的需要增容,万一接收的 new_capacity 比 _capacity 小,就不动。

  1. void reserve(size_t new_capacity)
  2. {
  3. if (new_capacity > _capacity)
  4. {
  5. char* tmp = new char[new_capacity + 1];// 开新空间
  6. strcpy(tmp, _str);// 搬运
  7. delete[] _str; // 释放原空间
  8. _str = tmp;// 没问题,递交给_str
  9. _capacity = new_capacity;// 更新容量
  10. }
  11. }

这里我们之前讲数据结构用的是 realloc,现在我们熟悉熟悉用 new,

 还是用申请新空间、原空间数据拷贝到新空间,再释放空间地方式去扩容。

我们的 _capacity 存储的是有效字符,没算 \0,所以这里还要 +1 为 \0 开一个空间。

4.2 push_back() 的实现

  1. void push_back(const char ch)
  2. {
  3. if (_size == _capacity)
  4. {
  5. reserve(_capacity == 0 ? 4 : _capacity * 2);
  6. }
  7. _str[_size++] = ch;// 在_size位置放字符后++
  8. _str[_size] = '\0';// 易漏
  9. }

首先检查是否需要增容,如果需要就调用我们上面实现的 reserve 函数,

参数传递可以用三目操作符,防止容量是0的情况,0乘任何数都是0从而引发问题的情况。

测试一下效果如何:

4.3 append() 的实现

append 是追加字符串的,首先我们把要追加的字符串长度计算出来,

然后看容量够不够,不够我们就交给 reserve 去扩容,扩 _size + len,够用就行。

  1. void append(const char* str)
  2. {
  3. int len = strlen(str);
  4. if (_size + len > _capacity)
  5. {
  6. reserve(_size + len);
  7. }
  8. strcpy(_str + _size, str);// 首字符+_size大小就是\0位置
  9. _size += len;
  10. }

这里我们甚至都不需要用 strcat,因为它的位置我们很清楚,不就在 _str + _size 后面插入吗。

用 strcat 还需要遍历找到原来位置的 \0,麻烦且效率低,strcat 函数我们以后尽量都不用。

4.4 operator+= 的实现

这就是我们一章说的 "用起来爽到飞起" 的 += ,因为字符和字符串都可以用 += 去操作。

所以我们需要两个重载版本,一个是字符的,一个是字符串的。

我们不需要自己实现了,直接复用 push_back 和 append 就好了:

  1. string& operator+=(const char ch)
  2. {
  3. push_back(ch);
  4. return *this;
  5. }
  6. string& operator+=(const char* str)
  7. {
  8. append(str);
  9. return *this;
  10. }

测试:

  1. void test_string4()
  2. {
  3. string s1("hello world");
  4. cout << s1.c_str() << endl;
  5. s1.push_back('!');
  6. cout << s1.c_str() << endl;
  7. s1.push_back('R');
  8. cout << s1.c_str() << endl;
  9. s1.append("abcd");
  10. cout << s1.c_str() << endl;
  11. s1 += 'e';
  12. s1 += "fgh";
  13. cout << s1.c_str() << endl;
  14. }

4.5 insert() 的实现

insert:字符

  1. string& insert(size_t pos, const char ch)
  2. {
  3. assert(pos < _size);
  4. if (_size == _capacity)
  5. {
  6. reserve(_capacity == 0 ? 4 : _capacity * 2);
  7. }
  8. for (size_t i = _size + 1;i > pos; --i)// 挪动数据,+1是挪动\0
  9. {
  10. _str[i] = _str[i - 1];
  11. }
  12. _str[pos] = ch;
  13. ++_size;
  14. return *this;
  15. }

insert:字符串

  1. string& insert(size_t pos, const char* str)
  2. {
  3. assert(pos <= _size);
  4. int len = strlen(str);
  5. if (_size + len > _capacity)
  6. {
  7. reserve(_size + len);
  8. }
  9. for (size_t i = _size + len ;i > pos + len - 1; --i)// 挪动数据,画图注意边界,参考上面inser字符的len == 1
  10. {
  11. _str[i] = _str[i - len];// 首先看\0 _size+len-len就是\0的位置
  12. }
  13. strncpy(_str + pos, str, len);
  14. _size += len;
  15. return *this;
  16. }

 测试:

  1. void test_string4()
  2. {
  3. string s1("hello world");
  4. cout << s1.c_str() << endl;
  5. s1.push_back('!');
  6. cout << s1.c_str() << endl;
  7. s1.push_back('R');
  8. cout << s1.c_str() << endl;
  9. s1.append("abcd");
  10. cout << s1.c_str() << endl;
  11. s1 += 'e';
  12. s1 += "fgh";
  13. cout << s1.c_str() << endl;
  14. s1.insert(0, 'x');
  15. s1.insert(6, 'T');
  16. cout << s1.c_str() << endl;
  17. s1.insert(6, "PPPPPPPPPP");
  18. cout << s1.c_str() << endl;
  19. s1.insert(0, "PPPPPPPPPP");
  20. cout << s1.c_str() << endl;
  21. }

 (测试后push_back 和 append 直接复用还能测试一波)

+=又是复用push_back 和 append 的,直接套娃开始:

  1. void reserve(size_t new_capacity)
  2. {
  3. if (new_capacity > _capacity)
  4. {
  5. char* tmp = new char[new_capacity + 1];// 开新空间
  6. strcpy(tmp, _str);// 搬运
  7. delete[] _str; // 释放原空间
  8. _str = tmp;// 没问题,递交给_str
  9. _capacity = new_capacity;// 更新容量
  10. }
  11. }
  12. void push_back(const char ch)
  13. {
  14. if (_size == _capacity)
  15. {
  16. reserve(_capacity == 0 ? 4 : _capacity * 2);
  17. }
  18. _str[_size++] = ch;// 在_size位置放字符后++
  19. _str[_size] = '\0';// 易漏
  20. //insert(_size, ch);
  21. }

4.6 resize() 的实现

我们为了扩容,先实现了 reverse,现在我们再顺便实现一下 resize。

这里再提一下 reverse 和 resize 的区别:

resize 分给初始值和不给初始值的情况,所以有两种:

库里面也是这么实现的。

但是我们上面讲构造函数的时候说过,我们可以使用全缺省的方式,这样就可以二合一了。

resize 实现的难点是要考虑种种情况,我们来举个例子分析一下:

 如果欲增容量比 _size 小的情况:

因为标准库是没有缩容的,所以我们实现的时候也不考虑去缩容。我们可以加一个 \0 去截断。

如果预增容量比 _size 大的情况:

resize 是开空间 + 初始化,开空间的工作我们就可以交给已经实现好的 reserve,

然后再写 resize 的初始化的功能,我们这里可以使用 memset 函数。

  1. void resize(size_t new_capacity, const char ch = '\0')
  2. {
  3. if (new_capacity > _size)// 插入数据
  4. {
  5. reserve(new_capacity);
  6. //for (size_t i = _size; i < new_capacity; ++i)
  7. //{
  8. // _str[i] = ch;
  9. //}
  10. memset(_str + _size, ch, new_capacity - _size);// 上面的for循环即memset的功能
  11. _str[new_capacity] = '\0';
  12. _size = new_capacity;
  13. }
  14. else// 删除数据
  15. {
  16. _str[new_capacity] = '\0';
  17. _size = new_capacity;
  18. }
  19. }

4.7 find() 的实现

 find:查找字符

如果遍历完整个字符串都没找到,就返回 npos(找到库的来)。

这个 npos 我们可以在成员变量中定义:

  1. size_t find(const char ch) const
  2. {
  3. for (size_t i = 0; i < _size; i++)
  4. {
  5. if (ch == _str[i])// 找到了
  6. {
  7. return i; // 返回下标
  8. }
  9. }
  10. return npos;// 找不到
  11. }
  12. private:
  13. char* _str;
  14. size_t _size;
  15. size_t _capacity;
  16. public:
  17. const static size_t npos = -1;// const static 语法特殊处理,直接可以当成定义初始化

 find:查找字符串

这里我们可以用 strstr 去找子串,如果找到了,返回的是子串首次出现的地址。

如果没找到,返回的是空。所以我们这里可以做判断,如果是 nullptr 就返回 npos。

如果找到了,就返回对应下标,子串地址 - 开头,就是下标了。

  1. size_t find(const char* str, size_t pos = 0) const
  2. {
  3. const char* ptr = strstr(_str + pos, str);
  4. if (ptr == nullptr)
  5. {
  6. return npos;
  7. }
  8. else
  9. {
  10. return ptr - _str; // 减开头
  11. }
  12. }

4.8 erase() 的实现

  1. string& erase(size_t pos, size_t len = npos)
  2. {
  3. assert(pos < _size);
  4. if (len == npos || pos + len >= _size)// 如果pos后面的都删完了,注意len == npos 不能忽略,因为npos + len 有可能重回到 1
  5. {
  6. _str[pos] = '\0';
  7. _size = pos;
  8. }
  9. else
  10. {
  11. strcpy(_str + pos, _str + pos + len);
  12. _size -= len;
  13. }
  14. return *this;
  15. }

测试find 和 erase:

5. 传统写法和现代写法

对于拷贝构造的深拷贝,传统写法就是本本分分分地去完成深拷贝:

  1. string(const string& s)/传统写法
  2. :_str(new char[s._capacity + 1])
  3. , _size(s._size)
  4. , _capacity(s._capacity)
  5. {
  6. strcpy(_str, s._str);
  7. }

5.1 拷贝构造的现代写法

现在我们来介绍一种现代写法,它和传统写法本质工作是一样的,即完成深拷贝。

现代写法的方式不是本本分分地去按着 Step 一步步干活,而是 "投机取巧" 地去完成深拷贝。

  1. void swap(string& s)// s和*this换
  2. {
  3. ::swap(s._str, _str);//注意这里要加域作用符,默认是全局的,不然就是自己调自己了
  4. ::swap(s._size, _size);
  5. ::swap(s._capacity, _capacity);
  6. }
  7. string(const string& s)/现代写法
  8. :_str(nullptr)
  9. , _size(0)
  10. , _capacity(0)
  11. {
  12. string tmp(s._str);
  13. swap(tmp);// tmp和*this换
  14. }

现代写法的本质就是复用了构造函数。

我想拷贝,但我又不想自己干,我把活交给工具人 tmp 来帮我干。

值得注意的是如果不给原_str赋空指针,那么它的默认指向会是个随机值。和tmp交换后,tmp 是一个局部对象,我们把 s2 原来的指针和 tmp 交换了,那么 tmp 就成了个随机值了。tmp 出了作用域要调用析构函数,对随机值指向的空间进行释放,怎么释放?都不是你自己的 new / malloc 出来的,你还硬要对它释放,就可能会引发崩溃。但是 delete / free 一个空,是不会报错的,因为会进行一个检查。所以是可以 delete 一个空的,我们这里初始化列表中把 nullptr 给 _str,是为了交换完之后, nullptr 能交到 tmp 手中,这样 tmp 出了作用域调用析构函数就不会翻车了。

5.2 赋值重载的现代写法

赋值重载的传统写法:

  1. string& operator=(const string& s)/传统写法
  2. {
  3. if (this != &s)
  4. {
  5. char* tmp = new char[s._capacity + 1];// 开辟新的空间
  6. strcpy(tmp, s._str);// 赋值到tmp
  7. delete[] _str;// 释放原有空间
  8. _str = tmp;// tmp赋值到想要的地方,出去tmp就销毁了
  9. _size = s._size;
  10. _capacity = s._capacity;
  11. }
  12. return *this;
  13. }

赋值重载的现代写法:

  1. string& operator=(const string& s)/现代写法
  2. {
  3. if (this != &s)
  4. {
  5. string tmp(s);
  6. swap(tmp);// tmp和*this换
  7. }
  8. return *this;
  9. }

比上面的拷贝构造的现代写法还要压榨tmp,交换完之后,正好让 tmp 出作用域调用析构函数,属实是一石二鸟的美事。把 tmp 压榨的干干净净,还让 tmp 帮忙把屁股擦干净(释放空间)。

用上面的测试简单测一下:

总结:

现代写法在 string 中体现的优势还不够大,因为好像和传统写法差不多。

 但是到后面实现 vector、list 的时候,会发现现代写法的优势真的是太大了。

现代写法写起来会更简单些,比如如果是个链表,传统写法就不是 strcpy 这么简单的了,

你还要一个一个结点拷贝过去,但是现代写法只需要调用 swap 交换一下就可以了。

现代写法更加简洁,只是在 string 这里优势体现的不明显罢了,我们后面可以慢慢体会。

6. operator 运算符重载

6.1 六个比较运算符重载

学日期类的时候就说过,只需实现 > 和 ==,剩下的都可以复用解决:

而且> 和 ==可以直接用strcmp ,剩下的复用,你不想用strcmp时剩下的也不用改了:

  1. bool operator>(const string& s) const
  2. {
  3. return strcmp(_str, s._str) > 0;
  4. }
  5. bool operator==(const string& s) const
  6. {
  7. return strcmp(_str, s._str) == 0;
  8. }
  9. bool operator>=(const string& s) const// 养成this指针写在前面的习惯
  10. {
  11. return *this > s || *this == s;
  12. }
  13. bool operator<(const string& s) const
  14. {
  15. return !(*this >= s);
  16. }
  17. bool operator<=(const string& s) const
  18. {
  19. return !(*this > s);
  20. }
  21. bool operator!=(const string& s) const
  22. {
  23. return !(*this == s);
  24. }

6.2 流插入和流提取重载

 我们当时实现日期类的流插入和流提取时,也详细讲过这些,当时讲解了友元。

 在友元那一章我们说过 "占参问题" ,这里就不再多做解释了。

如果我们重载成成员函数,第一个位置就会被隐含的 this 指针占据。

这样实现出来的流插入必然会不符合我们的使用习惯,所以我们选择在全局实现。

在全局里不存在隐含的 this 指针了。

而且我们已经有operator [ ] 可以访问私有成员了,所以不需要设置成友元函数:

流插入很简单:

  1. ostream& operator<<(ostream& out, string& s)
  2. {
  3. for (size_t i = 0;i < s.size();++i)
  4. {
  5. out << s[i];
  6. }
  7. return out;
  8. }

但是流提取是这么简单吗?下面的代码有什么问题?:

  1. istream& operator>>(istream& in, string& s)
  2. {
  3. char ch;
  4. in >> ch;
  5. while (ch != ' ' && ch != '\n')
  6. {
  7. s += ch;
  8. in >> ch;
  9. }
  10. return in;
  11. }

我们发现这样输入空格和换行也终止不了程序,因为cin会自动忽略空格和换行。

有什么办法?cin有一个get的成员函数,可以获取每一个字符,现在我们查下文档会用就行,

后面我们还会详细的讲解IO流,流提取普通实现:

  1. istream& operator>>(istream& in, string& s)
  2. {
  3. char ch;
  4. ch = in.get();
  5. while (ch != ' ' && ch != '\n')
  6. {
  7. s += ch;
  8. ch = in.get();
  9. }
  10. return in;
  11. }

这样实现的流提取有一个缺陷:频繁的 += 效率低,能想到什么办法优化?

以下是类似库里面的实现:(思路类似缓冲区)

  1. istream& operator>>(istream& in, string& s)// 流插入优化(类似库里面的)
  2. {
  3. char ch;
  4. ch = in.get();
  5. const size_t N = 32;
  6. char buff[N];// C++11支持的变长数组
  7. size_t i = 0;
  8. while (ch != ' ' && ch != '\n')
  9. {
  10. buff[i++] = ch;
  11. if (i == N - 1)// 如果buff的容量满了
  12. {
  13. buff[i] = '\0';// 在后面放\0,
  14. s += buff;// += 到 s 上
  15. i = 0;// 把 i 重新变成0 用来再次使用buff数组
  16. }
  17. ch = in.get();
  18. }
  19. buff[i] = '\0';// 处理一下buff剩余的
  20. s += buff;
  21. return in;
  22. }

简单测试下:

  1. void test_string6()
  2. {
  3. string s1;
  4. string s2;
  5. cin >> s1 >> s2;
  6. cout << s1 << endl << s2 << endl;
  7. cout << (s1 > s2) << endl;
  8. cout << (s1 == s2) << endl;
  9. cout << (s1 >= s2) << endl;
  10. cout << (s1 < s2) << endl;
  11. cout << (s1 <= s2) << endl;
  12. cout << (s1 != s2) << endl;
  13. }

7. 完整代码:

string.h:

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. #include<assert.h>
  5. using namespace std;
  6. namespace rtx
  7. {
  8. class string
  9. {
  10. public:
  11. string(const char* s = "")
  12. {
  13. _size =strlen(s);// 因为要算多次strlen 效率低 且放在初始化列表关联到声明顺序 所以不用初始化列表
  14. _capacity = _size;
  15. _str = new char[_size + 1];// 开_size+1大小的空间(多开一个放\0)
  16. strcpy(_str, s);
  17. }
  18. //string(const string& s)/传统写法
  19. // :_str(new char[s._capacity + 1])
  20. // , _size(s._size)
  21. // , _capacity(s._capacity)
  22. //{
  23. // strcpy(_str, s._str);
  24. //}
  25. void swap(string& s)// s和*this换
  26. {
  27. ::swap(s._str, _str);//注意这里要加域作用符,默认是全局的,不然就是自己调自己了
  28. ::swap(s._size, _size);
  29. ::swap(s._capacity, _capacity);
  30. }
  31. string(const string& s)/现代写法
  32. :_str(nullptr)
  33. , _size(0)
  34. , _capacity(0)
  35. {
  36. string tmp(s._str);
  37. swap(tmp);// tmp和*this换
  38. }
  39. //string& operator=(const string& s)/传统写法
  40. //{
  41. // if (this != &s)
  42. // {
  43. // char* tmp = new char[s._capacity + 1];// 开辟新的空间
  44. // strcpy(tmp, s._str);// 赋值到tmp
  45. // delete[] _str;// 释放原有空间
  46. // _str = tmp;// tmp赋值到想要的地方,出去tmp就销毁了
  47. // _size = s._size;
  48. // _capacity = s._capacity;
  49. // }
  50. // return *this;
  51. //}
  52. string& operator=(const string& s)/现代写法
  53. {
  54. if (this != &s)
  55. {
  56. string tmp(s);
  57. swap(tmp);// tmp和*this换
  58. }
  59. return *this;
  60. }
  61. ~string()
  62. {
  63. delete[] _str;
  64. _str = nullptr;
  65. }
  66. const char* c_str() const
  67. {
  68. return _str;
  69. }
  70. size_t size() const
  71. {
  72. return _size;
  73. }
  74. char& operator[](size_t pos)
  75. {
  76. assert(pos < _size);
  77. return _str[pos];
  78. }
  79. const char& operator[](size_t pos) const
  80. {
  81. assert(pos < _size);
  82. return _str[pos];
  83. }
  84. typedef char* iterator;
  85. iterator begin()
  86. {
  87. return _str;
  88. }
  89. iterator end()
  90. {
  91. return _str + _size;
  92. }
  93. typedef const char* const_iterator;
  94. const_iterator begin() const
  95. {
  96. return _str;
  97. }
  98. const_iterator end() const
  99. {
  100. return _str + _size;
  101. }
  102. void reserve(size_t new_capacity)
  103. {
  104. if (new_capacity > _capacity)
  105. {
  106. char* tmp = new char[new_capacity + 1];// 开新空间
  107. strcpy(tmp, _str);// 搬运
  108. delete[] _str; // 释放原空间
  109. _str = tmp;// 没问题,递交给_str
  110. _capacity = new_capacity;// 更新容量
  111. }
  112. }
  113. void push_back(const char ch)
  114. {
  115. if (_size == _capacity)
  116. {
  117. reserve(_capacity == 0 ? 4 : _capacity * 2);
  118. }
  119. _str[_size++] = ch;// 在_size位置放字符后++
  120. _str[_size] = '\0';// 易漏
  121. //insert(_size, ch);
  122. }
  123. void append(const char* str)
  124. {
  125. int len = strlen(str);
  126. if (_size + len > _capacity)
  127. {
  128. reserve(_size + len);
  129. }
  130. strcpy(_str + _size, str);// 首字符+_size大小就是\0位置
  131. _size += len;
  132. //insert(_size, str);
  133. }
  134. string& operator+=(const char ch)
  135. {
  136. push_back(ch);
  137. return *this;
  138. }
  139. string& operator+=(const char* str)
  140. {
  141. append(str);
  142. return *this;
  143. }
  144. string& insert(size_t pos, const char ch)
  145. {
  146. assert(pos <= _size);
  147. if (_size == _capacity)
  148. {
  149. reserve(_capacity == 0 ? 4 : _capacity * 2);
  150. }
  151. for (size_t i = _size + 1;i > pos; --i)// 挪动数据,+1是挪动\0
  152. {
  153. _str[i] = _str[i - 1];
  154. }
  155. _str[pos] = ch;
  156. ++_size;
  157. return *this;
  158. }
  159. string& insert(size_t pos, const char* str)
  160. {
  161. assert(pos <= _size);
  162. int len = strlen(str);
  163. if (_size + len > _capacity)
  164. {
  165. reserve(_size + len);
  166. }
  167. for (size_t i = _size + len ;i > pos + len - 1; --i)// 挪动数据,画图注意边界,参考上面inser字符的len == 1
  168. {
  169. _str[i] = _str[i - len];// 首先看\0 _size+len-len就是\0的位置
  170. }
  171. strncpy(_str + pos, str, len);
  172. _size += len;
  173. return *this;
  174. }
  175. void resize(size_t new_capacity, const char ch = '\0')
  176. {
  177. if (new_capacity > _size)// 插入数据
  178. {
  179. reserve(new_capacity);
  180. //for (size_t i = _size; i < new_capacity; ++i)
  181. //{
  182. // _str[i] = ch;
  183. //}
  184. memset(_str + _size, ch, new_capacity - _size);// 上面的for循环即memset的功能
  185. _str[new_capacity] = '\0';
  186. _size = new_capacity;
  187. }
  188. else// 删除数据
  189. {
  190. _str[new_capacity] = '\0';
  191. _size = new_capacity;
  192. }
  193. }
  194. size_t find(char ch) const
  195. {
  196. for (size_t i = 0; i < _size; i++)
  197. {
  198. if (ch == _str[i])// 找到了
  199. {
  200. return i; // 返回下标
  201. }
  202. }
  203. return npos;// 找不到
  204. }
  205. size_t find(const char* str, size_t pos = 0) const
  206. {
  207. const char* ptr = strstr(_str + pos, str);
  208. if (ptr == nullptr)
  209. {
  210. return npos;
  211. }
  212. else
  213. {
  214. return ptr - _str; // 减开头
  215. }
  216. }
  217. string& erase(size_t pos, size_t len = npos)
  218. {
  219. assert(pos < _size);
  220. if (len == npos || pos + len >= _size)// 如果pos后面的都删完了,注意len == npos 不能忽略,因为npos + len 有可能重回到 1
  221. {
  222. _str[pos] = '\0';
  223. _size = pos;
  224. }
  225. else
  226. {
  227. strcpy(_str + pos, _str + pos + len);
  228. _size -= len;
  229. }
  230. return *this;
  231. }
  232. bool operator>(const string& s) const
  233. {
  234. return strcmp(_str, s._str) > 0;
  235. }
  236. bool operator==(const string& s) const
  237. {
  238. return strcmp(_str, s._str) == 0;
  239. }
  240. bool operator>=(const string& s) const// 养成this指针写在前面的习惯
  241. {
  242. return *this > s || *this == s;
  243. }
  244. bool operator<(const string& s) const
  245. {
  246. return !(*this >= s);
  247. }
  248. bool operator<=(const string& s) const
  249. {
  250. return !(*this > s);
  251. }
  252. bool operator!=(const string& s) const
  253. {
  254. return !(*this == s);
  255. }
  256. private:
  257. char* _str;
  258. size_t _size;
  259. size_t _capacity;
  260. public:
  261. const static size_t npos = -1;// const static 语法特殊处理,直接可以当成定义初始化
  262. };
  263. ostream& operator<<(ostream& out, string& s)
  264. {
  265. for (size_t i = 0;i < s.size();++i)
  266. {
  267. out << s[i];
  268. }
  269. return out;
  270. }
  271. //istream& operator>>(istream& in, string& s)// 流插入普通实现
  272. //{
  273. // char ch;
  274. // ch = in.get();
  275. // while (ch != ' ' && ch != '\n')
  276. // {
  277. // s += ch;
  278. // ch = in.get();
  279. // }
  280. // return in;
  281. //}
  282. istream& operator>>(istream& in, string& s)// 流插入优化(类似库里面的)
  283. {
  284. char ch;
  285. ch = in.get();
  286. const size_t N = 32;
  287. char buff[N];// C++11支持的变长数组
  288. size_t i = 0;
  289. while (ch != ' ' && ch != '\n')
  290. {
  291. buff[i++] = ch;
  292. if (i == N - 1)// 如果buff的容量满了
  293. {
  294. buff[i] = '\0';// 在后面放\0,
  295. s += buff;// += 到 s 上
  296. i = 0;// 把 i 重新变成0 用来再次使用buff数组
  297. }
  298. ch = in.get();
  299. }
  300. buff[i] = '\0';// 处理一下buff剩余的
  301. s += buff;
  302. return in;
  303. }
  304. void test_string1()
  305. {
  306. string s1("hello world");
  307. string s2(s1);
  308. string s3("!!!");
  309. s1 = s3;
  310. cout << s1.c_str() << endl;
  311. cout << s2.c_str() << endl;
  312. cout << s3.c_str() << endl;
  313. string s4;
  314. cout << s4.c_str() << endl;
  315. }
  316. void test_string2()
  317. {
  318. string s1("hello world");
  319. string s2;
  320. for (size_t i = 0; i < s1.size(); i++)
  321. {
  322. cout << s1[i] << " ";
  323. }
  324. cout << endl;
  325. s1[0] = 'x';
  326. for (size_t i = 0; i < s1.size(); i++)
  327. {
  328. cout << s1[i] << " ";
  329. }
  330. cout << endl;
  331. }
  332. void test_string3()
  333. {
  334. string s1("hello world");
  335. string::iterator it = s1.begin();
  336. while (it != s1.end())
  337. {
  338. cout << *it << " ";// 读
  339. it++;
  340. }
  341. cout << endl;
  342. it = s1.begin();
  343. while (it != s1.end())
  344. {
  345. (*it)++;// 写,++的优先级比*高,+=就低,可以 *it += 1;
  346. cout << *it << " ";
  347. it++;
  348. }
  349. cout << endl;
  350. for (auto& e : s1)
  351. {
  352. cout << e << " ";
  353. }
  354. cout << endl;
  355. }
  356. void test_string4()
  357. {
  358. string s1("hello world");
  359. cout << s1.c_str() << endl;
  360. s1.push_back('!');
  361. cout << s1.c_str() << endl;
  362. s1.push_back('R');
  363. cout << s1.c_str() << endl;
  364. s1.append("abcd");
  365. cout << s1.c_str() << endl;
  366. s1 += 'e';
  367. s1 += "fgh";
  368. cout << s1.c_str() << endl;
  369. s1.insert(0, 'x');
  370. s1.insert(6, 'T');
  371. cout << s1.c_str() << endl;
  372. s1.insert(6, "PPPPPPPPPP");
  373. cout << s1.c_str() << endl;
  374. s1.insert(0, "PPPPPPPPPP");
  375. cout << s1.c_str() << endl;
  376. s1.resize(100,'x');
  377. cout << s1.c_str() << endl;
  378. }
  379. void test_string5()
  380. {
  381. string s1("hello world");
  382. string s2(s1);
  383. string s3 = s1;
  384. cout << s2.c_str() << endl << s3.c_str() << endl;
  385. cout << s1.find('d') << endl;// 打印d的下标:6
  386. cout << s1.find("world") << endl;// 打印了w的下标:10
  387. cout << s1.find("wold") << endl;// 打印了npos:4294967295
  388. cout << s1.find("world", 9) << endl;// 打印了npos:4294967295
  389. s1.erase(9, 2);// 从下标9开始删除2个字符
  390. cout << s1.c_str() << endl;
  391. s1.erase(s1.find('o'), 2);// 找到o,其下标为4,从下标4开始删除2个字符
  392. cout << s1.c_str() << endl;
  393. s1.erase(5);// 从下标5开始删完
  394. cout << s1.c_str() << endl;
  395. }
  396. void test_string6()
  397. {
  398. string s1;
  399. string s2;
  400. cin >> s1 >> s2;
  401. cout << s1 << endl << s2 << endl;
  402. cout << (s1 > s2) << endl;
  403. cout << (s1 == s2) << endl;
  404. cout << (s1 >= s2) << endl;
  405. cout << (s1 < s2) << endl;
  406. cout << (s1 <= s2) << endl;
  407. cout << (s1 != s2) << endl;
  408. }
  409. }

Test.c:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "string.h"
  3. int main()
  4. {
  5. try
  6. {
  7. rtx::test_string6();
  8. }
  9. catch (const exception& e)
  10. {
  11. cout << e.what() << endl;
  12. }
  13. return 0;
  14. }

本章完。

这篇博客两万多个字了......刚开始接触确实有点累der

不过STL的实现都是类似的,以后的学习就轻松多了,

下一步:了解vector的接口函数,写写vector的OJ题,模拟实现vector。

想笑~ 来伪装掉下的眼泪~

23年5月27,早起把博客发出去,我又行了。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号