用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,是否有隐含的错误?
 #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<complex>
#include<new>
#include<memory>
#include<exception>
using namespace std;
// interfaces should supported
//用char*管理内存
class String
{
public:
String();
String(const char*s);
String(size_t n, const char c);//用字符c填充String
String(const String&s);
String& operator=(const String&rhs);
String& operator=(const char*c);
char& operator[](size_t index);//要提供[]的两个版本
const char& operator[](size_t index)const;
String & operator*();//解引用重载是多余的,语言本身支持对所有类型的指针解引用,包括了自定义类型
String& operator+(const String&rhs);
String& operator+=(const String&rhs);
bool operator==(const String&rhs);
friend bool operator<(const String&lhs, const String&rhs);
friend ostream& operator<<(ostream& os, const String&rhs);
friend istream& operator>>(istream& in, String&rhs);
size_t size(); //如果是size_t length()会导致。。。。。
bool empty();
const char* c_str();
~String();
private:
char * data;
size_t length; };
String::String() //类外面定义menber function ,域作用符
{
this->data = new char[](); //空字符串 ,strlen对data运算得到的长度为0
data[] = '\0';
(*this).length = ;
}
String::String(const char*s) //允许implicit类型转换
{
// 不能用data=s;这导致创建的String对象与s共享了字符,而不是副本
length = strlen(s);
data = new char[length + ]();//value initialization,申请了字符指针指向的内存区域
strcpy(data, s);
*(data + length) = '\0';
}
String::String(size_t n, const char c)
{
length = n;
data = new char[length + ]();
for (size_t i = ; i < length;i++)
{
*(data + i) = c;
}
*(data + length) = '\0';
}
String::String(const String&s)
{
length = s.length;
data = new char[length + ](); //untill deconstructor to destory newed memory
for (size_t i = ; i < length;i++)
{
data[i] = s.data[i];
}
data[length] = '\0';
}
String& String::operator=(const String&rhs)
{
if (this->data == rhs.data) return *this;// 支持*this==rhs的关系比较操作吗? //assign 不是初始化,不需要申请内存空间,拷贝构造需要 ,但是原String与rhs大小可能不一样
delete[]data; //会导致重复删除data所指内存吗?? length = rhs.length;
data = new char[length + ]();
//如果发生异常 if (data == NULL) throw "allocate memory failed in copy ctr";
for (size_t i = ; i < length; i++)
{
data[i] = rhs.data[i];
}
data[length] = '\0';
}
char& String::operator[](size_t index)
{
if (index >= length||index<) throw "out of range";
return data[index];//如果data指向的字符串为"dhaj"这种不可修改的,会导致返回的引用不能有s[i]='c'的修改操作
}
String& String:: operator*()
{
if (this == NULL) throw "dereferrence null pointer ";
return *this;
}
String& String::operator+(const String&rhs)//a+b形式,也不改变a
{
String s;//不可伸缩,不能用来存a+b的结果
int len = strlen(this->data) + strlen(rhs.data);
char *p = new char[len + ]();
for (size_t i = ; i < strlen(this->data); i++)
{
p[i] = this->data[i];
}
for (int j = strlen(this->data),i=; j < len; j++,i++)
{
p[j] = rhs.data[i];
}
p[len] = '\0';
String *t = new String(p); //new了一个p,一个t,可以释放掉p,因为t是p的副本
delete[]p;
return *t;//返回了堆上的引用,再c=a+b这种使用后,c是会析构的,但中间这个看不见的对象不会析构
}
String& String::operator+=(const String&rhs)
{
*this = *this + rhs;
//释放掉原来的a,申请新空间
return *this;
}
bool String::operator==(const String&rhs)//比较是否相等,而不是相同
{
if (this->length != rhs.length) return false;
int i = ;
for (; (*this).data[i] == rhs.data[i]; i++);
if (i != (rhs.length + )) return false;
else return true;
}
bool operator<(const String&lhs, const String&rhs)
{
return true;
}
ostream& operator<<(ostream& os, const String&rhs) //申明为友元函数。调用方式为cout<<s,申明为men fun,调用形式为s<<cout
{
os << rhs.data;//输出null pointer会未定义行为
return os;
}
istream& operator>>(istream& is, String&rhs)
{//输入一个字符串到字符指针,字符指针需要预先分配一定大小的空间,如何根据输入的字符串调整空间大小
delete[]rhs.data;
rhs.data = new char[]();
is.getline(rhs.data, );//最后位置自动置为'\0',所以最多存49个字符,可接收空格
rhs.length = strlen(rhs.data);
return is;
}
size_t String::size()
{
return length;
}
bool String::empty()
{
if (length == ) return true;
else return false;
}
const char* String::c_str()//返回指向对象局部部分的引用,指针,能用来修改原对象吗?
{//如果为空串,也要返回data
return data; }
String::~String()
{ // 析构会发生错误吗?
//String创建时会new内存,当程序结束或局部String对象退出作用域时调用析构函数,释放该内存
delete[]data; //如果String不是new出来的, 指针p指向它,delete p会导致错误,new出来的可以调用delete,这时会调用~String
data = NULL;
} //int main()
//{
// String s;
// cout << s << endl;
// cout << strlen(s.c_str()) << endl;
// String s1 = "whah";
// cout << s1 << endl;
// String s2 = s1;
// cout <<"s2 length:"<< s2.size() << endl;
// s = s2;
// cout << "用s2赋值后的s: " << s << endl;
// String s3(10, 'c');
// cout << "字符填充初始化s3: " << s3 << " " << "s3 len= " << s3.size() << endl;
// s3[0] = 'a';
// cout << "下标修改s3后: " << s3 << endl;
// String s4 = s2 + s3+"ad"; //支持连续的+
// cout << s4 << endl;
// String *sp = &s4;
// cout << *sp << endl;
// cout << strlen((*sp).c_str()) << endl;
// String *q = new String(*sp);
// cout << *q << endl;
// delete q;
//
// char *sq = const_cast<char*> (s4.c_str());
// sq[0] = 'l';
// cout << sq << endl;
// cout << s4 << endl;
// const char *cptr = s2.c_str();
// string s5 = "";
// string s6 = "\0";
// cout << s5 << "**" << s6 << s5.size() << s6.size() << endl;
// string str = "jadj";
// char *sptr = const_cast<char *> (str.c_str());
// sptr[0] = 'd';
// cout << sptr << " " << str << endl;
// String s7(s2);
// s7 += s4;
// cout << "s7: " << s7.size() << s7 << endl;
// const String s8("daj");
// //s8[0]; 对const对象的[]调用,不能用non-const menber版本的operator[],要提供 const char& operator[]() const重载版本
// //s8 = "djwajk0";
//} int main()
{
string s2;
cin >> s2;
cout << s2 << endl;
String s, s1;
cin >> s >> s1;//用Enter间隔字符串,可接含有空格的字符串
cout << s <<"\n"<< s1 << endl;
cout << s1.size() << endl;
}

String类

最新文章

  1. Asp.Net跨平台:Ubuntu14.0+Mono+Jexus+Asp.Net
  2. 数组内部对象排序(sort)
  3. 又来了,SDE非直连
  4. Java堆内存
  5. sublime怎么实现函数之间的跳转
  6. JFrame??
  7. Yii2.0官方高级模板的目录结构分析
  8. github上传文件
  9. 织梦DedeCMS提示信息框的修改,修改ShowMsg方法函数
  10. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之五 || Swagger的使用 3.3 JWT权限验证【必看】
  11. RPC原理及其调用过程
  12. 深入理解 Java 垃圾回收机制
  13. 通过sqoop将hdfs数据导入MySQL
  14. django 模板层排序 class Meta 添加信息
  15. HDU3342Legal or Not 拓扑排序
  16. spring cloud学习(一) 服务注册
  17. RPC笔记之初探RPC:DIY简单RPC框架
  18. Spring Hibernate JPA 联表查询 复杂查询(转)
  19. genymotion无法连接相机问题
  20. IE浏览器SCRIPT5拒绝访问,谷歌浏览器XMLHttpRequest can&#39;t load file:/......

热门文章

  1. 《JAVA设计模式》之观察者模式(Observer)
  2. Codeforces - 1195D2 - Submarine in the Rybinsk Sea (hard edition) - 组合数学
  3. python学习第二十八天函数局部变量的用法
  4. CSS 实现水平垂直居中
  5. Electron 无边框窗口最大化最小化关闭功能
  6. 265-Keystone II JESD204B 66AK2L06 评估模块 (现行) XEVMK2LX
  7. 工作中常用的linux命令大全
  8. python爬虫:抓取下载视频文件,合并ts文件为完整视频
  9. windows H2database 安装
  10. C#调用谷歌翻译API