头文件
#pragma once
#include<iostream>
using namespace std;
class myString {
public:
typedef char* iterator;
public:
//构造和析构
myString(const char* s = "");
myString(const myString& str);
myString& operator=(const myString& str);
~myString();
//迭代器
iterator begin();
iterator end();
//容量
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 index);
const char& operator[](size_t index)const;
//修改
void push_back(char c);
myString& operator+=(const char* s);
void append(const char* s);
void clear();
void swap(myString& s);
//其他
const char* c_str()const;
bool operator<(const myString& s);
bool operator<=(const myString& s);
bool operator>(const myString& s);
bool operator>=(const myString& s);
bool operator==(const myString& s);
bool operator!=(const myString& 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,并返回该字符的位置
myString& insert(size_t pos, char c);
myString& insert(size_t pos, const char* str);
// 删除pos位置上的元素,并返回该元素的下一个位置
myString& erase(size_t pos, size_t len);
private:
friend ostream& operator<<(ostream& _cout, const myString& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
源文件:
#include"myString.h"
#include<assert.h>
//构造和析构
myString::myString(const char* s) {
if (s == nullptr) {
assert(0);
}
_str = new char[strlen(s) + 1];
_size = strlen(s);
_capacity = _size;
strcpy(_str, s);
}
//void myString::swap(myString& str) {
// std::swap(_str, str._str);
// std:: swap(_capacity, str._capacity);
// std::swap(_size, str._size);
//}
myString::myString(const myString& str)
:_str(nullptr)
,_size(0)
,_capacity(0){
//深拷贝
myString temp(str._str);
std::swap(_str, temp._str);
_size = str._size;
_capacity = str._capacity;
}
myString& myString::operator=(const myString& str) {
//深拷贝
if (this != &str) {
myString temp(str._str);
std::swap(_str, temp._str);
_size = str._size;
_capacity = str._capacity;
}
return *this;
}
myString::~myString() {
if (_str) {
delete[] _str;
_str = nullptr;
}
}
//迭代器
myString::iterator myString::begin() {
return _str;
}
myString::iterator myString::end() {
return _str + _size;
}
//容量
size_t myString::size()const {
return _size;
}
size_t myString::capacity()const {
return _capacity;
}
bool myString::empty()const {
return _size == 0;
}
void myString::resize(size_t n, char c) {
if (n > _size) {
//如果大于容量,需要扩容
if (n > _capacity) {
reserve(n);
}
memset(_str + _size, c, n - _size);
}
_size = n;
_str[_size] = '\0';
}
void myString::reserve(size_t n) {
if (n > _capacity) {
//扩容三部曲
char* temp = new char[n + 1];
strcpy(temp, _str);
delete[] _str;
_str = temp;
_capacity = n;
}
}
//元素访问
char& myString::operator[](size_t index) {
assert(index < _size);
return _str[index];
}
const char& myString::operator[](size_t index)const {
assert(index < _size);
return _str[index];
}
//修改
void myString::push_back(char c) {
if (_size == _capacity) {
reserve(_capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
}
myString& myString::operator+=(const char* s) {
append(s);
return *this;
}
void myString::append(const char* s) {
if (_size + strlen(s) > _capacity) {
size_t newcapacity = _capacity * 2 > _size + strlen(s) ? _capacity * 2 : _size + strlen(s) + 1;
reserve(newcapacity);
}
strcat(_str, s);
_size += strlen(s);
}
void myString::clear() {
_str[0] = '\0';
_size = 0;
}
void myString::swap(myString& s) {
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
//其他
const char* myString::c_str()const {
return _str;
}
bool myString::operator<(const myString& s) {
int ret = strcmp(_str, s._str);
if (ret < 0) {
return true;
}
return false;
}
bool myString::operator<=(const myString& s) {
return !(*this > s);
}
bool myString::operator>(const myString& s) {
int ret = strcmp(_str, s._str);
if (ret > 0) {
return true;
}
return false;
}
bool myString::operator>=(const myString& s) {
return !(*this < s);
}
bool myString::operator==(const myString& s) {
int ret = strcmp(_str, s._str);
if (ret == 0) {
return true;
}
return false;
}
bool myString::operator!=(const myString& s) {
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t myString::find(char c, size_t pos) const {
for (size_t i = pos; i < _size; i++) {
if (c == _str[i]) {
return i;
}
}
return -1;
}
// 返回子串s在string中第一次出现的位置
size_t myString::find(const char* s, size_t pos) const {
assert(s);
assert(pos < _size);
const char* cur = _str + pos;
while (*cur != '\0') {
const char* tmp = cur;
const char* s_cur = s;
while (*tmp != '\0' && *s_cur != '\0' && (*tmp == *s_cur)) {
tmp++;
s_cur++;
}
if (*s_cur == '\0') {
return cur - _str;
}
cur++;
}
return -1;
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
myString& myString::insert(size_t pos, char c) {
assert(pos <= _size);
//扩容
if (_size == _capacity) {
reserve(_capacity * 2 + 1);
}
//移数据
for (int i = _size; i >=(int)pos; i--) {
_str[i + 1] = _str[i];
}
_str[pos] = c;
_size++;
return *this;
}
myString& myString::insert(size_t pos, const char* str) {
assert(pos <= _size);
size_t len = strlen(str);
//扩容
if (_size + len > _capacity) {
reserve(_capacity + len + 10);
}
//移数据
for (int i = _size; i >= pos; i--) {
_str[i + len] = _str[i];
}
while (*str != '\0') {
_str[pos++] = *str++;
}
_size += len;
return *this;
}
//删除pos位置上的元素,并返回该元素的下一个位置
myString& myString::erase(size_t pos, size_t len) {
assert(pos < _size);
if (pos + len >= _size) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
//<<重载
ostream& operator<<(ostream& _cout, const myString& s) {
_cout << s._str;
return _cout;
}