首页 / C++ / String类(C++练习二)
String类(C++练习二)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了String类(C++练习二),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4692字,纯文字阅读大概需要7分钟。
内容图文
字符串类(String),熟悉内存管理与拷贝控制
- 类定义
#include <iostream> //#include <cstring>using std::cout; using std::cin; class String{ using iterator = char *; friend std::ostream &operator<< (std::ostream &, const String &); friend std::istream &operator>> (std::istream &, String &); friend String operator + (const String &, const String &); friend String operator + (const String &, constchar *); //对于非类内的函数,必须参数中存在类类型的参数; friend String operator + (constchar *, const String &); //否则会与默认类型的操作符冲突 friend unsigned int getline(std::istream &, String &); public: String(constchar *rhs = "hello, world"); //构造函数,默认为"hello, world" 通常我们可以默认为NULL String(const String &); //拷贝构造函数 String &operator= (const String&); //拷贝赋值运算符 String &operator= (constchar *); //拷贝赋值运算符 String & operator += (const String&); String & operator += (constchar *); charoperator[] (int); iterator begin(); iterator end(); String & push_back(constchar); String & clear(); ~String() { delete [] data; } //析构函数 private: char *data; }; std::ostream & operator<< (std::ostream &, const String &); std::istream &operator>> (std::istream &, String &); inline String::String(constchar *rhs) { if (!rhs) //必须先检查rhs是否为NULL;对于NULL的拷贝,会去读取未知地址 data = newchar[1]{0}; //因为我们的析构函数调用了delete,所以必须开辟一个空间, //否则代码在调用析构函数时会delete一个错误空间else { data = newchar[strlen(rhs) + 1]; strcpy(data, rhs); } } inline String& String::operator= (const String& rhs) //拷贝赋值运算{ this->data = newchar[strlen(rhs.data) + 1]; strcpy(this->data, rhs.data); return *this; } inline String & String::operator= (constchar *rhs) //拷贝赋值运算符{ this->data = newchar[strlen(rhs) + 1]; strcpy(this->data, rhs); return *this; }
- 类方法实现
- 先把开始的写好了,后面可以一直重用它,比如 += 和 +;
- 析构函数中需要使用delete [] data,是因为很多操作中都需要开辟空间,所以在默认构造函数中虽然里面不放东西但是还需要开辟一个空间,因为当这个类由于某些原因调用析构函数时,需要有一块空间给delete使用
- 使用智能指针应该也是可以的,这样的话就不需要delete了
#include "String.h" inline String::String(const String &rhs) //拷贝构造函数{ data = newchar[strlen(rhs.data) + 1]; //每次都要 +1 的原因是strlen不包含最后的‘\0‘ strcpy(data, rhs.data); } std::ostream &operator<< (std::ostream &os, const String &rhs) { os << rhs.data; return os; } std::istream& operator >> (std::istream &is, String &rhs) { is >> rhs.data; returnis; } String & String::operator += (const String &rhs) { if (!rhs.data) //如果要加的类的数据为NULL,则不需要处理了return *this; elseif (!data) //如果原来的数据为NULL,则直接把后来的数据拷贝过来;由于上面的判断,所以rhs.data != NULL; { this->data = newchar[strlen(rhs.data) + 1]; strcpy(this->data, rhs.data); return *this; } else//data != NULL && rhs.data != NULL { char *tmp = newchar[strlen(this->data) + 1]; strcpy(tmp, this->data); this->data = newchar[strlen(data) + strlen(rhs.data) + 1]; strcpy(data, tmp); strcat(data, rhs.data); free(tmp); return *this; } } String & String::operator += (constchar * rhs) { if (!rhs) //如果要加的类的数据为NULL,则不需要处理了return *this; elseif (!data) //如果原来的数据为NULL,则直接把后来的数据拷贝过来;由于上面的判断,所以rhs.data != NULL; { this->data = newchar[strlen(rhs) + 1]; strcpy(this->data, rhs); return *this; } else//data != NULL && rhs.data != NULL { char *tmp = newchar[strlen(this->data) + 1]; strcpy(tmp, this->data); this->data = newchar[strlen(data) + strlen(rhs) + 1]; strcpy(data, tmp); strcat(data, rhs); free(tmp); return *this; } } String operator + (const String &lhs, const String &rhs) { String tmp(lhs); tmp += rhs; return tmp; } String operator + (const String &lhs, constchar *rhs) { String tmp(lhs); tmp += rhs; return tmp; } String operator + (constchar *lhs, const String &rhs) { String tmp(lhs); tmp += rhs; return tmp; } String::iterator String::begin() { return data; } String::iterator String::end() { return data + strlen(data); } char String::operator[] (int index) { if (index > strlen(data) - 1 || index < 0) { cout << "index error"; return0; } elsereturn *(data + index); } String & String::push_back(constchar ch) { char *tmp = newchar[strlen(data) + 2]; strcpy(tmp, data); *(tmp + strlen(data)) = ch; //data + strlen(data)是原来的‘\0‘处 *(tmp + strlen(data) + 1) = ‘\0‘; data = newchar[strlen(data) + 2]; strcpy(data, tmp); //free(tmp); delete[] tmp; //delete [] tmp = free(tmp) : 怀疑delete就是把free封装了return *this; } String & String::clear() { delete[] data; data = newchar[]{0}; return *this; } unsigned int getline(std::istream &is, String & rhs) { char tmp; rhs.clear(); while (is.get(tmp)) { if (tmp && tmp != ‘\n‘) rhs.push_back(tmp); elsebreak; } return strlen(rhs.data); }
原文:http://www.cnblogs.com/dylqt/p/4928577.html
内容总结
以上是互联网集市为您收集整理的String类(C++练习二)全部内容,希望文章能够帮你解决String类(C++练习二)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。