#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
namespace test
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_size + 1];
memcpy(_str, str, _size + 1);
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size, s._size);
::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
,_size(0)
,_capacity(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
memcpy(tmp, _str, _size + 1);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
void append(const char* str)
{
size_t len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
memcpy(_str + _size, str, len + 1);
_size += len;
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = ch;
++_size;
return *this;
}
void insert(size_t pos, size_t n, char ch)
{
assert(pos <= _size);
if (_size + n > _capacity)
{
reserve(_size + n);
}
size_t end = _size + n;
while (end >= pos+n)
{
_str[end] = _str[end - n];
--end;
}
for (size_t i = pos; i < pos + n; i++)
{
_str[i] = ch;
}
_size += n;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
assert(str);
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size;
while (end + len >= pos + len)
{
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, str, len);
return *this;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
}
else
{
memcpy(_str + pos, _str + pos + len, _size - pos - len + 1);
_size = _size - len;
}
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str()const
{
return _str;
}
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 clear()
{
_str[0] = '\0';
_size = 0;
}
size_t find(char ch, size_t pos = 0)const
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (ch == _str[i])
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos = 0)const
{
assert(sub);
assert(pos < _size);
const char* ptr = strstr(_str + pos,sub);
if (ptr == nullptr)
{
return npos;
}
return ptr - _str;
}
string substr(size_t pos, size_t len = npos)const
{
assert(pos < _size);
size_t realLen = len;
if (len == npos || len + pos > _size)
{
realLen = _size - pos;
}
string sub;
for (size_t i = pos; i < pos + realLen; i++)
{
sub += _str[i];
}
sub += '\0';
return sub;
}
void resize(size_t n, char ch = '\0')
{
if (n > _size)
{
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
else
{
_str[n] = '\0';
_size = n;
}
}
bool operator>(const string& s) const
{
int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
return ret == 0 ? _size < s._size : ret < 0;
}
bool operator==(const string& s)const
{
return memcpy(_str, s._str, _size) == 0 && _size == s._size;
}
bool operator>=(const string& s)
{
return *this == s || *this > s;
}
bool operator<=(const string& s)const
{
return *this == s || !(*this > s);
}
bool operator!=(const string& s)
{
return !(*this == s);
}
private:
char* _str;
size_t _size;
size_t _capacity;
public:
const static size_t 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;
ch = in.get();
const size_t N = 32;
char buff[N];
size_t i = 0;
while(ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == N-1)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
buff[i] = '\0';
s += buff;
return in;
}
void test_string()
{
string s1("hello world");
string s2;
cout << "返回首地址输出" << endl;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
cout << "方括号输出" << endl;
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
for (size_t i = 0; i < s1.size(); i++)
{
s1[i]++;
cout << s1[i] << " ";
}
cout << endl;
cout << "迭代器输出" << endl;
string::iterator it = s1.begin();
while (it != s1.end())
{
(*it)++;
cout << *it << " ";
++it;
}
cout << endl;
}
void test_string2()
{
string s1("hello world");
string s2(s1);
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s2[0] = 'x';
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s2 = s1;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
}
void test_string3()
{
string s1("hello world");
string s3;
s3 = s1;
s3 = s3;
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
cout << s3.capacity() << endl;
s3 += 'x';
s3 += 'x';
s3 += 'x';
s3 += 'x';
s3 += 'x';
s3 += 'x';
s3 += 'x';
s3 += "666666";
cout << s3.c_str() << endl;
cout << s3.capacity() << endl;
}
void test_string4()
{
string s1("hello ");
s1.push_back('w');
cout << s1.c_str() << endl;
s1.append("orld");
cout << s1.c_str() << endl;
s1.insert(0,'x');
cout << s1.c_str() << endl;
s1.insert(0, "333333 ");
cout << s1.c_str() << endl;
s1.insert(3, 6,'6');
cout << s1.c_str() << endl;
}
void test_string5()
{
string s1("hello ");
s1.erase(1);
cout << s1.c_str() << endl;
string s2("hello ");
s1.erase(1,10);
cout << s1.c_str() << endl;
string s3("hello ");
s1.erase(1, 2);
cout << s1.c_str() << endl;
}
void test_string6()
{
string s1("hello ");
cout << s1<< endl;
cout << s1.c_str() << endl;
s1 += '\0';
s1 += "world";
cout << s1 << endl;
cout << s1.c_str() << endl;
string s2("hello"), s3;
cin >> s2 >> s3;
cout << s2 <<"----------" << s3 << endl;
}
void test_string7()
{
string s1("hello ");
s1.resize(7, '7');
cout << s1 << endl;
string s2("hello ");
s2.resize(1);
cout << s2 << endl;
}
void test_string8()
{
string s1("aello ");
string s2("bello ");
bool P = (s1 <= s2);
cout << P << endl;
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"
int main()
{
try
{
test::test_string8();
}
catch (const exception& e)
{
cout << e.what() << endl;
}
return 0;
}