String类
编写自定义的string类要运用到关于string的深拷贝
链接为:http://blog.youkuaiyun.com/zoctopusd/article/details/74775141
下面是我的代码部分,重点在于Find函数和Insert函数的两种方法。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class String
{
public:
String(char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
String(const String& s)
:_str(NULL)
, _size(0)
, _capacity(0)
{
String tmp(s._str);
this->Swap(tmp);
}
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()
{
delete[]_str;
_str = NULL;
_size = _capacity = 0;
}
char* GetStr()
{
return _str;
}
size_t Size()
{
return _size;
}
size_t Capacity()
{
return _capacity;
}
// 增删查改
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 (len + _size > _capacity)
{
Expand(len + _size);
}
strcpy(_str + _size, str);
}
void PopBack() //尾删
{
assert(_size);
--_size;
}
void Insert(size_t pos, char ch) //插入字符
{
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) //插入字符串
{
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++;
}
}
void Erase(size_t pos, size_t count) //删除从pos之后的count个字符
{
if (count + pos >= _size + 1)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + count);
_size -= count;
}
}
int Find(char ch) const //寻找字符
{
size_t i = 0;
for (; i <= (_size - 1);++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(substr);
size_t srcIndex = 0;
size_t subIndex = 0;
while (srcIndex<_size)
{
size_t match = srcIndex;
while (srcstr[match]==substr[subIndex])
{
++match;
++subIndex;
while (subIndex==sublen)
{
return srcIndex;
}
}
++srcIndex;
subIndex = 0;
}
return -1;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
bool operator<(const String& s) const
{
size_t i = 0;
while ((i != _size) && (i != s._size))
{
if (_str[i]<s._str[i])
{
return true;
}
else if (_str[i] == s._str[i])
{
++i;
}
else
{
return false;
}
}
return false;
}
bool operator<=(const String& s) const
{
return *this<s || *this == s;
}
bool operator>(const String& s) const
{
return !(*this <= s);
}
bool operator>=(const String& s) const
{
return !(*this < s);
}
bool operator==(const String& s) const
{
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)const
{
return !(*this == s);
}
void Expand(size_t n) //开辟空间
{
if (n > _capacity)
{
_str = (char *)realloc(_str, n + 1);
assert(_str);
_capacity = n;
}
}
private:
char* _str;
size_t _size; // 字符个数
size_t _capacity; // 容量空间
};
void TestString()
{
String s1("hello world!");
String s2;
String s3(s1);
s2 = s1;
cout << s1.GetStr() << endl;
cout << s3.GetStr() << endl;
cout << s2.GetStr() << endl;
}
void test2()
{
String s1("hello");
String s2("world");
cout << s1.GetStr() << endl;
cout << s2.GetStr() << endl;
s1.Insert(3, 'a');
cout << s1.GetStr() << endl;
s1.PushBack('b');
cout << s1.GetStr() << endl;
s1.PopBack();
s1.PopBack();
cout << s1.GetStr() << endl;
s1.PushBack("world");
cout << s1.GetStr() << endl;
}
void test3()
{
String s1("wollo");
String s2("world");
cout << s1.operator<(s2) << endl;
}
void test4()
{
String s1("abcdaaabb");
cout << s1.Find('d') << endl;
cout << s1.Find("aabb") << endl;
s1.Insert(3, ' ');
cout << s1.GetStr() << endl;
s1.Insert(4, "hhh");
cout << s1.GetStr() << endl;
s1.Erase(4, 4);
cout << s1.GetStr() << endl;
}
void test5()
{
String s1("hello");
String s2("hello");
cout << (s1 < s2) << endl;
cout << (s1 <= s2) << endl;
cout << (s1 > s2) << endl;
cout << (s1 >= s2) << endl;
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
}
int main()
{
test4(); //测试用例
system("pause");
return 0;
}