#include
#include
using namespace std;
namespace mystring
{
class string
{
public:
typedef char* iterator;
public:
string(const char* str = "")
:_size(strlen(str))
,_capacity(strlen(str))
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const string& s)
{
_capacity = s._capacity;
_size = s._size;
_str = new char[_capacity + 1];
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
~string()
{
delete[] _str;
_size = _capacity = 0;
}
// iterator
const iterator begin() const
{
return _str;
}
const iterator end() const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
// modify
void push_back(char c)
{
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
_str[_size] = c;
_size++;
_str[_size] = '\0';
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(string& s)
{
char* tmp = _str;
_str = s._str;
s._str = tmp;
int itmp = _size;
_size = s._size;
s._size = itmp;
itmp = _capacity;
_capacity = s._capacity;
s._capacity = itmp;
}
const char* c_str()const
{
return _str;
}
// capacity
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t n, char c = '\0')
{
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else
{
if (n <= _capacity)
{
while (_size < n)
{
_str[_size] = c;
_size++;
}
_str[_size] = '\0';
}
else
{
size_t newcapacity = _capacity == 0 ? n + 1 : _capacity + n + 1;
reserve(newcapacity);
while (_size < n)
{
_str[_size] = c;
_size++;
}
_str[_size] = '\0';
}
}
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
// access
char& operator[](size_t index)
{
return _str[index];
}
const char& operator[](size_t index)const
{
return _str[index];
}
//relational operators
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 *this > s || *this == s;
}
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 0;
}
bool operator!=(const string& s)
{
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const
{
for (int i = pos; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return -1;
}
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const
{
//int len = strlen(s);
//for (int i = 0; i <= _size - len; i++)
//{
// if (strncmp(s, _str + i, len) == 0)
// {
// return i;
// }
//}
//return -1;
const char* p = strstr(_str + pos, s);
if (p)
{
return p - _str;//指针相减得到下标
}
else
{
return npos;
}
}
// 在pos位置上插入字符c/字符串str
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
_size++;
return *this;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
int len = strlen(str);
if (_size + len >= _capacity)
{
size_t newcapacity = _capacity == 0 ? len + 1 : _size + len + 1;
reserve(newcapacity);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
end--;
}
for (int i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
_size += len;
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = pos;
while (begin <= _size - len)
{
_str[begin] = _str[begin + len];
begin++;
}
_size -= len;
}
return *this;
}
private:
char* _str;
size_t _capacity;
size_t _size;
const static 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];
//}
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
char ch;
ch = in.get();//一个字符一个字符取
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
}
void Test1()
{
string s1("Hello World");
cout << s1.c_str() << endl;
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
it++;
}
cout << endl;
s1.append("hello world");
s1.push_back('a');
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
cout << "---------------------------------------------" << endl;
}
void Test2()
{
string s1;
s1 += '!';
s1 += " hello world ";
s1 += '!';
s1.insert(0, '#');
s1.insert(s1.size(), '*');
cout << s1.c_str() << endl;
s1.insert(0, "abcdefg");
cout << s1.c_str() << endl;
s1.insert(s1.size(), " xxxxxx");
cout << s1.c_str() << endl;
cout << "---------------------------------------------" << endl;
}
void Test3()
{
string s1;
s1.insert(0, "abcdefg");
s1.erase(6, 1);
cout << s1.c_str() << endl;
cout << "---------------------------------------------" << endl;
}
void Test4()
{
string s1("Hello World");
string s2("Hello World");
cout << s1 << " " << s2 << endl;
cout << (s1 <= s2) << endl;
cout << (s1 >= s2) << endl;
cout << (s1 < s2) << endl;
cout << (s1 < s2) << endl;
cout << "---------------------------------------------" << endl;
}
void Test5()
{
string s1("aaabbb");
string s2(s1);
cout << s1 << endl;
cout << s2 << endl;
s1[0] = '#';
swap(s1, s2);
cout << s1 << endl;
cout << s2 << endl;
s1.resize(2);
s2.resize(20);
cout << s1 << endl;
cout << s2 << endl;
cout << "---------------------------------------------" << endl;
}
void Test6()
{
string s1 = " abcdefg";
cout << s1.find("de") << endl;
cout << s1.find("ccc") << endl;
cout << s1.find("g") << endl;
s1.resize(2);
cout << s1 << endl;
s1.resize(20,'x');
cout << s1 << endl;
cout << "---------------------------------------------" << endl;
}