string类
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
using namespace std;
namespace zyf {
class Mystring {
public:
typedef char* iterator;
iterator begin() {
return _str;
}
iterator end() {
return _str + _size;
}
Mystring(const char* str = "")
:_size(strlen(str)) {
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
~Mystring() {
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
Mystring(const Mystring& s) {
_str = new char[s._size + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._size;
}
Mystring& operator=(const Mystring& s) {
if (this != &s) {
delete[] _str;
_str = new char[s._size + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._size;
}
return *this;
}
size_t size() const {
return _size;
}
size_t capacity() const {
return _capacity;
}
char& operator[](size_t pos) {
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const {
assert(pos < _size);
return _str[pos];
}
void reserve(size_t n) {
if (n > _capacity) {
char* newstr = new char[n + 1];
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0') {
if (n < _size) {
_size = n;
_str[_size] = '\0';
}
else {
if (n > _capacity)
reserve(n);
for (size_t i = _size; i < n; ++i)
_str[i] = ch;
_size = n;
_str[_size] = '\0';
}
}
void push_back(char ch) {
if (_size == _capacity) {
if (_capacity == 0)
reserve(2);
else
reserve(_capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char* str) {
size_t len = strlen(str);
if (_size + len > _capacity)
reserve(_size + len);
strcpy(_str + _size, str);
_size += len;
}
Mystring& operator+=(char ch) {
this->push_back(ch);
return *this;
}
Mystring& operator+=(const char* str) {
this->append(str);
return *this;
}
Mystring& insert(size_t pos, char ch) {
assert(pos < _size);
if (_size == _capacity)
reserve(_capacity * 2);
size_t end = _size;
while (end >= pos) {
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
Mystring& 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;
while (end >= pos) {
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
void erase(size_t pos, size_t len = npos) {
assert(pos < _size);
if (_size - pos <= len) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
size_t find(char ch, size_t pos = 0) {
for (size_t i = pos; i < _size; ++i) {
if (_str[i] == ch)
return i;
}
return npos;
}
size_t find(const char* str, size_t pos = 0) {
const char* p = strstr(_str + pos, str);
if (p == nullptr)
return npos;
else
return p - _str;
}
bool operator<(const Mystring& s) {
return strcmp(_str, s._str) < 0;
}
bool operator==(const Mystring& s) {
return strcmp(_str, s._str) == 0;
}
bool operator<=(const Mystring& s) {
return *this < s || *this == s;
}
bool operator>(const Mystring& s) {
return !(*this <= s);
}
bool operator>=(const Mystring& s) {
return !(*this < s);
}
bool operator!=(const Mystring& s) {
return !(*this == s);
}
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
size_t Mystring::npos = -1;
ostream& operator<<(ostream& out, const Mystring& s) {
for (size_t i = 0; i < s.size(); ++i)
out << s[i];
return out;
}
istream& operator>>(istream& in, Mystring& s) {
while (1) {
char ch = in.get();
if (ch == ' ' || ch == '\n')
break;
else
s += ch;
}
return in;
}
}
void test1() {
zyf::Mystring s1("hello");
s1.push_back('x');
s1.push_back('y');
s1.append("world");
s1 += '!';
s1 += "hello world";
cout << s1 << endl;
for (size_t i = 0; i < s1.size(); ++i) {
s1[i] += 1;
cout << s1[i] << " ";
}
cout << endl;
zyf::Mystring::iterator it1 = s1.begin();
while (it1 != s1.end()) {
*it1 -= 1;
cout << *it1 << " ";
++it1;
}
cout << endl;
for (auto& ch : s1) {
ch += 1;
cout << ch << " ";
}
cout << endl;
zyf::Mystring s2("hello");
s2 += 'x';
s2.resize(3);
s2.resize(7, 'x');
s2.resize(15, 'x');
}
void test2() {
zyf::Mystring s1("hello");
s1.insert(2, 'e');
cout << s1 << endl;
s1.insert(2, "world");
cout << s1 << endl;
cout << s1.find("lde") << endl;
zyf::Mystring s2("hello world wrold");
cout << s2 << endl;
s2.erase(5, 6);
cout << s2 << endl;
s2.erase(5, 10);
cout << s2 << endl;
}
int main() {
test1();
cout << endl;
test2();
return 0;
}
深浅拷贝
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
using namespace std;
namespace zyf {
class Mystring {
public:
Mystring(const char*str = "")
:_str(new char[strlen(str) + 1]) {
strcpy(_str, str);
}
Mystring(const Mystring& s)
:_str(nullptr) {
Mystring tmp(s._str);
swap(_str, tmp._str);
}
Mystring& operator=(Mystring s) {
swap(_str, s._str);
return *this;
}
~Mystring() {
delete[] _str;
_str = nullptr;
}
const char*c_str() {
return _str;
}
private:
char *_str;
};
}
int main() {
zyf::Mystring s1("hello");
cout << s1.c_str() << endl;
zyf::Mystring s2(s1);
cout << s2.c_str() << endl;
zyf::Mystring s3("world");
s1 = s3;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
getchar();
return 0;
}