String类主要模拟实现string(库)中字符串的增删查改操作。有插入一个字符,插入一个字符串,删除一个特定位置的字符,删除特定位置特定的字符串,头插,头删,尾插,尾删,可以复用Insert()和Erase()函数。字符串比较,字符相同则同时向后走,当字符串相同,或有不同的字符时返回相应的结果。具体操作如下:
#pragma warning(disable:4996)//禁止报strcpy的错误
#include<iostream>
#include<cassert>
using namespace std;
class String
{
public:
String(const char* str ="")
{
size_t len = strlen(str);
_str = new char[len+1];
strcpy(_str,str);
_size = len;
_capacity = len;
}
//s1(s2)
String(const String& s)
:_str(NULL)
,_size(0)
,_capacity(0)
{
String tmp(s._str);
Swap(tmp);
}
//d1=d2
String& operator=(String s)//现代写法
{
this->Swap(s);
return *this;
}
void Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
~String()
{
if(_str)
{
delete[] _str;
_str = NULL;
_size = _capacity = 0;
}
}
char* GetStr()
{
return _str;
}
size_t Size()
{
return _size;
}
size_t Capacity()
{
return _capacity;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
void Expand(size_t n)
{
assert(n > _capacity);
_str = (char*)realloc(_str,n+1);
_capacity = n;
}
void PushBack(char ch)//尾插一个字符
{
if(_size == _capacity)
{
Expand(_capacity*2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void PushBack(const char* str)//尾插一个字符串
{
size_t len = strlen(str);
if(_size+len > _capacity)
{
Expand(_size+len);
}
strcpy(_str+_size,str);
}
void PopBack()
{
if(_size)
{
--_size;
_str[_size] = '\0';
}
}
void PushFront(char ch)
{
Insert(0,ch);
}
void PushFront(const char* str)
{
Insert(0,str);
}
void PopFront()
{
if(_str)
{
strcpy(_str,_str+1);
--_size;
}
}
void Insert(size_t pos, char ch)//在pos前面插入一个字符
{
if(_size == _capacity)
Expand(_capacity*2);
//挪动数据
int end = _size;
while(end >= (int)pos)
{
_str[end+1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
void Insert(size_t pos, const char* str)//在pos前面插入一个字符串
{
size_t len = strlen(str);
if(_size+len > _capacity)
Expand(_size+len);
int end = _size;
while(end >= (int)pos)
{
_str[end+len] = _str[end];
--end;
}
while(*str)
_str[pos++] = *str++;
_size = _size + len;
}
void Erase(size_t pos, size_t count)
{
if(pos+count >= _size-1)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str+pos, _str+pos+count);
//把pos+count之后的内容拷贝到pos后
_size -= count;
}
}
int Find(char ch) const
{
for(size_t i=0; i<_size; ++i)
{
if(_str[i] == ch)
return i;
}
return -1;
}
int Find(const char* str) const
{
assert(str);
const char* srcStr = _str;
const char* subStr = str;
size_t subLen = strlen(str);
size_t srcIndex = 0;
size_t subIndex = 0;
while(srcIndex < _size)
{
size_t matchIndex = srcIndex;
while(srcStr[matchIndex] == subStr[subIndex])
{
++matchIndex;
++subIndex;
if(subIndex == subLen)
return srcIndex;
}
subIndex = 0;
srcIndex++;
}
return -1;
}
bool operator<(const String& s)
{
size_t i=0;
for( ;i<_size && i<s._size; ++i)
{
if(_str[i] < s._str[i])
return true;
else if(_str[i] > s._str[i])
return false;
}
if(i == _size && i<s._size)//i是已经for循环过的值
return true;
else
return false;
}
inline bool operator<=(const String& s)
{
return *this<s || *this == s;
}
inline bool operator>(const String& s)
{
return !(*this<=s);
}
bool operator>=(const String& s)
{
return !(*this<=s) || *this == s;
}
bool operator==(const String& s)
{
size_t i =0;
for(; i<_size && i<s._size; ++i)
{
if(_str[i] != s._str[i])
return false;
}
if(i == _size && i == s._size)
return true;
else
return false;
}
bool operator!=(const String& s)
{
return !(*this == s);
}
private:
char* _str;
size_t _size;//字符个数
size_t _capacity;//容量空间
};
测试用例:
#include"String.h"
void TestString1()
{
String s1("hello");
String s2(s1);
//cout<<s1.GetStr()<<endl;
//cout<<s2.GetStr()<<endl;
s1.PushBack(' ');
s1.PushBack('C');
s1.PushBack('h');
s1.PushBack('i');
s1.PushBack('n');
s1.PushBack('a');
cout<<s1.GetStr()<<endl;
/*s2.PushBack(" China");
cout<<s2.GetStr()<<endl;*/
s1.PushFront(' ');
s1.PushFront('m');
cout<<s1.GetStr()<<endl;
/*s2.PopBack();
s2.PopBack();
s2.PopBack();
s2.PopBack();
s2.PopBack();
s2.PopBack();*/
// cout<<s2.GetStr()<<endl;
s2.PopFront();
s2.PopFront();
s2.PopFront();
s2.PopFront();
s2.PopFront();
s2.PopFront();
cout<<s2.GetStr()<<endl;
String s3("world");
s3.Insert(0,"hello ");
s3.Insert(6,'m');
cout<<s3.GetStr()<<endl;
//s3.Erase(7,2);
//cout<<s3.GetStr()<<endl;
cout<<"Pos"<<s3.Find('w')<<endl;
cout<<"Pos"<<s3.Find("wor")<<endl;
}
void TestString2()
{
String s1("computer");
String s2("compare");
String s3("computerly");
String s4("computer");
bool ret;
//ret = s1<s2;
//cout<<"s1<s2 ? "<<ret<<endl;
//ret = s1<s3;
//cout<<"s1<s3 ? "<<ret<<endl;
/*ret = s1>s2;
cout<<"s1 >s42? "<<ret<<endl;*/
/*ret = s1>=s2;
cout<<"s1 >= s2 ? "<<ret<<endl;*/
ret = s1<=s2;
cout<<"s1 <= s2 ? "<<ret<<endl;
}
int main()
{
//TestString1();
TestString2();
return 0;
}