<span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS
#define DEFAULT 8
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
class String
{
private:
char* _ptr;//
size_t _size;//
size_t _capacity;//
private:</span>
<span style="font-size:18px;">// : < ,
void _CheckCapacity(size_t capacity)
{
if (_capacity < capacity)
{
_capacity = capacity + DEFAULT;
char* tmp = new char[_capacity];
strcpy(tmp, _ptr);
delete[] _ptr;
_ptr = tmp;
}
}
void Swap(String& s)
{
swap(_ptr, s._ptr);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
void Reserve(size_t capacity)
{
_CheckCapacity(capacity);
}
public:</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">/* , , {} , ; , / , , */
</span>
<span style="font-size:18px;">
String(char* ptr = "")
: _size(strlen(ptr))
, _capacity(_size + DEFAULT)
{
_ptr = new char[_capacity];
strcpy(_ptr, ptr);
}
String(const String& s)
:_ptr(NULL)
, _size(0)
, _capacity(0)
{
String tmp(s._ptr);
Swap(tmp);
}
String& operator=(String s)
{
if (this != &s)
{
Swap(s);
}
return *this;
}
~String()
{
if (_ptr)
{
delete[] _ptr;
_size = 0;
_capacity = 0;
}
}
friend ostream& operator<<(ostream& os, const String& s)
{
cout << s._ptr;
return os;
}
friend istream& operator>>(istream& is, String& s)
{
s.Reserve(100);
is.getline(s._ptr,100);
return is;
}
public:
char* GetStr()
{
return _ptr;
}
void PushBack(char ch)
{
_CheckCapacity(_size + 2);
_ptr[_size++] = ch;
_ptr[_size] = '\0';
}
void PushBack(char* str)
{
_CheckCapacity(_size + strlen(str) + 1);
while (*str)
{
_ptr[_size++] = *str++;
}
_ptr[_size] = '\0';
}
void PopBack()
{
_ptr[--_size] = '\0';
}
void Insert(size_t pos, char ch)
{
assert(pos <= _size);
_CheckCapacity(_size + 2);
int i = _size;
for (; i >= (int)pos; --i)
{
_ptr[i + 1] = _ptr[i];
}
_ptr[pos] = ch;
++_size;
}
void Insert(size_t pos, char* str)
{
assert(pos <= _size);
int len = strlen(str);
_CheckCapacity(_size + len + 1);
int j = _size;
int i = _size + len;
for (; j >= (int)pos;)
{
_ptr[i--] = _ptr[j--];
}
while (*str)
{
_ptr[pos++] = *str++;
}
_size += len;
}
int Find(char ch)
{
int count = 0;
char* tmp = _ptr;
while (_ptr && (*_ptr))
{
if (*_ptr == ch)
{
return count;
}
else
{
++count;
++_ptr;
}
}
_ptr = tmp;
return -1;
}
int Find(char* str)
{
char* tmp = str;
size_t count = 0;
size_t recount = count;
size_t rrcount = 0;
char* pptr = _ptr;
while (_ptr)
{
while (*str)
{
if (*_ptr == *str)
{
++_ptr;
++str;
++rrcount;
}
else
{
++_ptr;
++count;
if (*str != *tmp)
{
str = tmp;
recount = count;
}
}
}
recount = count + rrcount - strlen(tmp);
_ptr = pptr;
return recount;
}
return -1;
}
public:
// C
bool operator<(const String& s)
{
char* s1 = _ptr;
char* s2 = s._ptr;
while (s1 && s2)
{
if (*s1<*s2)
{
return true;
}
else if (*s1>*s2)
{
return false;
}
else
{
++s1;
++s2;
}
}
if (s1 == NULL && s2 != NULL)
{
return true;
}
return false;
}
bool operator<=(const String& s)
{
if (*this<s || *this == s)
{
return true;
}
return false;
}
bool operator>(const String& s)
{
if (*this <= s)
{
return false;
}
return true;
}
bool operator>=(const String& s)
{
if (*this < s)
{
return false;
}
return true;
}
bool operator==(const String& s)
{
char* s1 = _ptr;
char* s2 = s._ptr;
while (*s1 && *s2)
{
if (*s1++ != *s2++)
return false;
}
if (*s1 == *s2)
{
return true;
}
return false;
}
String operator+(const String& s)
{
String tmp(_ptr);
tmp.Insert(_size, s._ptr);
return tmp;
}
String& operator+=(const String& s)
{
Insert(_size, s._ptr);
return *this;
}
};
//
void Test1()
{
String s1("abcd");
String s2(s1);
String s3;
s3 = s2;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
}
// , ,
void Test2()
{
String s1("abcd");
String s2;
s2.PushBack('e');
s2.PushBack('f');
s2.PushBack('g');
s2.PushBack('h');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
s2.PushBack("abcdef");
cout << "s2:" << s2 << endl;
s2.PopBack();
s2.PopBack();
s2.PopBack();
cout << "s2:" << s2 << endl;
s2.Insert(5, 'x');
cout << "s2:" << s2 << endl;
s2.Insert(5, "vbnmg");
cout << "s2:" << s2 << endl;
}
//
void Test3()
{
String s1("abcgefcd");
int ret1 = s1.Find('x');
cout << ret1 << endl;
int ret2 = s1.Find("cd");
cout << ret2 << endl;
}
//
void Test4()
{
String s1("abcdef");
String s2("abcde");
if (s1 < s2)
cout << "True" << endl;
else
cout << "False" << endl;
String s3("abcdef");
String s4("abcdef");
if (s3 == s4)
cout << "True" << endl;
else
cout << "False" << endl;
String s5("abcdefa");
String s6("abcdef");
if (s5 <= s6)
cout << "True" << endl;
else
cout << "False" << endl;
String s7("abcde");
String s8("abcdef");
if (s7 > s8)
cout << "True" << endl;
else
cout << "False" << endl;
String s9("abcdefa");
String s10("abcdef");
if (s9 >= s10)
cout << "True" << endl;
else
cout << "False" << endl;
}
// +and+=
void Test5()
{
String s1("abcdefa");
String s2("abcdef");
s1 += s2;
cout << "s1:" << s1 << endl;
String s3("abcdefa");
String s4("abcdef");
String s5;
s5 = s3 + s4;
cout << "s5:" << s5 << endl;
cin >> s1;
cout << s1 << endl;
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
return 0;
}</span>