1、string类的构造
上面的代码从表面看没什么问题,但是运行后会发现程序有多处bug
但是如上图一样,这样改进依然有bug
因为我们编写无参构造函数的时候,肯定要让_str默认为nullptr,但是这样的话,在main函数中创建对象s2并调用函数c_str,函数就会返回nullptr,无法打印,程序就会崩溃
2.string的拷贝构造
如果没有建立拷贝构造函数,则在程序中有对象拷贝另一个对象的时候,就会调用系统默认的拷贝构造。默认的拷贝构造是浅拷贝,浅拷贝对程序的危害很大,极容易产生内存方面的问题
解决方法就是自己建立一个拷贝构造函数
string(const string& str){_size = str._size;_capacity = str._capacity;_str = new char[_capacity + 1];memmove(_str, str._str, _size + 1);}
3、string类的析构函数
~string(){if (_str != nullptr){delete[] _str;_str = nullptr;}_capacity = 0;_size = 0;}
4、string类的运算符重载
4.1operator=写法
4.1.1传统写法
4.1.2创新写法
4.1.3简洁写法
4.2operator>>写法
初步的思路:
但是会发现:
所以我们要调用istream&的函数get
我们要添加一个clear函数,这个clear可以清空原本对象的内容
void clear(){_str[0] = '\0';_size = 0;}
但是都做到这一步了,还发现有bug
最终,再次进行改进,解决以上的两个问题
4.3operator<<的写法
为什么程序会运行错误呢?
主要有两点原因
解决方法:
整体代码(含有部分string类的函数):
#pragma once
#include<assert.h>
#include<stdio.h>
#include<string.h>
using namespace std;
namespace bit
{class string{public:typedef char* iterator;typedef const char* const_iterator;string(const char* str = ""){_size = strlen(str);_capacity = _size;_str = new char[_capacity + 1];memmove(_str, str, _size + 1);}string(const string& str){_size = str._size;_capacity = str._capacity;_str = new char[_capacity + 1];memmove(_str, str._str, _size + 1);}~string(){if (_str != nullptr){delete[] _str;_str = nullptr;}_capacity = 0;_size = 0;}const char* c_str()const{return this->_str;}size_t size()const{return _size;}const char& operator[](size_t pos)const{assert(pos < _size);return _str[pos];}char* begin()const{return _str;}char* end()const{return _str + _size;}void reserve(size_t n){if (n > _capacity){char* tem = new char[n + 1];memmove(tem, _str, _size + 1);delete[] _str;_capacity = n;_str = tem;}}void insert(size_t pos, size_t n, const char c){assert(pos <= _size);if (_size + n > _capacity){reserve(n + _size);}int end = (int)_size;while (end >= (int)pos){_str[end + n] = _str[end];end--;}for (int i = n; i > 0; i--){_str[pos + i - 1] = c;}_size += n;}void resize(size_t n, const char c='\0'){if (n > _size && n > _capacity){reserve(n);int k = n - _size;for (int i = _size; i < _size + k; i++){_str[i] = c;}_str[_size + k] = '\0';_capacity = n;_size = n;}if (n > _size && n < _capacity){int k = n - _size;for (int i = _size; i < _size + k; i++){_str[i] = c;}_str[_size+k] = '\0';_size = n;}if (n < _size && n < _capacity){_str[n] = '\0';_size = n;}}void push_back(char c){if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}_str[_size] = c;++_size;_str[_size] = '\0';}void append(const char* str){int size = strlen(str);if (_size + size > _capacity)reserve(_size + size+1);memmove(_str + _size, str, size+1);_size += size;}string& operator+=(char ch){push_back(ch);return *this;}string& operator=(const string& str){_size = str._size;_capacity = str._capacity;_str = new char[_capacity + 1];memmove(_str, str._str, _size + 1);return *this;}string& operator+=(const char* str){append(str);return *this;}void erase(size_t pos, size_t len = (size_t)-1){assert(pos <= _size);if (len == (size_t)-1||pos+len>_size){_str[pos] = '\0';_size = pos;}else{int end = pos;while (end+len<=_size){_str[end] = _str[end + len];end++;}_str[end] = '\0';_size -= len;}}size_t find(char ch, size_t pos = 0){assert(pos <= _size);for (int i = pos; i <= _size; i++){if (_str[i] == ch){return i;}}return (size_t)-1;}size_t find(const char* str, size_t pos = 0){assert(pos <= _size);const char* tem = strstr(_str + pos, str);if (tem){return tem-_str;}else{return (size_t)-1;}}string substr(size_t pos = 0, size_t len = (size_t)-1){assert(pos < _size);bit::string a;if (len == (size_t)-1||len+pos>_size){a.reserve(_size - pos);a._capacity = _size - pos;for (int i = pos; i < _size; i++){a += _str[i];}}else{a.reserve(len);a._capacity = len;for (int i = pos; i < pos + len; i++){a += _str[i];}}a._size = len;return a;}void clear(){_str[0] = '\0';_size = 0;}bool operator<(const bit::string& s) const{int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);return ret == 0 ? _size < s._size : ret < 0;}bool operator==(const bit::string& s) const{return _size == s._size&& memcmp(_str, s._str, _size) == 0;}bool operator<=(const bit::string& s) const{return *this < s || *this == s;}bool operator>(const bit::string& s) const{return !(*this <= s);}bool operator>=(const bit::string& s) const{return !(*this < s);}bool operator!=(const bit::string& s) const{return !(*this == s);}const static size_t npos = -1;private:size_t _size;size_t _capacity;char* _str;};ostream& operator<<(ostream& out, const bit::string& str){for (auto s : str){out << s;}return out;}istream& operator>>(istream& in, string& s){s.clear();char ch = in.get();while (ch == ' ' || ch == '\n'){ch = in.get();}char b[128];int i = 0;while (ch != ' ' && ch != '\n'){b[i++] = ch;if (i == 127){b[i] = '\0';s += b;i = 0;}ch = in.get();}if (i != 0){b[i] = '\0';s += b;}return in;}
};