String类实现
来源:互联网 发布:网络小说家怎么赚钱 编辑:程序博客网 时间:2024/06/03 17:43
class String{// friend ostream& operator<<(ostream&,String&);// friend istream& operator>>(ostream&,String&);public: String(const char *str=NULL); String(const String& other); ~String(); String& operator=(const String& other); String operator+(const String& other)const; bool operator==(const String& other); char &operator[](unsigned int); size_t size(){return strlen(m_data);}private: char *m_data;};inline String::String(const char*str){ if(str==NULL) m_data=NULL; else { m_data=new char[strlen(str)+1]; strcpy(m_data,str); }}inline String::String(const String& other){ if(other.m_data==NULL) m_data=NULL; else{ m_data=new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); }}inline String& String::operator=(const String& other){ if(this!=&other){ delete[] m_data; if(other.m_data==NULL) m_data=NULL; else{ m_data=new char[strlen(other.m_data)+1]; strcpy(m_data,other.m_data); } } return *this;}inline String String::operator+(const String& other)const { String newString; if(other.m_data==NULL) newString=*this; else if(m_data==NULL) newString=other; else{ newString.m_data=new char[strlen(m_data)+strlen(other.m_data)+1]; strcpy(newString.m_data,m_data); strcpy(newString.m_data,other.m_data); } return newString;}inline bool String::operator==(const String& other){ if(strlen(other.m_data) != strlen(m_data)) return false; return strcmp(m_data,other.m_data)?true:false;}inline char& String::operator[](unsigned int index){ if(index<=strlen(m_data)) return m_data[index];}//ostream& operator>>(ostream &os,String &str){// os<<str.m_data;// return os;//}//istream& operator<<(istream& input,String& str){// char tmp[255];// input>>setw(255)>>tmp;// str=tmp;// return input;//}
阅读全文
0 0
- string类c++实现
- c++ string 类 实现
- string类的实现
- String类的实现
- String类的实现
- String类的实现
- String类实现
- 自己实现String类
- String 类的实现
- String类的实现
- string类实现
- C++实现String类
- string类的实现
- String类的实现
- string类实现
- String类的实现
- string类的实现
- c++ string类实现
- hunnu 11463 信封问题 (找规律||搜索)
- Eclipse 接口开发(一)
- MindManager可以为您和您的团队做些什么?
- 20171215.03
- JavaScript的函数支持重载吗?
- String类实现
- 分布式开发--数据库中间件
- 解决Eclipse中跟代码无法查看源码变量信息
- 递归算法2
- StringUtil(java String工具类)
- 字符串反转
- 一篇文章读懂阿里云企业级数据库最佳实践
- 百度地图点聚合自定义marker
- 解决Bootstrap 模态对话框只加载一次请求数据问题