c++ String 类的简单实现和写时拷贝
来源:互联网 发布:远程网络教育的优势 编辑:程序博客网 时间:2024/06/05 07:10
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>#include<string>using namespace std;class String {public: friend ostream& operator<<(ostream& os, String& str) ; String() : _str(new char[1]) { *_str = '\0'; } String(const char* str) :_str(new char[strlen(str)+1]) { strcpy (_str,str); } String(const String& str) :_str(new char[strlen(str._str )+1]) { strcpy(_str ,str._str ); } /* String& operator = (const String& str) { if(this != &str) { delete[] _str; _str= new char[strlen(str._str )+1]; strcpy(_str, str._str ); } return *this; }*/ String& operator = ( String str) { swap(_str , str._str ); return *this; } ~String() { if(_str != NULL) { delete[] _str; } } const char *c_str() { return _str; } public: char& operator[](int index) { return _str[index]; } String& operator+=(const String& s) { int sz = strlen(_str) + strlen(s._str )+1; Check( sz); strcat(_str,s._str ); return *this; } String& Insert(int pos, const String& s) //《就这个函数想的我头疼,终于也是做出来了,嘎嘎》 { int sz = strlen(_str)+strlen(s._str)+1; Check(sz); for(int i = strlen(_str) ; i >= pos;i--)//先将 某位置起所需的空间 的后面的元素进行后移所需空间的个数 { _str[i+strlen(s._str )] = _str[i]; } for(int j = pos, k = 0; k < strlen(s._str ),j < pos+strlen(s._str );k++,j++) //从某位置起插入所需的字符串 { _str[j] = s._str [k]; } return *this; } void PushBack(char ch) { int sz = strlen(_str)+2; Check( sz); _str[sz-2] = ch; _str[sz-1] ='\0'; } void Check(int sz) { char* tmp = new char[sz]; strcpy(tmp,_str); delete[] _str; _str = tmp; }private: char* _str;}; ostream& operator<<(ostream& os, String& str) { os << str._str ; return os; } void test() { String str1("hello world !"); String str2(str1); String str3; str3 = str2; String str4("change world!"); str1+=str4; cout<<str1[2]<<endl; cout<<str1<<endl; cout<<str2<<endl; cout<<str3<<endl; cout<<str1<<endl; cout<<str4[2]<<endl; } void test3() { String str1("hello worl"); str1.PushBack ('d'); cout<<str1<<endl; } void test2() { String str1("horld !"); cout<<str1.Insert(1,"ello w")<<endl; }int main(){ test2(); return 0;}写时拷贝(更好的开辟空间的方式是一次性开辟string的空间和_pcount的空间。然后利用指针的偏移进行数据的操作,此处的方法类似,只不过多用了一次new,优点在于方便理解,容易查找出错误)#include<iostream>#include<string>using namespace std;class String {public: friend ostream& operator<<(ostream& os, String& str) ; String() : _str(new char[1]) ,_pcount(new int(1)) { *_str = '\0'; } String(const char* str) :_str(new char[strlen(str)+1]) ,_pcount(new int(1)) { strcpy (_str,str); } String(const String& str) :_str(str._str ) ,_pcount(new int(1)) { delete _pcount; _pcount = str._pcount ; (*_pcount)++; } String& operator = ( String& str) { if(this != &str) { delete _pcount; delete[] _str; _str= str._str ; _pcount = str._pcount ; (*_pcount)++; } return *this; } ~String() { if(--(*_pcount) == 0 ) { delete[] _str; delete _pcount; } cout<<" ~String()"<<endl; }private: char* _str; int* _pcount;}; ostream& operator<<(ostream& os, String& str) { os << str._str ; return os; } void test() { String str1("hello world !"); String str2(str1); String str3; str3 = str2; cout<<str1<<endl; cout<<str2<<endl; cout<<str3<<endl; }int main(){ test(); return 0;}
0 0
- c++ String 类的简单实现和写时拷贝
- 【C++】浅拷贝和深拷贝以及怎样实现一个简单的string类!!!
- 【C++】浅析浅拷贝,深拷贝及写时拷贝(copy_on_write),模拟实现String类。
- string类的简单实现(写时拷贝Copy-on-write)
- 【C++】简单实现String类--深拷贝的普通版本和简洁版本
- 【C语言】String类的写时拷贝
- 写时拷贝的方式实现基本的String类
- 写时拷贝的方式实现C++中string类
- 模拟实现String类---->写时拷贝
- C++String深浅拷贝、写时拷贝
- string类的简单实现(深拷贝,浅拷贝)
- string类的写时拷贝
- String类的写时拷贝
- String类的写时拷贝
- String类的写时拷贝
- C++【拷贝构造】和【拷贝赋值】(实现自定义的string类)
- 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)
- string的写时拷贝
- 自定义控件
- JQ课件代码记录
- Dojo初探之3:dojo的DOM操作、query操作和domConstruct元素位置操作(基于dojo1.11.2版本)
- LintCode_510 Maximal Rectangle
- RMQ返回最小值,索引
- c++ String 类的简单实现和写时拷贝
- 浙大PAT甲级 1035
- 拼搏不息的精神
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Batch Normalization 学习
- Qt的四个常见的图像叠加模式
- 计算机网络-可靠数据传输机制及其用途总结
- UVA 12898 AND OR
- HDU-2830最大矩形问题(三)