C++之实现MyString类
来源:互联网 发布:nba骑士数据 编辑:程序博客网 时间:2024/06/05 16:31
1.MyString.h
#pragma once#include<iostream>using namespace std;class MyString{public: MyString(const char *str = NULL); MyString(const MyString&another); MyString&operator=(const MyString&another); char* c_str(); MyString operator+(const MyString&another); bool operator>(const MyString&another); bool operator<(const MyString&another); bool operator==(const MyString&another); char &operator[](int idx); char at(int idx); friend istream& operator>>(istream& in,MyString & str); friend ostream& operator<<(ostream& out,const MyString & str); ~MyString(void);private: char *_str;};
2.MyString.cpp
#include "MyString.h"//构造函数MyString::MyString(const char *str){ if(str == NULL) { _str = new char[1]; _str[0] = '\0'; } else { int len = strlen(str); _str = new char[len+1]; strcpy(_str,str); }}//拷贝构造函数MyString::MyString (const MyString&another){ int len = strlen(another._str); this->_str = new char[len+1]; strcpy(this->_str,another._str);}//运算符=重载MyString&MyString::operator=(const MyString&another){ if(this == &another) { return *this; } else{ delete []this->_str; int len = strlen(another._str); this->_str = new char[len+1]; strcpy(this->_str,another._str); return *this; }}//获取字符char* MyString::c_str(){ return this->_str;}//运算符+重载MyString MyString::operator + (const MyString& another){ MyString mystr; int len = strlen(this->_str)+strlen(another._str); delete mystr._str; mystr._str = new char[len+1]; memset(mystr._str,0,len+1); strcat(mystr._str,this->_str); strcat(mystr._str,another._str); return mystr;}//运算符>重载bool MyString::operator>(const MyString& another){ if(strcmp(this->_str,another._str)>0) return true; else return false;}//运算符<重载bool MyString::operator<(const MyString& another){ if(strcmp(this->_str,another._str)<0) return true; else return false;}//运算符==重载bool MyString::operator==(const MyString& another){ if(strcmp(this->_str,another._str)==0) return true; else return false;}//下标取值char & MyString::operator[](int idx){ return this->_str[idx];}//函数取值char MyString::at(int idx){ return this->_str[idx];}//输入运算符重载istream& operator>>(istream& in,MyString & str){ delete []str._str; char buf[1024]; scanf("%s",buf); int len = strlen(buf); str._str = new char[len+1]; strcpy(str._str,buf); return in;}//输出运算符重载ostream& operator<<(ostream& out,const MyString & str){ out<<str._str; return out;}//析构函数MyString::~MyString(void){ delete []this->_str;}
3.main.cpp
#include<iostream>#include "MyString.h"using namespace std;int main(){ MyString s1; cin>>s1; cout<<s1<<endl; cout<<s1.c_str()<<endl; MyString s2("hello"); cout<<"s2="<<s2.c_str()<<endl; MyString s3(s2); cout<<s3.c_str()<<endl; MyString s4 = " world"; cout<<"s4="<<s4.c_str()<<endl; MyString s5 = s3 + s4; cout<<s5.c_str()<<endl; if(s2<s4) cout<<"s2<s4"<<endl; else if(s2>s4) cout<<"s2>s4"<<endl; else cout<<"s2==s4"<<endl; system("pause");}
其实MyString类是模范C++的string类写的,string类里面有很多的成员函数,而我只实现了其中的一小部分常见的功能函数,如果有兴趣可以自己试着扩展更多的功能。
0 0
- C++之实现MyString类
- C++之实现MyString类
- MyString类实现
- MyString类的实现
- mystring类的实现
- 实现MyString类(二)—MyString.cpp实现
- 【15】实现一个MyString类
- C++ 自定义实现MyString类
- 实现MyString类(一)—类声明mystring.h
- mystring实现
- MyString实现
- MyString实现
- 【c++】自己编写MyString类实现C++中String类功能
- 重学C++Primer笔记11---类的四个合成和MyString类的实现
- 自定义字符串类MyString的C++实现
- C++ MyString类的简单实现
- C++用字符数组实现MyString类
- string类的实现(mystring)
- zookeeper curator 源码试读
- sql中date数据处理
- iOS开发中的一些缓存机制
- Android 第三方QQ、微信、微博登录分享
- gpio模拟I2C,驱动pcf8574T
- C++之实现MyString类
- Java 阻塞
- Tcl -- info
- 从此不求人:自主研发一套PHP前端开发框架(21)
- NSDate YYYY-MM-dd hh:mm:ss
- Gson源码
- Android Studio 运行出现 Multiple dex files define Landroid/support/annotation/AnimRes;解决方法
- 过大的数组该怎么存储
- IOS常用数据结构