模仿實現C++庫函數----String 類----用 寫時拷貝 實現
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#define DEFAULTNUME 3
using namespace std;
class String
{
public:
int& GetCount()
{
return *(((int *)_str) - 1);
}
char* GetStr()
{
return _str;
}
//找到字符 ch 並返回它的下標
int Find(char ch)
{
if (_str == NULL)
{
return -1;
}
else
{
int count = 0;
char* str = _str;
while (str && count < _size)
{
if (*str == ch)
{
return count;
}
str++;
count++;
}
}
return -1;
}
//實現下標訪問
char& operator[](int pos)
{
char* cur = _str;
cur = cur + pos ;
return *cur;
}
bool operator<(const String& s)
{
char* pSourse = this->_str;
char* pDest = s._str;
if (pSourse == NULL)
{
return false;
}
else if (pDest == NULL)
{
return true;
}
else
{
while ((*pSourse != NULL) && (pDest != NULL))
{
if (*pSourse < *pDest)
{
return true;
}
else if (*pSourse > *pDest)
{
return false;
}
else
{
pSourse++;
pDest++;
}
}
}
if (*pSourse == NULL && *pDest != NULL)
{
return true;
}
else
{
return false;
}
}
bool operator==(const String& s)
{
char* pSource = _str;
char* pDest = s._str;
if (_size == s._size)
{
while (*pSource == *pDest && *pSource != NULL && *pDest != NULL)
{
pSource++;
pDest++;
}
if (*pSource == NULL && *pDest == NULL)
{
return true;
}
}
return false;
}
bool operator>(const String& s)
{
return !((*this < s) && (*this == s));
}
bool operator>=(const String& s)
{
return ((*this > s) && (*this == s));
}
bool operator<=(const String& s)
{
return ((*this < s) && (*this == s));
}
bool operator!=(const String& s)
{
return !(*this == s);
}
char* CheckCapacity(int capacity)
{
if (capacity > _capacity)
{
char* cur = new char[capacity + DEFAULTNUME];
return cur;
}
return _str;//>>>>>>>>>>>>>>>>>>>改
}
void Insert(int pos, const char* s)
{
*(((int *)_str) - 1) -= 1;
int len = strlen(s);
char* p = CheckCapacity(_size + len + 1);
strcpy(p, _str);
_str = p;
p = NULL;
//移動原字符串 空出來空間給待插入的字符串
int moveCount = _size - pos + len + 1;// moveCount 為移動次數
int i = _size;// i 為移動的元素位置
while (moveCount--)//把pos位置及pos後面的 字符 集體向後移一位
{
_str[i + len] = _str[i];
i--;
}
//插入待插入的字符串
//int insertCount = len; // insertCount 為字符插入次數
//int posSource = pos;// posSource 為原字符串的下標
//int posDest = 0;// posDest 為待插入的字符串s的下標
//while (insertCount--)
//{
// _str[posSource - 1] = s[posDest];
// posSource++;
// posDest++;
//} //>>>用strncpy就可以實現(多注意常用的庫函數啊!)
char* tem = _str + pos - 1;
strncpy(tem, s,len);
_size += len;
_capacity = _size + 4; //4 為 '\0' + DEFAULTNUME 的個數
}
void Insert(size_t pos, const char ch)
{
*(((int *)_str) - 1) -= 1;
char* p = new char[_capacity + 1];
strcpy(p, _str);
_str = p;
p = NULL;
int count = _size - pos + 2;// count 為移動次數
int i = _size;//移動的元素位置
while (count--)//把pos位置及pos後面的 字符 集體向後移一位
{
_str[i + 1] = _str[i];
i--;
}
_str[pos - 1] = ch;
_size++;
_capacity = _size + 4; //4 為 '\0' + DEFAULTNUME 的個數
}
String& operator+=(const String& s)
{
*(((int *)_str) - 1) -= 1;
char* cur = CheckCapacity(_size + s._size + 5);
cur = cur + 4;
strcpy(cur, _str);//拷貝原字符串
_str = cur;
Insert(_size + 1, s._str);//拷貝s._str字符串
*((int*)cur - 1) = 1;
cur = NULL;
_size += s._size;
_capacity = _size + 4; //4 為 '\0' + DEFAULTNUME 的個數
return *this;
}
public:
String(char* str = "")
:_str(new char[_size + 5])
{
_str = _str + 4;
strcpy(_str, str);
_size = strlen(str);
_capacity = _size + 1;
//GetCount() = 1;// 為什麼 錯了?
*(((int *)_str) - 1) += 1;//用++為什麼錯了
}
String(const String& s)
{
_str = s._str;
_size = s._size;
_capacity = _size + 1;
int count = GetCount();
GetCount()++;
}
~String()
{
if (--GetCount() == 0 && _str != NULL)
{
delete[] _str;
_str = NULL;//>>>>>>>>>>>>>>>>>改
}
}
String& operator=(const String& s)
{
if (this != &s)
{
_str = s._str;
_size = s._size;
_capacity = _size + 1;
int count = GetCount();
//GetCount()++;
*(((int *)_str) - 1) += 1;
}
return *this;
}
private:
void Check(int a);//實現引用計數
char* _str;
int _size;
int _capacity;
};
//test 構造函數 拷貝構造函數 析構函數 賦值操作符重載 引用計數
void test1()
{
String s1("abcd");
cout << s1.GetStr() << endl;
String s2 = s1;
cout << s2.GetStr() << endl;
String s3;
cout << s3.GetStr() << endl;
s3 = s1;
cout << s3.GetStr() << endl;
}
//test Find operator[](只讀操作)
void test2()
{
String s1("abcdefg");
cout << s1.Find('b') << endl;
cout << s1.Find('g') << endl;
cout << s1.Find('h') << endl;
cout << s1[2] << endl;
cout << s1[6] << endl;
cout << s1[9] << endl;
}
//test operator< operator== operator>
//test operator<= operator>= operator!=
void test3()
{
String s1("abcd");
String s2("abc");
String s3("abcd");
String s4("acd");
cout << (s2 < s1) << endl;
cout << (s1 < s2) << endl;
cout << (s1 < s3) << endl;
cout << (s1 < s4) << endl;
cout << (s1 == s3) << endl;
cout << (s1 == s2) << endl;
}
//test operator+= Insert
void test4()
{
String s1("tan");
String s2("xiaohui is superman");
cout << (s1 += s2).GetStr() << endl;
//s1.Insert(2, "ooooo");
//cout << s1.GetStr() << endl;
s1.Insert(4, '-');
cout << s1.GetStr() << endl;
}
int main()
{
//test1();
//test2();
//test3();
test4();
system("pause");
return 0;
}