String寫時拷貝實現

來源:互聯網
上載者:User

標籤:

標頭檔部分
1
/* 2 著作權資訊:狼 3 檔案名稱:String.h 4 檔案標識: 5 摘 要:對於上版本簡易的String進行最佳化跟進。 6 改進 7 1.(將小塊記憶體問題與大塊分別對待)小記憶體塊每個對象都有,當記憶體需求大於定義大小時利用動態分配 8 2.實現大塊記憶體的寫時拷貝功能,提高效率,最佳化空間利用 9 3.類似new[]實現機制:將動態記憶體塊大小資訊儲存為隱藏“頭”10 11 目前的版本:1.212 修 改 者:狼13 完成日期:2015-12-1214 15 取代版本:1.116 原 作 者:狼17 完成日期:2015-12-1118 */19 #ifndef _STRING_H20 #define _STRING_H21 #include<iostream>22 using namespace ::std;23 24 namespace COW25 {26 class String27 {28 friend void Check_Interface(String &S);29 friend ostream& operator << (ostream&os, String &Str);30 31 public:32 String(const char* str = "");33 String(String& Str);34 String& operator=(const String& Str);35 ~String();36 37 private:38 void decrease();//減少引用計數。39 void increase();//增加引用計數。40 41 private:42 enum{BufSize = 16}; //利用枚舉進行數組初始化43 char *_str; //內容塊,“頭”代替capacity44 char _buff[BufSize]; //小記憶體塊處理。45 };46 ostream& operator << (ostream&os, String &Str);47 }48 #endif

///////////////實現檔案
#include"String.h"#pragma warning (disable:4351 4996)using namespace COW;String::String(const char *Str) :_str(NULL) , _buff(){ //採用動態分配 if (strlen(Str) >= BufSize) { _str = new char[strlen(Str) + 5]; *((int*)_str) = 1; strcpy(_str + 4, Str); } else//棧記憶體儲 { strcpy(_buff, Str); }}String::String(String& Str) :_str(NULL) , _buff(){ // 1.如果是小塊記憶體。淺拷貝 if (Str._str == NULL) { strcpy(_buff,Str._buff); } else { _str = Str._str; this->increase(); }}String& String::operator=(const String& Str){ //都是動態 且指向一處 if (_str == Str._str&&_str != NULL) return *this; //兩個都是小記憶體時 if (Str._str == NULL&&_str==NULL) { strcpy(_buff, Str._buff); } //如果*this 是小記憶體(不存在記憶體變更,不用處理buff),Str 動態。 else if (_str==NULL&&Str._str!=NULL) { _str = Str._str; this->increase(); } //*this 動態,Str小記憶體。減少*this._str計數。。.更改buff else if(_str!=NULL&&Str._str==NULL) { this->decrease(); _str = NULL; strcpy(_buff,Str._buff); } //兩個都是動態分配 但不同 else { this->decrease(); _str = Str._str; this->increase(); } return *this;}void String::increase(){ *(int*)_str += 1;}void String::decrease(){ if (_str != NULL) { //////問題2,,每次decrease之後必須變更_str..為NULL或者指向新塊 if ((*(int*)_str) - 1 != 0) { (*(int*)_str) -= 1; } else { delete[]_str; _str = NULL; } }}String::~String(){ this->decrease(); _str = NULL;}///////問題1...命名空間衝突時ostream& COW::operator << (ostream&os, String &Str){ if (Str._str) { os << (Str._str + 4) << "\t"<< (*(int*)Str._str); } else os << Str._buff; return os;}//////friend 友員函數不能跨越命名空間訪問私人成員????????????//void Check_Interface(COW::String &S)//{// //char COW::String::*ch = S._str;// char *ch = S._str;// cout << endl;//}
/////////////測試模組
#include"String.h"using namespace COW;void Test_COPY_EPU(){ String S1("hellow world!"); String S2("hellow world"); String S3; S3 = S2; String S4; S4 = S1; //少對多,少對少 cout << "小記憶體賦值" << S3 << endl; cout << "小記憶體賦值" << S4 << endl; //多對多,多對少 String S5("change world"); S5 = S1; cout << S5 << endl; S5 = S2; cout << S5 << endl; //多多且等 String S6 = S1; S6 = S1; cout << S6 << endl;}void main(){ Test_COPY_EPU();}

String寫時拷貝實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.