#include<iostream>
#include<assert.h>
using namespace std;
#pragma warning(disable:4996)
#define DEFAULT_CAPACITY 5
class String
{
public:
String(const char* str = "")
:_str(new char[strlen(str) + 1])
, _size(strlen(str))
, _capacity(DEFAULT_CAPACITY)
{
strcpy(_str,str);
}
String(const String& s)
:_str(new char[strlen(s._str) + 1])
, _size(s._size)
, _capacity(s._capacity)
{
strcpy(_str, s._str);
}
~String()
{
if (_str)
{
delete[] _str;
}
}
String& operator=( String& s)
{
if (this != &s)
{
swap(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
public:
void PushBack(char ch)
{
//_size = strlen(_str);
CheekCapacity(_size + 2);
_str[_size++] = ch;
_str[_size] = '\0';
}
void PopBack()
{
if (_str != NULL)
{
//_size = strlen(_str);
_str[_size - 1] = '\0';
_size--;
}
else
{
cout << "string is empty" << endl;
return;
}
}
void Insert(int pos, char ch)
{
//_size = strlen(_str);
CheekCapacity(_size + 2);
if ((pos >= 0) && (pos <= _size))
{
for (int i = _size + 1; i > pos; i--) //连‘\0’一起往后挪
{
_str[i] = _str[i - 1];
}
_str[pos] = ch;
_size++;
}
}
void InsertStr(int pos, const char* str)
{
int len = strlen(str);
CheekCapacity(_size + 1 + len);
if ((pos >= 0) && (pos <= _size))
{
for (int i = _size; i >= pos; i--)
{
_str[i + len] = _str[i];
}
}
while (len)
{
_str[pos++] = *str++;
len--;
}
_size += len;
}
int Find(char ch)
{
int i = 0;
while (*_str != '\0')
{
if (*_str == ch)
{
cout << "find " << ch << endl;
return i;
}
_str++;
i++;
}
return -1;
}
int FindStr(const char* str)
{
int i, j, k;
assert(str);
for ( i = 0; i < strlen(_str); i++)
{
for ( k = i,j = 0; j < strlen(str); k++, j++)
{
if (_str[k] != str[j])
{
break;
}
}
if (str[j] == '\0' && k > 0)
{
cout << "find " << str << endl;
return 1;
}
}
return 0;
}
public:
bool operator<( String & s)
{
while (*_str && *s._str)
{
if (*_str < *s._str)
{
return true;
}
if (*_str == *s._str)
{
_str ++ ;
s._str++;
}
else
{
return false;
}
}
if (*_str)
{
return false;
}
else if (*s._str)
{
return true;
}
return false;
}
bool operator==( String & s)
{
while (*_str && *s._str)
{
if (*_str == *s._str)
{
_str++;
s._str++;
}
else
{
return false;
}
}
if (*_str || *s._str)
{
return false;
}
return true;
}
bool operator>=( String & s)
{
return !(*this < s);
}
bool operator<=(String & s)
{
return (*this < s) || (*this == s);
}
bool operator>( String & s)
{
return !(*this <= s);
}
String operator+(const String& s)
{
InsertStr(_size, s._str);
_size += strlen(s._str);
return *this;
}
private:
void CheekCapacity(size_t capacity)
{
if (_capacity < capacity)
{
_capacity *= capacity;
char* tmp = new char[_capacity];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
}
}
friend ostream& operator<<(ostream& out, const String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
ostream& operator<<(ostream& out, const String& s)
{
out << s._str;
return out;
}
void Test()
{
String s1("abcd");
s1.PushBack('e');
s1.PushBack('f');
s1.PushBack('g');
cout << s1<<endl;
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.PopBack();
//s1.PopBack();
cout << s1<<endl;
}
void Test2()
{
String s1("abcd");
s1.InsertStr(4, "yangrujing");
cout << s1;
s1.Find('f');
}
void Test3()
{
String s1("yangrujing");
String s2("ru");
//cout << "s1 < s1 ? : " <<(s1 < s2)<< endl;
//cout << "s1 = s1 ? : " << (s1 == s2) << endl;
//cout << "s1 >= s1 ? : " << (s1 >= s2) << endl;
//cout << "s1 <= s1 ? : " << (s1 <= s2) << endl;
//cout << "s1 > s1 ? : " << (s1 > s2) << endl;
//cout << "s1 + s2 = " << s1 + s2 << endl;
s1.FindStr("jing");
}
int main()
{
/*String s1("abcd");
String s2;
s2 = s1;
cout << s2;*/
Test3();
getchar();
return 0;
}
模拟c++的string类
最新推荐文章于 2024-10-13 17:13:37 发布