C++:String的寫時拷貝

String的寫時拷貝

//test.h
#pragma once#include <iostream>
#include <string.h>
#include <cstdio>
#include <assert.h>
using namespace std;#define TESTHEADER printf("\n================%s=============\n", __FUNCTION__)
class String
{
public:String(const char* str);String(const String& s);String& operator = (const String s);~String();char* c_str();void CopyOnWrite();char& operator [] (size_t pos);const char& operator [] (size_t pos)const;String& Insert(size_t pos, char ch);String& Insert(size_t pos, const char* str);String& Append(const char* str);String& Erase(size_t pos, size_t n);void Expand(size_t n);
private:char* _str;int* _pcount;size_t _size;size_t _capacity;
};//test.cc
#include "test.h"
String::String(const char* str):_size(strlen(str))
,_capacity(_size)
{_str = new char [strlen(str) + 1];_pcount = new int(1);strcpy(_str, str);
}//s(s1)
String::String(const String& s)
{_str = s._str;_pcount = s._pcount;++(*_pcount);_size = s._size;_capacity = s._capacity;
}//s1 = s2
String& String:: operator = (const String s) 
{if(_str != s._str){_str = s._str;--(*_pcount);if(*_pcount == 0){delete [] _str;delete _pcount;}_size = s._size;_capacity = s._capacity;}return *this;
}String::~String()
{delete [] _str;delete _pcount;_size = 0;_capacity = 0;
}
char* String::c_str()
{return _str;
}void String::CopyOnWrite()
{if(*_pcount > 1){char* tmp = new char[_size + 1];strcpy(tmp, _str);--(*_pcount);_str = tmp;_pcount = new int(1);}
}//s[1]
char& String::operator [] (size_t pos)
{CopyOnWrite();return _str[pos];
}const char& String:: operator [] (size_t pos)const
{return _str[pos];
}void String::Expand(size_t n)
{if( *_pcount  == 1){char* tmp = new char [n];strcpy(tmp, _str);delete [] _str;--(*_pcount);delete _pcount;_pcount = new int(1);_str = tmp;}else{char* tmp = new char [n];strcpy(tmp, _str);_str = tmp;--(*_pcount);_pcount = new int(1);}
}String& String::Insert(size_t pos, char ch)
{assert(pos < _size);if(_size == _capacity){Expand(2 * _capacity);}int end = _size;for(; end >= (int)pos; end--){_str[end] = _str[end - 1];}_str[pos] = ch;return *this;
}String& String::Insert(size_t pos, const char* str)
{assert(pos <= _size);int len = strlen(str);if(_size == _capacity){Expand(_size + len);}int end = _size;for(; end >= (int)pos; end--){_str[end + len] = _str[end];}strncpy(_str + pos, str, len);_size += len;/* _str[_size] = '\0'; */return *this;
}String& String::Append(const char* str)
{int len_ = strlen(_str);int len = strlen(str);if(_size == _capacity){Expand(_size + len);}strcpy(_str + len_, str);_size += len;return *this;
}String& String::Erase(size_t pos, size_t n)
{assert(pos <= _size);CopyOnWrite();if(pos + _size > _capacity){_str[pos] = '\0';}strcpy(_str + pos, _str + pos + n);_size -= n;return *this;
}

如果有什么錯誤,還望大家提出來,共同進步

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383903.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383903.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383903.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

兩個棧實現一個隊列與兩個隊列實現一個棧

http://blog.csdn.net/z84616995z/article/details/19204529 兩個棧實現一個隊列&#xff1a; 原理方法&#xff1a;用一個棧為主棧&#xff0c;一個棧為輔助棧存放臨時元素。 入隊&#xff1a;將元素依次壓入主棧 出隊&#xff1a;先檢測輔助棧是否為空&#xff0c;如果非空&a…

UVa11426——歐拉函數

發現對于gcd問題要多和歐拉函數聯系在一起&#xff0c;雖然有時候并不是互質&#xff0c;但是我們知道有多少互質的然后根據互質的數目就能解決很多個gcd的問題 對于這道題目&#xff0c;題目要求的是所有數對的gcd的和&#xff0c;直接思考的話有難度。但是我們如果聯想到歐拉…

C++:繼承和多態

虛函數:只有類的成員函數才能定義為虛函數 虛函數 在類的成員函數前面加上一個 virtual 關鍵字, 此時這個成員函數就叫做虛函數 虛函數 當在子類中定義了一個與父類完全相同的虛函數的時候,此時就叫做子類的虛函數重寫了父類的虛函數 構成多態的條件 派生類重寫基類的虛函數…

POJ 1061擴展歐幾里得

擴展歐幾里得的模板題&#xff0c;需要注意的是為了得到一個最小正數解我們要使axbyc中的a,b都是正數 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #include<ctim…

C++::探索對象模型

前面我們已經知道, 在沒有虛函數的時候, 對象的大小就是對應的成員變量的大小, 而成員函數不會占用對象的空間, 今天我們來討論一下, 當類中定義了虛函數的時候, 此時對象的大小以及對象模型 非繼承下的對象模型 class Base { public:virtual void func1(){cout << &qu…

auto_ptr

#include <iostream> #include <memory> using namespace std;class A { public:A(){cout<<"構造"<<endl;}~A(){cout<<"A析構"<<endl;}void fun(){cout<<"A::fun"<<endl;} };class PA { public…

POJ 2142——擴展歐幾里得

題目是很裸的擴展歐幾里得&#xff0c;但是對x,y有限制條件&#xff0c;要求所有x,y中abs(x)abs(y)最小&#xff0c;在這個條件下要求abs(a* x)abs(b* y)最小 顯然我們需要用擴展歐幾里得求得一組解&#xff0c;問題在于如何處理這組解以得到符合條件的值。 我是這樣處理的&a…

C++::模板

模板的簡單介紹 C中模板是為了能夠使得函數或者類實現范型編程的目的, 同時C模板的出現是為了避免代碼的冗余 舉個例子 void Swap(int& a, int& b) {int tmp a;b a;a b; } void Swap(char& a, char& b) {char tmp a;b a;a b; } 上面的函數除了類型不…

Linux select TCP并發服務器與客戶端編程

http://blog.csdn.net/szkbsgy/article/details/10558881 [cpp] view plaincopy <span style"font-size:18px;">服務端&#xff1a; #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> #i…

BZOJ - 2186 歐拉函數

題目的意思大概是求1~N!中和M&#xff01;互質的數的個數 因為對歐拉函數理解不夠深刻所以我是分析得到結果的&#xff1a; 當N<M的時候顯然符合要求的數的個數為0&#xff1b; 當N>M的時候我們要求的是1~N!中不含1 ~M的素因子的的數的個數&#xff0c;結合歐拉函數的…

多態相關概念

多態相關注意事項 所謂的多態就是指函數有多中狀態, 在C中通常是通過父類指針指向子類對象的方法實現多態, 這樣父類可以通過子類的類型調用不同的方法. 即實現一個接口多種方法, 多態的引用是為了實現接口復用 在 C中多態是通過虛函數來實現的. 子類通過對父類相關接口進行重…

模板實現棧隊列以及鏈表

模板實現鏈表 //test.h #include <iostream> #include <cstdio> #include <assert.h> using namespace std;template <class T> struct ListNode {ListNode* _prev;ListNode* _next;T _data;ListNode(const T& x):_prev(NULL),_next(NULL),_data(…

基于Visual C++2013拆解世界五百強面試題--題5-自己實現strstr

http://blog.csdn.net/itcastcpp/article/details/12907371 請用C語言實現字符串的查找函數strstr&#xff0c; 找到則返回子字符串的地址&#xff0c;沒有找到返回為空&#xff0c;請用數組操作與指針操作實現 看到題目想到最簡單的方法就是母字符串和子字符串比較&#xff0c…

卡特蘭數

卡特蘭數的引入與n邊形分成三角形的個數有關&#xff1a; 我們令f[n]表示n邊形可以分成的三角形的個數&#xff0c;特殊的&#xff0c;令f[2]1 我們考慮以頂點1頂點的一個三角形&#xff0c;假設用的是n邊形的k-k1邊&#xff0c;那么這種情況的方案數就是f[k]?f[n?k1]f[k]*…

軟件測試相關概念

什么叫軟件測試 軟件測試就是測試產品沒有錯誤,同時又證明軟件是可以正確運行的 測試和調試的區別 調試一般都在開發期間 ,測試是伴隨著整個軟件的生命周期, 調試是發現程序中問題并且解決問題, 測試是發現程序中的缺陷 軟件測試的目的和原則 目的:驗證軟件有沒有問題 原…

Linux 線程信號量同步

https://www.cnblogs.com/jiqingwu/p/linux_semaphore_example.html 信號量和互斥鎖(mutex)的區別&#xff1a;互斥鎖只允許一個線程進入臨界區&#xff0c;而信號量允許多個線程同時進入臨界區。 不多做解釋&#xff0c;要使用信號量同步&#xff0c;需要包含頭文件semaphore.…

本原勾股數組

勾股數我們都很熟悉&#xff0c;a2b2c2&#xff0c;可是如何快速找到所有的勾股數組呢&#xff1f; 本原勾股數組a2b2c2性質&#xff1a; 1. a,b奇偶不同&#xff0c;c一定是奇數 2. 若b為偶數&#xff0c;c-b和cb一定是完全平方數 3. 設t>s>1,且均為奇數&#xff0c;則…

C++靜態成員函數訪問非靜態成員的幾種方法

https://www.cnblogs.com/rickyk/p/4238380.html 大家都知道C中類的成員函數默認都提供了this指針&#xff0c;在非靜態成員函數中當你調用函數的時候&#xff0c;編譯器都會“自動”幫你把這個this指針加到函數形參里去。當然在C靈活性下面&#xff0c;類還具備了靜態成員和靜…

費馬大定理

當n>3時方程 xnynzn沒有正整數解 結論很簡潔&#xff0c;剛才看了一下證明的歷史&#xff0c;我勒個去。。。。

類中的靜態成員函數訪問非靜態成員變量

http://blog.csdn.net/u011857683/article/details/52294353 1.思路&#xff1a; 靜態成員函數屬于類(通過類訪問&#xff0c;調用函數時沒有提供this指針)&#xff0c; 非靜態成員函數屬于實例&#xff08;通過對象訪問&#xff09;&#xff08;默認都提供了this指針&#xf…