字符串类的实现
来源:互联网 发布:js给a标签的href赋值 编辑:程序博客网 时间:2024/06/06 10:49
写一个字符串类,实现字符串的功能
MyString.h
#ifndef __MYSTRING_H__#define __MYSTRING_H__#include <iostream>class MyString{// 重载 << 操作符friend std::ostream& operator<<(std::ostream &out, MyString &s);// 重载 >> 操作符friend std::istream& operator>>(std::istream& in, MyString &s);public:MyString(); // 无参构造MyString(const char *s); // 有参构造MyString(int len, char data = 0); // 有参构造MyString(const MyString &s); // 拷贝构造~MyString(); // 析构函数// 重载=、[]操作符public:MyString& operator=(const char *s); // 普通字符串赋值MyString& operator=(const MyString &s); // 类对象之间赋值char & operator[](int index);// 重载 + 运算符public:MyString& operator+(const char *str);MyString& operator+(const MyString &s);MyString& operator+=(const char *str);MyString& operator+=(const MyString &s);// 重载 == !=public:bool operator==(const char *str) const;bool operator==(const MyString &s) const;bool operator!=(const char *str) const;bool operator!=(const MyString &s) const;// 重载 < >public:bool operator>(const char *str) const;bool operator>(const MyString &s) const;bool operator<(const char *str) const;bool operator<(const MyString &s) const;public:const char *c_str(){return m_p;}char *c_str2(){return m_p;}int leng(){return m_len;}private:int m_len; // 字符串长度char *m_p; // 字符串数据};#endif // __MYSTRING_H__
MyString.h
#include "MyString.h"MyString::MyString(){m_len = 0;m_p = new char[1];m_p[0] = '\0';}MyString::MyString(const char *s){if(s == NULL){m_len = 0;m_p = new char[1];m_p[0] = '\0';}else {int len = strlen(s);m_len = len;m_p = new char[len+1];strcpy(m_p, s);}}MyString::MyString(int len , char data ) //默认参数只需在声明中写{m_len = len;m_p = new char[len+1];memset(m_p , data, len);m_p[len] = '\0';}MyString::MyString(const MyString &s){if(s == NULL){m_len = 0;m_p = new char[1];m_p[0] = '\0';}else{m_len = s.m_len;m_p = new char[m_len+1];strcpy(m_p, s.m_p);}}MyString::~MyString(){if(m_p != NULL){delete[] m_p;m_p = NULL;}}MyString& MyString::operator=(const char *s){if(s == NULL)return *this;if(m_p != NULL){delete[] m_p;m_p = NULL;}m_len = strlen(s);m_p = new char[m_len+1];strcpy(m_p, s);return *this;}MyString& MyString::operator=(const MyString &s){if(s.m_p == NULL)return *this;if(m_p != NULL){delete[] m_p;m_p = NULL;}m_len = s.m_len;m_p = new char[m_len+1];strcpy(m_p, s.m_p);return *this;}char & MyString::operator[](int index){return m_p[index];}MyString & MyString::operator+(const char *str){if(str == NULL)return *this;int len = strlen(m_p) + strlen(str);m_len = len;char * tmp = m_p;m_p = new char[len+1];strcpy(m_p, tmp);strcat(m_p, str);delete []tmp;return *this;}MyString & MyString::operator+(const MyString &s){return (*this + s.m_p);}MyString & MyString::operator+=(const char *str){return (*this + str);}MyString & MyString::operator+=(const MyString &s){return (*this + s);}bool MyString::operator==(const char *str) const //后面的const表示this指针也是只读变量{if(m_len != strlen(str))return false;for(int i = 0; i<m_len ; i++){if(m_p[i] != str[i])return false;}return true;}bool MyString::operator==(const MyString &s) const{return (*this == s.m_p);} bool MyString::operator!=(const MyString &s) const{return !(*this == s);}bool MyString::operator>(const char *str) const{if(str == NULL)return true;/*int len = strlen(str);int max = (m_len > len ? m_len : len );for(int i = 0; i<max ; i++){if(m_p[i] == str[i])break;if(m_p[i] > srt[i])return true;elsereturn false;}return (m_lem > len ? true : false);*///因为超过长度了就用'\0' 比较,然后返回,所以不会越界int len = strlen(str);int max = (m_len > len ? m_len : len );for(int i = 0; i<max ; i++){if(m_p[i] == str[i])break;if(m_p[i] > str[i])return true;elsereturn false;}return true; //当他们长度相等,并且大小相等时,返回true}bool MyString::operator>(const MyString &s) const{return (*this > s.m_p);}//小于号有一个问题,当两个字符串相等时,会返回falsebool MyString::operator<(const char *str) const{return !(*this > str);}bool MyString::operator<(const MyString &s) const{return !(*this > s.m_p);}std::ostream& operator<<(std::ostream &out, MyString &s){out << s.m_p;return out;}std::istream& operator>>(std::istream& in, MyString &s){in >> s.m_p;return in; }
main.cpp
#include <iostream>#include "MyString.h"using namespace std;int main(){MyString str1 = "hello";MyString str2 ;MyString str3(10,'a');cout << str1 << endl;cout << str2 << endl;cout << str3 << endl;//str2 = str1 + " world";//cout << str2 << endl;//str2 = str1 + str3;//cout << str2 << endl;str2 = str3;cout << str2 << endl;cout << (str2 == str3) << endl;cout << (str1 == "hello") << endl;cout << (str2 != str3) << endl;cout << (str1 > "hello") << endl;cout << (str1 < "hello") << endl;cout << (str1 > str2) << endl;cout << (str1 < str2) << endl;system("pause");return 0;}
阅读全文
0 0
- 字符串类的实现
- 字符串类函数的实现
- c++字符串类String的实现
- C#类实现字符串的公共操作
- C#类实现字符串的公共操作
- 自定义字符串类MyString的C++实现
- c++类实现字符串的基本函数
- 字符串类string的基本实现
- 实现可变长度字符串的类
- 实现自己的字符串类String
- 字符串操作的实现
- 字符串的实现机制
- 实现字符串的反转
- 字符串的包含实现
- 实现字符串的连接
- 字符串函数的实现
- 字符串函数的实现
- 字符串函数的实现
- 关于Redis的相关知识个人总结
- 个人域名绑定git page,步骤详细,不懂联系本人企鹅446690578
- 聊天图形化界面
- Python自动化视频
- 简单栈实现背包问题
- 字符串类的实现
- centos7编译安装内核最新版
- JAVA计算距现在的日期相差年数月数
- ActiveMQ系列—ActiveMQ性能优化(中3)(消费者和生产者性能总结)
- 导出时候文件乱码方法
- c++、类、对象
- 将表单序列化为json对象
- 不高兴的津津(顺序查找)
- Falsy Bouncer|FreeCodeCamp