简单复制和拷贝功能的实现
常规写法
#pragma once
#include<iostream>
using namespace std;
namespace lich
{
class string
{
public:
string(const char* str)
:_str(new char[strlen(str)+1])
{
strcpy(_str, str);
}
string(const string& s)
:_str(new char[strlen(s._str) + 1])
{
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[strlen(s._str) + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
}
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
}
private:
char* _str;
};
void test_string()
{
string s1("hello lich");
string s2(s1);
string s3("sort");
s1 = s3;
}
}
简易写法
namespace lich1
{
class string
{
public:
string(const char* str)
:_str(new char[strlen(str) + 1])
{
strcpy(_str, str);
}
string(const string& s)
:_str(nullptr)
{
string tmp(s._str);
swap(_str, tmp._str);
}
string& operator=(string s)
{
swap(_str, s._str);
return *this;
}
~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
}
含有基本功能的string类的实现:
#pragma once
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
namespace lich
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const iterator end() const
{
return _str + _size;
}
string(const char* str = "")
:_size(strlen(str))
, _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const size_t n, const char ch)
:_size(n)
, _capacity(_size)
{
_str = new char[_capacity + 1];
for (size_t i = 0; i < n; i++)
{
_str[i] = ch;
}
_str[_size] = '\0';
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size, s._size);
::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(string s)
{
swap(s);
return *this;
}
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()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void resize(size_t n, char ch = '\0')
{
if (n < _size)
{
_str[n] = '\0';
_size = n;
}
else
{
if (n > _size)
{
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)
{
size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(newcapacity);
}
_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;
}
string& operator+=(char ch)
{
push_back(ch);
return*this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string& operator+=(const string& s)
{
*this += s._str;
return *this;
}
size_t size() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(newcapacity);
}
for (size_t i = _size + 1; i > pos; --i)
{
_str[i] = _str[i - 1];
}
_str[pos] = ch;
++_size;
return *this;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
for (size_t i = _size + len; i >= (pos + len); i--)
{
_str[i] = _str[i - len];
}
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
_size += len;
return *this;
}
string& erase(size_t pos = 0, size_t len = npos)
{
assert(pos < _size);
if (len >= (_size - pos))
{
_str[pos] = '\0';
_size = pos;
}
else
{
for (size_t i = pos + len; i <= _size; i++)
{
_str[i - len] = _str[i];
}
_size -= len;
}
return *this;
}
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* sub, size_t pos = 0)
{
const char* p = strstr(_str + pos, sub);
if (p)
return p - _str;
return npos;
}
const char* c_str()
{
return _str;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
private:
char* _str;
int _size;
int _capacity;
static const size_t npos;
};
const size_t string::npos = -1;
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
char ch = in.get();
while (ch != '\n' && ch != ' ')
{
s += ch;
ch = in.get();
}
return in;
}
istream& getline(istream& in, string& s)
{
s.clear();
char ch = in.get();
while (ch != '\n')
{
s += in.get();
ch = in.get();
}
return in;
}
string operator+(const string& s1, const char ch)
{
string ret = s1;
ret += ch;
return ret;
}
string operator+(const string& s1, const string& s2)
{
string ret = s1;
ret += s2;
return ret;
}
bool operator>(const string& s1, const string& s2)
{
size_t i1 = 0, i2 = 0;
while (s1.size() > i1 && s2.size() > i2)
{
if (s1[i1] > s2[i2])
{
return true;
}
else if (s1[i1] < s2[i2])
{
return false;
}
else
{
++i1;
++i2;
}
}
if (i1 < s1.size())
{
return true;
}
return false;
}
bool operator==(const string& s1, const string& s2)
{
size_t i1 = 0, i2 = 0;
while (i1 < s1.size() && i2 < s2.size())
{
if (s1[i1] > s2[i2])
{
return false;
}
else if (s1[i1] < s2[i2])
{
return false;
}
else
{
++i1;
++i2;
}
}
if (i1 == s1.size() && i2 == s2.size())
{
return true;
}
return false;
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
bool operator>=(const string& s1, const string& s2)
{
return (s1 > s2 || s1 == s2);
}
bool operator<(const string& s1, const string s2)
{
return !(s1 >= s2);
}
bool operator<=(const string& s1, const string& s2)
{
return !(s1 > s2);
}
}