string的应用是非常典型的,下面我写了一些简单的string类的相关的一些操作,包括增删查改等一些功能和一些运算符的重载,包括他们的测试用例:
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
#define CARA 3;
class String
{
public:
String(char *str = "")
:_str(new char[strlen(str) + 1])
, _size(strlen(str))
, _capacity(strlen(str)+1)
{
strcpy(_str, str);
}
String(const String& s)
:_str(NULL)
{
String tmp(s._str);
swap(_str,tmp._str);
}
char& operator[](int size)
{
return _str[size];
}
String& operator=(const String& s)
{
if (this != &s)
{
String tmp(s._str);
swap(_str, tmp._str);
}
return *this;
}
void _CheckCapacity(size_t capacity)
{
if (capacity > _capacity)
{
_capacity = capacity + CARA;
_str = (char*)realloc(_str, _capacity);
assert(_str);
}
}
void PushBack(char ch)
{
_CheckCapacity(_size+2);
_str[_size++] = ch;
_str[_size] = '\0';
//或者:
/*Insert(_size+1,ch);
_size++;*/
}
void PopBack()
{
--_size;
_str[_size] = '\0';
}
void PushFront(char ch)
{
_CheckCapacity(_size + 2);
for (int begin = _size; begin >= 0; begin--)
{
_str[begin + 1] = _str[begin];
}
_str[0] = ch;
_size++;
}
void PopFront()
{
for (int begin = 1; begin <= _size; begin++)
{
_str[begin - 1] = _str[begin];
}
_size--;
}
void Insert(size_t pos, char ch)
{
_CheckCapacity(_size + 2);
for (int begin = _size; begin >= pos-1; begin--)
{
_str[begin + 1] = _str[begin];
}
_str[pos-1] = ch;
_size++;
}
void Insert(size_t pos, char* str)
{
_CheckCapacity(_size + strlen(str)+1);
//char *strncpy(char *strDest, const char *strSource, size_t count); strncpy的用法
int end = strlen(str) + _size;
for (int begin = _size; begin >= pos; begin--)
{
_str[end] = _str[begin];
end--;
}
strncpy(_str+pos, str, strlen(str));
_size += strlen(str);
}
int Find(const String& s)
{
if (_size < s._size)
{
return -1;
}
int srcIndex = 0;
int desIndex = 0;
int tmp = 0;
while (srcIndex<_size)
{
tmp = srcIndex;
while (desIndex < s._size)
{
if (_str[srcIndex] == s._str[desIndex])
{
srcIndex++;
desIndex++;
}
else
{
desIndex = 0;
srcIndex = tmp + 1;
break;
}
}
if (desIndex == s._size)
{
return tmp;
}
}
return -1;
}
bool operator<(const String& s)
{
char*begin1 = _str;
char*begin2 = s._str;
while (*begin1 && *begin2)
{
if (*begin1 < *begin2)
{
return true;
}
else if (*begin1 > *begin2)
{
return false;
}
else
{
begin1++;
begin2++;
}
}
return *begin1 < *begin2;
}
bool operator==(const String& s)
{
char*begin1 = _str;
char*begin2 = s._str;
if (_size != s._size)
{
return false;
}
while (*begin1 && *begin2)
{
if (*begin1 != *begin2)
{
return false;
}
else
{
begin1++;
begin2++;
}
}
return true;
}
bool operator>( const String& s)
{
return !((*this < s) ||( *this == s));
}
char* Disply()
{
return _str;
}
~String()
{
if (_str)
{
delete[]_str;
}
}
private:
char *_str;
int _size;
int _capacity;
};
void Test1()
{
String s1("abcde");
String s2(s1);
cout << s2.Disply() << endl;
String s3;
s3 = s1;
cout << s3.Disply() << endl;
}
void Test2() //测试PushBack
{
String s1("abcde");
s1.PushBack('z');
cout << s1.Disply() << endl;
}
void Test3() //测试Insert一个字符
{
String s1("abcde");
s1.Insert(4,'z');
cout << s1.Disply() << endl;
}
void Test4() //测试Insert一个字符串
{
String s1("abcde");
s1.Insert(5, "fgh");
cout << s1.Disply() << endl;
}
void Test5() //测试PopBack
{
String s1("abcde");
cout << s1.Disply() << endl;
s1.PopBack();
cout << s1.Disply() << endl;
}
void Test6() //测试<
{
String s1("ab");
String s2("abc");
cout << (s1 < s2) << endl;
}
void Test7() //测试==
{
String s1("ab");
String s2("abc");
cout << (s1 == s2) << endl;
}
void Test8() //测试>
{
String s1("a");
String s2("abc");
cout << (s1 > s2) << endl;
}
void Test9()
{
String s1("abcdabcef");
int ret = s1.Find("efg");
cout << ret << endl;
}
void Test10()
{
String s1("abcd");
s1.PushFront('a');
cout << s1.Disply() << endl;
}
void Test11()
{
String s1("abcd");
s1.PopFront();
cout << s1.Disply() << endl;
}
int main()
{
//Test1();
Test2(); //测试PushBack
//Test3();//测试Inser
//Test4(); //测试Insert一个字符串
//Test5();//测试PopBack
//Test6(); //测试<
//Test7(); //测试==
//Test8(); //测试>
//Test9(); //测试 Find
//Test10(); //测试PushFront
//Test11(); //测试PopFront
return 0;
}
转载于:https://blog.51cto.com/10706198/1763142