本节内容主要针对的是在STL之前的string类的实现,通过模拟实现string类的一些功能来更好地使用string。本篇主要针对的是非const对象,如果要实现const的对象的话,只需要在对应的部分增添上const就行了。编者水平有限,如有错误欢迎指正。
目录
1.String.h
2.Test.cpp
1.String.h
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
class String
{friend istream& getline(istream &in, String &s);friend ostream& operator<<(ostream&out,String&s);friend istream& operator>>(istream &in, String &s);
public: static const size_t npos = -1;// 实现迭代器typedef char *iterator;typedef const char *const_iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}const_iterator begin() const{return _str;}const_iterator end() const{return _str + _size;}// 构造String(const char *str = "");// 析构~String();//拷贝构造String(const String& s);// 获取长度size_t size() const;// 获取容器容量size_t capacity() const;// 访问元素char &operator[](size_t pos);// 获取字符数组const char *c_str() const;// 容量预设void reserve(size_t n);// 调整大小void resize(size_t n, char c = '\0');// 尾插字符void push_back(char c);// 尾插字符串void append(const char *s);// 尾插数据String &operator+=(const char *s);String &operator+=(char c);// 清除数据String &erase(size_t pos = 0, size_t len = npos);// 查找字符或子串size_t find(char c, size_t pos = 0) const;size_t find(const char *s, size_t pos = 0) const;// 取子串String substr(size_t pos = 0, size_t len = npos) const;// 清除void clear();// 交换void swap(String &s);// 插入String &insert(size_t pos, char c);String &insert(size_t pos, const char *s);// 重载String operator+(const char* s);String &operator=(const String &s);bool operator>(const String& s);bool operator<(const String& s);bool operator==(const String& s);private:// 长度int _size;// 容量int _capacity;// 内容char *_str;
};
istream& getline(istream &in, String &s)
{//先对原字符串进行清空s.clear();//然后获取字符,填充buff数组char ch;ch = in.get();char buff[128];size_t i = 0;//取到'\n'截止while (ch != '\n'){buff[i++] = ch;//如若数组到头了,就将最后一个字符置为'\0',并将字符数组的值赋予给String对象if (i == 127){buff[127] = '\0';s += buff;i = 0;}ch = in.get();}if (i > 0){buff[i] = '\0';s += buff;}return in;
}ostream& operator<<(ostream&out,String&s)
{out<<s._str<<endl;return out;
}istream& operator>>(istream&in,String&s)
{//先对原字符串进行清空s.clear();//然后获取字符,填充buff数组char ch;ch = in.get();char buff[128];size_t i = 0;//取到' '或者'\n'截止while (ch != ' ' && ch != '\n'){buff[i++] = ch;//如若数组到头了,就将最后一个字符置为'\0',并将字符数组的值赋予给String对象if (i == 127){buff[127] = '\0';s += buff;i = 0;}ch = in.get();}if (i > 0){buff[i] = '\0';s += buff;}return in;
}String::String(const char *str) : _size(strlen(str))
{//初始容量即为初始长度_capacity = _size;//开辟空间应该比_capacity多1个用来存储'\0'_str = new char[_capacity + 1];//拷贝数值strcpy(_str, str);
}String::~String()
{//防止二次释放if(_str){delete[] _str;_str = nullptr;_size = _capacity = 0;}
}String::String(const String &s)
{//道理同构造函数_size = s._size;_capacity = s._capacity;_str = new char[_capacity + 1];strcpy(_str, s._str);
}size_t String::size() const
{return _size;
}size_t String::capacity() const
{return _capacity;
}char &String::operator[](size_t pos)
{assert(pos < _size);return _str[pos];
}const char *String::c_str() const
{return _str;
}void String::reserve(size_t n)
{// 扩容,如果n大于_capacity,就扩容并挪动数据assert(n > 0);if (n > _capacity){char *temp = new char[n + 1];strcpy(temp, _str);delete[] _str;_str = temp;_capacity = n;}
}void String::resize(size_t n, char c)
{// 如果n小于了目前的长度,则从n起的所有元素被清除,大于目前的长度则扩容if (n <= _size){_str[n] = '\0';_size = n;}else{reserve(n);for (int i = _size; i < n; i++){_str[i] = c;}_str[n] = '\0';_size = n;}
}void String::push_back(char c)
{if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}_str[_size++] = c;_str[_size] = '\0';
}void String::append(const char *s)
{int len = strlen(s);if (len + _size > _capacity){reserve(len + _size);}strcpy(_str + _size, s);_size += len;
}String &String::operator+=(const char *s)
{append(s);return *this;
}String &String::operator+=(char c)
{push_back(c);return *this;
}String &String::erase(size_t pos, size_t len)
{assert(pos < _size);// 如果清除长度大于剩余长度,则一次性清完,否则擦除了之后就把剩下的copy到后面去if (pos + len >= _size || len == npos){_str[pos] = '\0';_size = pos;}else{strcpy(_str + pos, _str + pos + len);_size -= len;}return *this;
}size_t String::find(char c, size_t pos) const
{// 找到了返回相对于起点的定位索引,找不到返回nposfor (int i = pos; i < _size; i++){if (c == _str[i]){return i;}}return npos;
}size_t String::find(const char *s, size_t pos) const
{// 找到了返回相对于起点的定位索引,找不到返回nposconst char *p = strstr(_str + pos, s);if (p){return p - _str;}else{return npos;}
}String String::substr(size_t pos, size_t len) const
{String sub;// 判断所取长度是否超过了剩余长度,是则直接取完,否则按需所取if (len == npos || len >= _size - pos){for (size_t i = pos; i < _size; i++){sub += _str[i];}}else{for (size_t i = pos; i < pos + len; i++){sub += _str[i];}}return sub;
}void String::clear()
{// 直接置为0即可_size = 0;_str[_size] = '\0';
}void String::swap(String &s)
{std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);
}String &String::insert(size_t pos, char c)
{assert(pos <= _size);// 检查容量if (_size = _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}// 挪动数据size_t index = _size + 1;while (index > pos){_str[index] = _str[index - 1];index--;}// 添加数据_str[pos] = c;_size++;return *this;
}String &String::insert(size_t pos, const char *s)
{assert(pos <= _size);// 先检查容量,挪动数据size_t len = strlen(s);if (len > _capacity - _size){reserve(_capacity + len);}// 接着将数据从pos位置后移strlen(s)个单位size_t end = _size + len;while (end > pos + len - 1){_str[end] = _str[end - len];end--;}// 最后使用strncpy把s中的数据copy进去strncpy(_str + pos, s, len);_size += len;return *this;
}String String::operator+(const char*s)
{//erro 浅拷贝导致同一块空间被多次delete(已订正)String temp=*this;temp+=s;return temp;
}String &String::operator=(const String &s)
{//道理同拷贝构造,但是需要将原来指向的空间delete_size = s._size;_capacity = s._capacity;delete[]_str;_str = new char[_capacity + 1];strcpy(_str, s._str);return *this;
}bool String::operator>(const String &s)
{if(strcmp(_str,s._str)>0)return true;return false;
}bool String::operator<(const String &s)
{if(strcmp(_str,s._str)<0)return true;return false;
}bool String::operator==(const String &s)
{if(strcmp(_str,s._str)==0)return true;return false;
}
2.Test.cpp
#include "String.h"
void Test1()
{String a1;String a2("hahaha");cout << a2[1] << endl;cout << a2[0] << endl;cout << a2.size() << endl;cout << a2.capacity() << endl;
}
void Test2()
{String a1("ssDut");cout << a1.c_str() << endl;a1.reserve(3);cout << a1.capacity() << endl;a1.reserve(20);cout << a1.capacity() << endl;a1.resize(0);cout << a1.c_str() << endl;a1.resize(23, 'a');cout << a1.capacity() << endl;cout << a1.c_str() << endl;
}
void Test3()
{String a1("hello world");cout << a1.c_str() << endl;cout << a1.capacity() << endl;cout << a1.size() << endl;// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// a1.push_back('a');// cout<<a1.c_str()<<endl;// cout<<a1.capacity()<<endl;// cout<<a1.size()<<endl;a1.append("Qinhan");cout << a1.c_str() << endl;cout << a1.capacity() << endl;cout << a1.size() << endl;
}
void Test4()
{String a1("I bite you");// (a1+="r mother")+="Timi";cout << a1.c_str() << endl;cout << a1.capacity() << endl;cout << a1.size() << endl;a1.erase(3, 4);cout << a1.c_str() << endl;cout << a1.capacity() << endl;cout << a1.size() << endl;a1.erase();cout << a1.c_str() << endl;cout << a1.capacity() << endl;cout << a1.size() << endl;// a1.erase(10, 10);// cout << a1.c_str() << endl;// cout << a1.capacity() << endl;// cout << a1.size() << endl;
}
void Test5()
{String a1("I bite you");for(auto e:a1){cout<<e<<endl;}cout<<"————————————————————"<<endl;// a1.insert(10,'c');// a1.insert(2,"happy day");// for(auto e:a1)// {// cout<<e<<endl;// }// cout<<(int)a1._str[a1._size]<<endl;
}
void Test6()
{String a1("I bite you");String a2=a1+"r mom";for(auto e:a2){cout<<e;}cout<<endl;cout<<(a1<a2)<<endl;
}
void Test7()
{String s1("Hello");cout<<s1;// cin>>s1;getline(cin,s1);cout<<s1;
}
int main()
{Test7();return 0;
}