C++字符串处理操作符重载
&& || 不能进行运算符重载,因为运算符重载不能进行截断操作
截断操作就是当 a || b,a为真的时候就不会再判断b了,但是运算符重载不能达到这效果。
类定义
// 类定义#include <iostream>
using namespace std;//c中没有字符串 字符串类(c风格的字符串)
//空串 ""
class MyString
{friend ostream& operator<<(ostream &out, MyString &s);friend istream& operator>>(istream &in, MyString &s);
public:MyString(int len = 0);MyString(const char *p);MyString(const MyString& s);~MyString();public: //重载=号操作符MyString& operator=(const char *p);MyString& operator=(const MyString &s);char& operator[] (int index);public: //重载 == !== bool operator==(const char *p) const;bool operator==(const MyString& s) const;bool operator!=(const char *p) const;bool operator!=(const MyString& s) const;public:int operator<(const char *p);int operator>(const char *p);int operator<(const MyString& s);int operator>(const MyString& s);//把类的指针 露出来
public:char *c_str(){return m_p;}const char *c_str2(){return m_p;}int length(){return m_len;}
private:int m_len;char *m_p;};
<< 和 >>
操作符重载
ostream& operator<<(ostream &out, MyString &s)
{out<<s.m_p;return out;
}istream& operator>>(istream &in, MyString &s)
{cin>>s.m_p;return in;
}
copy
构造函数
//拷贝构造函数
//MyString s3 = s2;MyString::MyString(const MyString& s)
{this->m_len = s.m_len;this->m_p = new char[m_len +1];strcpy(this->m_p, s.m_p);
}
=
运算符重载
s4 = "s2222";
MyString& MyString::operator=(const char *p)
{//1 旧内存释放掉if (m_p != NULL){delete [] m_p;m_len = 0;}//2 根据p分配内存if (p == NULL){m_len = 0;m_p = new char[m_len + 1];strcpy(m_p, "");}else{m_len = strlen(p);m_p = new char[m_len + 1];strcpy(m_p, p);}return *this;
}// s4 = s2;
MyString& MyString::operator=(const MyString &s)
{//1 旧内存释放掉if (m_p != NULL){delete [] m_p;m_len = 0;}//2 根据s分配内存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];
}
== 和 !=
运算符重载
//if (s2 == "s222222")
bool MyString::operator==(const char *p) const
{if (p == NULL){if (m_len == 0){return true;}else{return false;}}else{if (m_len == strlen(p)){return !strcmp(m_p, p);}else{return false;}}
}bool MyString::operator!=(const char *p) const
{return !(*this == p);
}bool MyString::operator==(const MyString& s) const
{if (m_len != s.m_len){return false;}return !strcmp(m_p, s.m_p);
}bool MyString::operator!=(const MyString& s) const
{return !(*this == s);
}
> 和<
操作符重载
//if (s3 < "bbbb")
int MyString::operator<(const char *p)
{return strcmp(this->m_p , p);
}int MyString::operator>(const char *p)
{return strcmp(p, this->m_p);
}int MyString::operator<(const MyString& s)
{return strcmp(this->m_p , s.m_p);
}int MyString::operator>(const MyString& s)
{return strcmp(s.m_p, m_p);
}
C++字符串处理操作符重载相关推荐
- 操作符重载——C/C++学习笔记
此篇文章来自于网上,作为自己学习中的笔记,若有侵权行为,请告之,24小时之内必删除!下面就转入正题吧! 一.什么是操作符重载? 一看到重载,很容易就让人联想到成员函数重载,函数重载可以使名称相同的函数 ...
- C++——构造函数(拷贝构造,拷贝复制),析构函数,操作符重载
C++--构造函数(拷贝构造,拷贝复制),析构函数,操作符重载 构造函数与析构函数:: 涉及构造函数还可以看这篇文章C++搞懂深拷贝初始化=与赋值=的区别 1.声明和定义构造函数和析构函数 构造函数在 ...
- 泛型与操作符重载杂谈
泛型基础 1.为什么要用泛型类,我们先来看一段代码: 以交换两个数为例,可能需要定义多个重载方法 public static void swap(ref int x,ref int y) { int ...
- Python3 操作符重载方法
操作符重载方法: 类(class)通过使用特殊名称的方法(len(self))来实现被特殊语法(len())的调用 类(class)通过使用特殊名称的方法(len(self))来实现被特殊语法(len ...
- 拷贝构造,操作符重载
拷贝构造 #include <iostream> #include <string.h> using namespace std; class mystring { pu ...
- C#锐利体验-第八讲 索引器与操作符重载(转)
第八讲 索引器与操作符重载 南京邮电学院 李建忠(cornyfield@263.net) 索引 C#锐利体验 "Hello,World!"程序 C#语言基础介绍 Microsoft ...
- C++基础12-类和对象之操作符重载-string练习
总结: 1.等号操作符重载注意事项: (1)防止自身赋值 (2)先将自身的额外开辟的空间回收掉 (3)执行深拷贝 2.注意函数的返回引用或者元素:如果需要连续使用 返回元素 3 ...
- 类型转换和操作符重载 (c#)
http://www.cnblogs.com/chenxizhang/archive/2008/09/14/1290735.html 也许你从来没有考虑过类型转换和操作符重载的问题,毕竟在很多时候,我 ...
- C++赋值操作符重载
1.赋值操作符重载的原因 赋值操作符是一个使用频率最高的操作之一,通常情况下它的意义十分明确,就是将两个同类型的变量的值从一端(右端)传到另一端(左端).但在以下两种情况下,需要对赋值操作符进行重载. ...
最新文章
- 迪拜宣布亿航“无人机的士”计划,将于7月份正式运营
- Linux套接字与虚拟文件系统
- 嵌入式培训学习历程第三天
- max分发-(网络系统克隆)
- KMP算法的来龙去脉
- [系统集成] RT(Request Tracker)执行自定义脚本及发送微信、短信的实现方法
- redhat下安装mysql 5.6.20,解压zip包,查看已经安装过的mysql,卸载rpm安装包,安装mysql服务器端和客户端,修改mysql用户名,登陆mysql,启动关闭mysql
- AngularJS $watch 性能杀手
- 数学与计算机教学设计,数学和信息技术整合的思考 教案教学设计
- 能不能翻译PHP网站源码,有朋友可以帮忙用PHP翻译一段PYTHON代码吗?
- java 字符串 移位_算法学习之字符串左移和右移
- 幕享 for Mac(全平台全设备投屏共享软件)中文版
- 星云的Linux专用学习手册
- 网络数据传输过程分析
- 基于单片机的电集中抄表设计
- ubuntu18.04安装monaco字体
- 修改input提示文字样式
- css3 箭头动态效果,CSS3制作上下跳动动画箭头效果
- Z600服务器是阵列硬盘,Z600 使用SAS卡,SAS硬盘组Raid 5
- Android用shape画个虚线
热门文章
- nyoj-Color the necklace(Ploya定理 + 欧拉函数 + 扩展欧几里得(求逆元))
- Pytho正则表达式-match
- 领域驱动设计,让程序员心中有码(七)
- asp.net mvc中用 log4net记录日志到数据库中
- 引号不是字符串中唯一的可以被转义字符。下面是常见的转义序列列表:
- MVC中Spring.net 对基类控制器无效 过滤器控制器无效
- Java中执行存储过程和函数(web基础学习笔记十四)
- Executor 执行器接口笔记
- [云炬创业基础笔记] 第三章测试1
- 科大星云诗社动态20210211