迭代器
typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}const_iterator begin() const//左值const{return _str;}const_iterator end() const{return _str + _size;}
for(auto e : s)底层就是迭代器
[]运算符重载
char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos) const{assert(pos < _size);return _str[pos];}
=运算符重载
string& operator=(const string& str){//delete[]_str;if (this != &str){char* tmp = new char[str._size + 1];strcpy(tmp, str._str);delete[]_str;_str = tmp;_size = str._size;_capacity = str._capacity;}return *this;}
赋值运算符不写会自动生成,这三个放在一起,都是会默认生成,官方文档如下:
比较运算符重载
bool operator>(const string& str) const{return strcmp(_str, str._str) > 0;}bool operator==(const string& str) const{return strcmp(_str, str._str) == 0;}bool operator>=(const string& str) const{return *this > str || *this == str;//左边的是非const右边的是const,写反就报错}
如果> 和 == 这俩函数的左操作数不是const ,那么>= 这里 *this和str有顺序
reserve
void reserve(size_t n){if (n > _size)//防止缩容{char* tmp = new char[n + 1];strcpy(tmp, _str);delete[]_str;_str = tmp;_capacity = n;}}
n>_size防止缩容,先开空间,再释放,再该指向,防止损坏扩容失败损坏_str里数据
push_back
void push_back(char ch){if (_size + 1 > _capacity){reserve(_capacity * 2);//如果为0??}_str[_size] = ch;_size++;_str[_size] = '\0';}
默认构造里:_capacity = _size == 0 ? 4 : _size;就可以防止_capacity=0;
append
void append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}strcpy(_str + _size, str);_size += len;}
strcpy会把\0也拷贝过去,添值->_size肯定改变,strcpy(_str + _size, str); 前面的参数就是最后\0的位置
+=运算符重载
string& operator+=(const char* str){append(str);return *this;}string& operator+=(const char ch){push_back(ch);return *this;}
复用
resize
void resize(size_t n, char ch = '\0'){if (n <= _size){_str[n] = '\0';_size = n;}else{if (n > _capacity){reserve(n);}size_t i = _size;while (i < n){_str[i++] = ch;}_size = n;_str[n] = '\0';}}
n<_size直接\0截断,_size跟着改,
n==_size不走
n>_size,先判断是否扩容,再添加到个数n,_str[n]是第n+1个位置
insert
string& insert(size_t pos, char ch)//库里有个n,三个参数{assert(pos <= _size);if (_size + 1 > _capacity){reserve(_capacity * 2);}size_t end = _size + 1;while (end > pos){_str[end] = _str[end - 1];end--;}_str[pos] = ch;_size++;return *this;}
同样的配方,先检查容量,再是插入,下面这个不行,end>=pos,若pos=0,end和是size_t,改end类型为int也不行,因为pos是size_t,pos也改就行,但是为了尽量和库里一致,上面的就可以在0>0停止(库里是n个char,有三个参数的)
size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
end--;
}
string& insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}size_t end = _size + len;while (end > pos + len - 1)//解释③{_str[end] = _str[end - len];end--;}strncpy(_str + pos, str, len);_size += len;return *this;}
用strncpy,用个数len防止拷贝到\0
erase
string& erase(size_t pos, size_t len = npos){if (len == npos || pos + len >= _size)//越界{_str[pos] = '\0';_size = pos;}else{strcpy(_str + pos, _str + pos + len);_size -= len;}return *this;}
swap
void swap(string& s){std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);}
比直接用swap(s1,s2)高效,这里是直接交换指向,不用拷贝构造
find
size_t find(char ch, size_t pos = 0){assert(pos <= _size);for (size_t i = pos; i < _size; i++){if (_str[i] == ch)return i;}return -1;}size_t find(const char* str, size_t pos = 0){assert(pos <= _size);char* p = strstr(_str + pos, str);if (p){return p - _str;}else{return npos;}}
<<运算符重载
ostream& operator<<(ostream& out, const string& s){for (auto e : s){out << e;}return out;}
>>运算符重载
istream& operator >>(istream& in, string& s){s.clear();char ch = in.get();//get可以得到每一个字符包括空格和换行,in>>不行while (ch != ' ' && ch != '\0'){s += ch;ch = in.get();}return in;}
get可以得到每一个字符包括空格和换行,in>>不行