1.string的成员变量、(拷贝)构造、析构函数
1.1.成员变量
private:char* _str;size_t _size; //string中有效字符个数size_t _capacity; //string中能存储有效字符个数的大小
1.2(拷贝)构造函数
//构造函数string(const char* str = ""){//结尾处的'\0'不算做有效字符,//所以在底层的空间上要多开一个字节的空间来存放'\0'_size = strlen(str);_str = new char[_size + 1];//_capacity是能存储有效字符的大小,不包括'\0'_capacity = _size;strcpy(_str,str);}
//拷贝构造string(const string& s){_str = new char[s._capacity+1];strcpy(_str,s._str);_size = s._size;_capacity = s._capacity;}
1.3析构函数
//析构函数~string(){delete[] _str;_str = nullptr;_capacity = _size = 0;}
1.4赋值拷贝
//赋值拷贝string& operator=(const string& s){if (this != &s){char * tmp = new char[s._capacity + 1];strcpy(tmp, s._str);delete[] _str;_str = tmp;_size = s._size;_capacity = s._capacity;}return *this;}
2.string的遍历
2.1下标遍历
//下标遍历char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos) const{assert(pos < _size);return _str[pos];}
2.2迭代器遍历
范围for底层逻辑其实就是套用的迭代器,故我们不考虑范围for
//迭代器遍历typedef char* iterator;iterator begin(){return _str;}iterator end(){return _str+_size;}//迭代器指向的内容不能变typedef const char* const_iterator;const_iterator begin()const{return _str;}const_iterator end()const{return _str + _size;}
3.string的增删查改
3.1尾插
//扩容void reserve(size_t n){if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}}//尾插void push_back(char ch){//判断是否扩容if (_size = _capacity){//需要注意capacity是否为0size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;reserve(newcapacity);}_str[_size] = ch;_str[++_size] = '\0';}void append(const char* str){//判断是否扩容size_t len = strlen(str);if (_size + len > _capacity){reserve(_size+len);}strcpy(_str + _size, str);_size += len;}string& operator+=(char ch){push_back(ch);return *this;}string& operator+=(const char* str){append(str);return *this;}
3.2中间插入(头插)
//中间插入(头插)string& insert(size_t pos, char c){//判断是否越界assert(pos <= _size);//判断是否扩容if (_size = _capacity){//需要注意capacity是否为0size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;reserve(newcapacity);}for (size_t end = _size + 1; end > pos; end--){_str[end] = _str[end - 1];}_str[pos] = c;++_size;return *this;}string& insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);//判断是否扩容if (_size + len > _capacity){reserve(_size + len);}//调整空间for (size_t end = _size + len; end > pos; end--){_str[end] = _str[end - len];} strncpy(_str + pos, str , len);_size += len;return *this;}
3.3删除字符
//删除pos位置上的元素string& erase(size_t pos, size_t len = npos){assert(pos < _size);//pos以后的全部删除的情况if (len == npos || _size - pos <= len ){_str[pos] = '\0';_size = pos;}//删除部分的情况else{size_t end = pos + len;strcpy(_str + pos, _str + end);_size = end;}return *this;}
3.4查找字符/字符串
//查找字符// 返回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;}}}// 返回子串s在string中第一次出现的位置size_t find(const char* str, size_t pos = 0) const{const char * ptr = strstr(_str+pos, str);if (ptr == nullptr){return npos;}else{return ptr - _str;}}
返回len长度的子串
string substr(size_t pos = 0, size_t len = npos)
//返回len长度的子串string substr(size_t pos = 0, size_t len = npos){assert(pos < _size);size_t end = pos + len;//取完的情况if (len == npos || _size - pos <= len){end = _size;}string s;s.reserve(end - pos);for (size_t i = pos; i < end; i++){s += _str[i];}return s;}
4.string的cout\cin
//cout\cin的重载ostream& operator<<(ostream& out, const string& s){for (auto ch : s){out << ch;}return out;}istream& operator>>(istream& in, string& s){//清空字符串ss.clear();char ch = in.get();char buff[128];int i = 0;while (ch != ' ' && ch != '\n'){buff[i++] = ch;if (i == 127){buff[127] = '\0';s += buff;i = 0;}ch = in.get();}if (i > 0){buff[i] = '\0';s += buff;}return in;}