Mystring部分 多種實現方法。

2021-08-08 17:27:04 字數 1919 閱讀 4842

這也是部分。

#include "stdafx.h"

#include //建構函式(預設)

mystring::mystring(const char* str = null)

else

}//拷貝建構函式

mystring::mystring(const mystring & another)

//運算子過載 =(賦值)

mystring & mystring::operator = (const mystring & another)

//先刪除自己開闢的空間

mystring::~mystring();

//獲取another字元長度

int len = strlen(another._str);

//開闢記憶體空間+1 '\0'

this->_str = new char[len + 1];

//將another資料賦值給開闢的空間裡面

strcpy_s(this->_str, len + 1, another._str);

//*_str

return *this;

}//運算子過載 +(加法)

mystring mystring::operator + (const mystring & another)

//cout << j (const mystring & another)

else

}//關係運算子過載 <

bool mystring::operator < (const mystring & another)

else

}//關係運算子過載 ==

bool mystring::operator == (const mystring & another)

else

}//輸入流過載 >>

istream & operator >>(istream &in , mystring &another)

//輸出流過載 <<

ostream & operator <<(ostream &out ,const mystring &another)

//申請記憶體過載

void* mystring::operator new(size_t size)

//new過載

void* mystring::operator new(size_t size)

//刪除申請的記憶體

void mystring::operator delete(void* p)

//delete

void mystring::operator delete(void* p)

//查詢字元,在字串第幾個

//類,查詢的字元

/*char mystring::find(const mystring & p, char data)

//else

int leng = strlen(this->_str);

for (int i = 0;i < leng;i++)

} return data;

}替換第幾個字元;

//pos開始的num個字元,字元

char mystring::replace(int pos, int num,char data)

for (int i = 0;i <= leng;i++)

} cout << this->_str << endl;

}//運算子過載

char& mystring::operator(int idx)

//列印輸出

void mystring::format()

cout << endl;

}//析構函式__釋放物件開闢的資源空間

mystring::~mystring()

mystring類的實現

ifndef cmystring h define cmystring h include include class cmystring data new char strlen s 1 strcpy data,s copy ctor cmystring const cmystring other...

C 之實現MyString類

1.mystring.h pragma once include using namespace std class mystring 2.mystring.cpp include mystring.h 建構函式 mystring mystring const char str else 拷貝建構函...

string類的實現(mystring)

最近參加了實習招聘感覺自己的c 都快忘光了,其中面試過程中問到了string類的實現,需手寫實現一遍。現在再實現一下 class mystring inline mystring mystring char str null else inline mystring mystring inline ...