string类是表示字符串的字符串类,它是basic_string模板类的一个实例,也可以说它的底层是basic_string模板类的一个别名,typedef basic_string<char, char_traits, allocator> string
.char_traits是base_string的一个模板参数,它主要负责关于字符的属性和方法,string类的字符类型是char类型。接下来,我们就模拟实现一下string类的的常见接口的功能。
//#pragma once
#include <iostream>
#include <cstring>
#include <assert.h>
namespace liu {
class String {
public:
typedef char* Iterator;
Iterator begin() {
return _str;
}
Iterator end() {
return _str + _size;
}
String(const char* str = ""){
if (str == nullptr) {
assert(false);
return;
}
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
String(String& s) :
_str(nullptr),
_size(0),
_capacity(0) {
String tmp(s._str);
this->Swap(tmp);
}
void Swap(String& s) { //交换对象
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
String& operator=(const String& s) {
if (*this != s) {
char* pStr = new char[s._capacity + 1];
strcpy(pStr, s._str);
delete[] _str;
_str = pStr;
_size = s._size;
_capacity = s._capacity;
}
//this->Swap(s);
return *this;
}
~String() {
if (_str) {
delete[] _str;
_size = 0;
_capacity = 0;
}
}
char* C_str() {//用C语言格式输出字符串
return _str;
}
bool operator<(const String& s) {
return strcmp(_str, s._str) < 0;
}
bool operator>(const String& s) {
return strcmp(_str, s._str) > 0;
}
bool operator<=(const String& s) {
return ((*this < s) || (*this == s));
}
bool operator>=(const String& s) {
return !(*this < s);
}
bool operator==(const String& s) {
return strcmp(_str, s._str) == 0;
}
bool operator!=(const String& s) {
return !(*this == s);
}
void Resize(size_t newSize, char ch = char()) {
if (_size < newSize) {
if (_capacity < newSize) {
Reserve(newSize);
}
memset(_str + _size, ch, newSize - _size);
}
_size = newSize;
_str[_size] = '\0';
return;
}
void Reserve(size_t newCapacity) {
//判断容量并实现扩容
if (_capacity < newCapacity) {
//std::cout << "reserver" << std::endl;
char* str = new char[newCapacity + 1];
strcpy(str, _str);
delete[] _str;
_str = str;
_capacity = newCapacity;
}
return;
}
void Clear() {
_size = 0;
_str[_size] = '\0';
}
size_t Size() const {
return _size;
}
size_t Capacity() const {
return _capacity;
}
bool Empty() const {
return _size == 0;
}
/*截取从pos起始的n个字符*/
String Substr(size_t pos, size_t n) {
char* pStr = new char[n + 1];
size_t i = 0, j = pos;
while (i < n) {
if (j < _size) {
pStr[i++] = _str[j++];
} else {
break;
}
}
pStr[i] = '\0';
String ret(pStr);
return ret;
}
/*追加一个字符*/
void PushBack(char ch) {
Reserve(_size * 2);
_str[_size++] = ch;
_str[_size] = '\0';
}
/*追加一个字符串*/
void Append(const char* str) {
//Reserve(_size + strlen(str) + 1);
//strcpy(_str + _size, str);
// _size += strlen(str);
for (size_t i = 0; i < strlen(str); ++i) {
PushBack(str[i]);
}
}
String& operator+=(char ch) {
PushBack(ch);
return *this;
}
String& operator+=(const char* str) {
Append(str);
return *this;
}
String& operator+=(const String& str) {
for (size_t i = 0; i < str.Size(); ++i) {
PushBack(str[i]);
}
return *this;
}
/*插入一个字符*/
/*void Insert(size_t pos, char ch) {
assert(pos <= _size);
Reserve(_size + 1);
size_t end = _size;
while (end >= pos) {
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
_size++;
}*/
String& Insert(size_t pos, char ch) {
String pCh;
pCh = Substr(pos, _size - pos);
_size = pos;
*this += ch;
*this += pCh;
return *this;
}
/*插入一个字符串*/
/*void Insert(size_t pos, const char* str) {
assert(pos <= _size);
Reserve(_size + strlen(str) + 1);
for (size_t i = 0; i < strlen(str); ++i) {
Insert(pos++, str[i]);
}
}*/
String& Insert(size_t pos, const char* str) {
String pStr;
pStr = Substr(pos, _size - pos);
_size = pos;
*this += str;
*this += pStr;
return *this;
}
/*删除一串字符串*/
void Erase(size_t pos, size_t len) {
assert(pos <= _size);
size_t start = pos + len;
size_t end = _size;
while (start <= end) {
_str[pos++] = _str[start++];
}
_size -= len;
}
char& operator[](size_t index) {
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index) const {
assert(index < _size);
return _str[index];
}
/*从pos起始, 查找一个字符*/
size_t Find(char ch, size_t pos = 0) {
size_t i = 0;
while (_str[i]) {
if (ch == _str[i]) {
return i;
}
++i;
}
return -1;
}
/*pos起始, 查找一个字符串*/
size_t Find(const char* str, size_t pos = 0) {
size_t sz = strlen(str);
if (sz > _size - pos)
return String::npos;
for (size_t i = 0; i < _size; ++i) {
size_t j = 0;
if (_str[i] == str[j]) {
for (j = 1; j < sz; ++j) {
if (str[j] != _str[i + j])
break;
}
if (j >= sz)
return i;
}
}
return String::npos;
}
char* Getstr(const String& s) const{
return s._str;
}
private:
friend std::ostream& operator<<(std::ostream& _cout, const String s);
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
std::ostream& operator<<(std::ostream& _cout, const String& s) {
std::cout << s.String::Getstr(s);
return _cout;
}
size_t String::npos = -1;
}