1. 程式人生 > >自定義實現字符串string的接口

自定義實現字符串string的接口

初始 定義類 per code enter public 自定義 truct this

用char*管理String類的內存,new動態分配,在析構函數中delete char*指向的new出來的內存,一個string類需要實現那些接口可參考標準庫裏的string: http://www.cplusplus.com/reference/string/string/

  • 實現思路是:在創建String、String需要伸縮(如賦值時需要調整大小、字符串相加也需要更大的空間)時重新new內存,並釋放掉原有內存;標準庫string的空間是預先分配的,有多余的空間可用,如果string需要伸縮,就開辟新的內存空間。
  • >>重載的思路是:用cin.getline來讀取標準輸入到字符指針指向的空間——空間有個預設的大小,如100,可以接收包含有空格的字符串
  • String初始化為空字符串("\0"或 ""是等價的),也需要new字符數組,這樣才能delete,如果用char *data="\0",不能用delete釋放不是new出來的內存
  • []需要重載兩個版本,const版本 const char& operator[](size_t index) const; const成員函數的this指針隱式的轉換為指向常量的指針: operator(const String *this, size_t index),const對象只能調用const menber function
  • 不需要重載解引用*和取地址&,它們對自定義的類型也支持
  • +=調用了operator+,問為什麽operator+要返回引用,而不能返回對象?
  • 沒有實現重載的relational operators
  • 相比標準庫,自定義的String實現的接口很少,似乎什麽都做不了,只能創建,賦值,輸出,訪問元素,甚至連標準庫裏的string開發中也不好用:為什麽大多數的C++的開源庫都喜歡自己實現一個string?
  • new管理內存是否存在memory leak,是否有隱含的錯誤?
技術分享
  1 #include<iostream>
  2 #include<string>
  3 #include<vector>
  4 #include<algorithm>
  5
#include<cstdio> 6 #include<complex> 7 #include<new> 8 #include<memory> 9 #include<exception> 10 using namespace std; 11 // interfaces should supported 12 //用char*管理內存 13 class String 14 { 15 public: 16 String(); 17 String(const char*s); 18 String(size_t n, const char c);//用字符c填充String 19 String(const String&s); 20 String& operator=(const String&rhs); 21 String& operator=(const char*c); 22 char& operator[](size_t index);//要提供[]的兩個版本 23 const char& operator[](size_t index)const; 24 String & operator*();//解引用重載是多余的,語言本身支持對所有類型的指針解引用,包括了自定義類型 25 String& operator+(const String&rhs); 26 String& operator+=(const String&rhs); 27 bool operator==(const String&rhs); 28 friend bool operator<(const String&lhs, const String&rhs); 29 friend ostream& operator<<(ostream& os, const String&rhs); 30 friend istream& operator>>(istream& in, String&rhs); 31 size_t size(); //如果是size_t length()會導致。。。。。 32 bool empty(); 33 const char* c_str(); 34 ~String(); 35 private: 36 char * data; 37 size_t length; 38 39 }; 40 String::String() //類外面定義menber function ,域作用符 41 { 42 this->data = new char[1](); //空字符串 ,strlen對data運算得到的長度為0 43 data[0] = \0; 44 (*this).length = 0; 45 } 46 String::String(const char*s) //允許implicit類型轉換 47 { 48 // 不能用data=s;這導致創建的String對象與s共享了字符,而不是副本 49 length = strlen(s); 50 data = new char[length + 1]();//value initialization,申請了字符指針指向的內存區域 51 strcpy(data, s); 52 *(data + length) = \0; 53 } 54 String::String(size_t n, const char c) 55 { 56 length = n; 57 data = new char[length + 1](); 58 for (size_t i = 0; i < length;i++) 59 { 60 *(data + i) = c; 61 } 62 *(data + length) = \0; 63 } 64 String::String(const String&s) 65 { 66 length = s.length; 67 data = new char[length + 1](); //untill deconstructor to destory newed memory 68 for (size_t i = 0; i < length;i++) 69 { 70 data[i] = s.data[i]; 71 } 72 data[length] = \0; 73 } 74 String& String::operator=(const String&rhs) 75 { 76 if (this->data == rhs.data) return *this;// 支持*this==rhs的關系比較操作嗎? 77 78 //assign 不是初始化,不需要申請內存空間,拷貝構造需要 ,但是原String與rhs大小可能不一樣 79 delete[]data; //會導致重復刪除data所指內存嗎?? 80 81 length = rhs.length; 82 data = new char[length + 1](); 83 //如果發生異常 84 85 if (data == NULL) throw "allocate memory failed in copy ctr"; 86 for (size_t i = 0; i < length; i++) 87 { 88 data[i] = rhs.data[i]; 89 } 90 data[length] = \0; 91 } 92 char& String::operator[](size_t index) 93 { 94 if (index >= length||index<0) throw "out of range"; 95 return data[index];//如果data指向的字符串為"dhaj"這種不可修改的,會導致返回的引用不能有s[i]=‘c‘的修改操作 96 } 97 String& String:: operator*() 98 { 99 if (this == NULL) throw "dereferrence null pointer "; 100 return *this; 101 } 102 String& String::operator+(const String&rhs)//a+b形式,也不改變a 103 { 104 String s;//不可伸縮,不能用來存a+b的結果 105 int len = strlen(this->data) + strlen(rhs.data); 106 char *p = new char[len + 1](); 107 for (size_t i = 0; i < strlen(this->data); i++) 108 { 109 p[i] = this->data[i]; 110 } 111 for (int j = strlen(this->data),i=0; j < len; j++,i++) 112 { 113 p[j] = rhs.data[i]; 114 } 115 p[len] = \0; 116 String *t = new String(p); //new了一個p,一個t,可以釋放掉p,因為t是p的副本 117 delete[]p; 118 return *t;//返回了堆上的引用,再c=a+b這種使用後,c是會析構的,但中間這個看不見的對象不會析構 119 } 120 String& String::operator+=(const String&rhs) 121 { 122 *this = *this + rhs; 123 //釋放掉原來的a,申請新空間 124 return *this; 125 } 126 bool String::operator==(const String&rhs)//比較是否相等,而不是相同 127 { 128 if (this->length != rhs.length) return false; 129 int i = 0; 130 for (; (*this).data[i] == rhs.data[i]; i++); 131 if (i != (rhs.length + 1)) return false; 132 else return true; 133 } 134 bool operator<(const String&lhs, const String&rhs) 135 { 136 return true; 137 } 138 ostream& operator<<(ostream& os, const String&rhs) //申明為友元函數。調用方式為cout<<s,申明為men fun,調用形式為s<<cout 139 { 140 os << rhs.data;//輸出null pointer會未定義行為 141 return os; 142 } 143 istream& operator>>(istream& is, String&rhs) 144 {//輸入一個字符串到字符指針,字符指針需要預先分配一定大小的空間,如何根據輸入的字符串調整空間大小 145 delete[]rhs.data; 146 rhs.data = new char[50](); 147 is.getline(rhs.data, 50);//最後位置自動置為‘\0‘,所以最多存49個字符,可接收空格 148 rhs.length = strlen(rhs.data); 149 return is; 150 } 151 size_t String::size() 152 { 153 return length; 154 } 155 bool String::empty() 156 { 157 if (length == 0) return true; 158 else return false; 159 } 160 const char* String::c_str()//返回指向對象局部部分的引用,指針,能用來修改原對象嗎? 161 {//如果為空串,也要返回data 162 return data; 163 164 } 165 String::~String() 166 { // 析構會發生錯誤嗎? 167 //String創建時會new內存,當程序結束或局部String對象退出作用域時調用析構函數,釋放該內存 168 delete[]data; //如果String不是new出來的, 指針p指向它,delete p會導致錯誤,new出來的可以調用delete,這時會調用~String 169 data = NULL; 170 } 171 172 //int main() 173 //{ 174 // String s; 175 // cout << s << endl; 176 // cout << strlen(s.c_str()) << endl; 177 // String s1 = "whah"; 178 // cout << s1 << endl; 179 // String s2 = s1; 180 // cout <<"s2 length:"<< s2.size() << endl; 181 // s = s2; 182 // cout << "用s2賦值後的s: " << s << endl; 183 // String s3(10, ‘c‘); 184 // cout << "字符填充初始化s3: " << s3 << " " << "s3 len= " << s3.size() << endl; 185 // s3[0] = ‘a‘; 186 // cout << "下標修改s3後: " << s3 << endl; 187 // String s4 = s2 + s3+"ad"; //支持連續的+ 188 // cout << s4 << endl; 189 // String *sp = &s4; 190 // cout << *sp << endl; 191 // cout << strlen((*sp).c_str()) << endl; 192 // String *q = new String(*sp); 193 // cout << *q << endl; 194 // delete q; 195 // 196 // char *sq = const_cast<char*> (s4.c_str()); 197 // sq[0] = ‘l‘; 198 // cout << sq << endl; 199 // cout << s4 << endl; 200 // const char *cptr = s2.c_str(); 201 // string s5 = ""; 202 // string s6 = "\0"; 203 // cout << s5 << "**" << s6 << s5.size() << s6.size() << endl; 204 // string str = "jadj"; 205 // char *sptr = const_cast<char *> (str.c_str()); 206 // sptr[0] = ‘d‘; 207 // cout << sptr << " " << str << endl; 208 // String s7(s2); 209 // s7 += s4; 210 // cout << "s7: " << s7.size() << s7 << endl; 211 // const String s8("daj"); 212 // //s8[0]; 對const對象的[]調用,不能用non-const menber版本的operator[],要提供 const char& operator[]() const重載版本 213 // //s8 = "djwajk0"; 214 //} 215 216 int main() 217 { 218 string s2; 219 cin >> s2; 220 cout << s2 << endl; 221 String s, s1; 222 cin >> s >> s1;//用Enter間隔字符串,可接含有空格的字符串 223 cout << s <<"\n"<< s1 << endl; 224 cout << s1.size() << endl; 225 }
String類

自定義實現字符串string的接口