c string類的簡單實現

2021-08-09 08:03:49 字數 321 閱讀 9696

本文只是實現string的一些簡單功能,實現建構函式、拷貝建構函式、賦值函式、析構函式這幾個非常重要的部分。因為string裡涉及動態記憶體的管理,這時就要求程式設計師自己實現這些函式進行深複製,即不止複製指標,需要連同記憶體的內容一起複製,析構函式釋放對應的記憶體。

除了以上幾個個必須的函式,這裡還實現了其他一些比較常用的函式 

1.常見=,+,==,<<,>>等運算子的過載,可以加深對string的認識和運算子過載的理解。

2.對字串的常用操作,如新增、刪除、翻轉、查詢。

string類的整體實現如下:

string.h

string.cpp

C string類簡單實現

include includeusing namespace std class mystring ostream operator ostream os,const mystring s mystring mystring length 0 cout default constructtion i...

C String 類的簡單實現

string類裡面主要包含構造,拷貝構造,析構,賦值,比較,字串相加,獲取長度及子串等方法。include include using namespace std class string 建構函式 string string char str null else 拷貝建構函式 string str...

CString的簡單實現

注意事項 1 注意為結尾符 0 申請乙個位元組的空間 2 在拷貝建構函式和賦值函式中,注意使用深拷貝,而不是淺拷貝 3 過載輸入 輸出運算子 include includeusing namespace std class cstring cstring const cstring other cs...