模仿实现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;

}


你可能感兴趣的:(String类,写时拷贝)