头文件
#include<iostream>
#include<assert.h>
using namespace std;
namespace asd
{
typedef char* iterator;
class string
{
public:
string(const char* str = "");
string(const string& s);
string& operator=(const string& s);
~string();
iterator begin();
iterator end();
void push_back(char c);
string& operator+=(char c);
void append(const char* str);
string& operator+=(const char* str);
void clear();
void swap(string& s);
const char* c_str() const;
size_t size() const;
size_t capacity() const;
bool empty() const;
void resize(size_t n, char c = '\0');
void reserve(size_t n);
char& operator[](size_t pos);
const char& operator[](size_t pos) const;
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
//返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const;
//返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const;
//在pos位置上插入字符c/字符串str,并返回该字符的位置
string& insert(size_t pos, char c);
string& insert(size_t pos, const char* str);
//删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len);
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos = -1;
};
}
源文件
#include"string.h"
asd::string::string(const char* str)
:_size(strlen(str))
,_capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
asd::string& asd::string::operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_capacity = s._capacity;
_size = s._size;
}
return *this;
}
asd::string::string(const string& s)
{
//_str = new char[s._capacity + 1];
//strcpy(_str, s._str);
//_size = s._size;
//_capacity = s._capacity;
string tmp(s._str); //通过构造函数,对tmp创建
std:: swap(_str, tmp._str);
std::swap(_size, tmp._size);
std::swap(_capacity, tmp._capacity);
}
asd::string::~string()
{
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
asd::iterator asd::string::begin()
{
return _str;
}
asd::iterator asd::string::end()
{
return _str + _size;
}
void asd::string::reserve(size_t n)
{
char* tmp = new char[n + 1];//多开一个用于存放'\0'
_capacity = n;
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
}
void asd::string::push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
}
asd::string& asd::string::operator+=(char c)
{
this->push_back(c);
return *this;
}
const char* asd::string::c_str() const
{
return _str;
}
void asd::string::append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
for (size_t i = 0; i < len; i++)
{
*this += str[i];
}
_size += len;
//strcpy(_str + _size, str);
//_size += len;
}
asd::string& asd::string::operator+=(const char* str)
{
this->append(str);
return *this;
}
void asd::string::clear()
{
_str[0] = '\0';
_size = 0;
}
void asd::string::swap(string& s)
{
string tmp = s;
s = *this;
*this = tmp;
}
size_t asd::string::size() const
{
return _size;
}
size_t asd::string::capacity() const
{
return _capacity;
}
bool asd::string::empty() const
{
return _size == 0;
}
void asd::string::resize(size_t n, char c)
{
if (n <= _size)
{
_str[n] = '\0';
}
else
{
reserve(n);
while (_size != n)
{
_str[_size] = c;
_size++;
}
_str[_size] = '\0';
}
}
char& asd::string::operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& asd::string::operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
bool asd::string::operator<(const string& s)
{
return strcmp(this->_str, s._str) < 0;
}
bool asd::string::operator<=(const string& s)
{
return *this < s || *this == s;
}
bool asd::string::operator>(const string& s)
{
return !(*this <= s);
}
bool asd::string::operator>=(const string& s)
{
return !(*this < s);
}
bool asd::string::operator==(const string& s)
{
return strcmp(this->_str, s._str) == 0;
}
bool asd::string::operator!=(const string& s)
{
return !(*this == s);
}
size_t asd::string::find(char c, size_t pos) const
{
if (pos >= _size)
{
return npos;
}
while (pos != _size)
{
if (_str[pos++] == c)
{
return pos;
}
}
return npos;
}
size_t asd::string::find(const char* s, size_t pos) const
{
if (pos > _size || strlen(s) > _size)
{
return npos;
}
const char* tmp = s;
while (_str[pos] != '\0')
{
if (_str[pos] != *tmp)
{
tmp = s;
}
else
{
tmp++;
}
pos++;
}
if (tmp == s)
{
return npos;
}
return pos - strlen(s) + 1;
}
asd::string& asd::string::insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t len = _size;
while (len != pos)
{
_str[len] = _str[len - 1];
len--;
}
_str[pos] = c;
_size++;
_str[_size] = '\0';
return *this;
}
asd::string& asd::string::insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len_str = strlen(str);
if (_size + len_str > _capacity)
{
reserve(_size + len_str);
}
size_t len_this_str = _size;
while (len_this_str != pos)
{
_str[len_this_str + len_str - 1] = _str[len_this_str - 1];
len_this_str--;
}
strncpy(_str + pos, str, len_str);
_size += len_str;
_str[_size] = '\0';
return *this;
}
asd::string& asd::string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
while (_str[pos + len] != '\0')
{
_str[pos] = _str[pos + len];
pos++;
}
_size -= len;
_str[_size] = '\0';
}
return *this;
}