运算符重载实现判断集合异同
来源:互联网 发布:古筝大师软件 编辑:程序博客网 时间:2024/05/21 14:56
/*定义一个集合类 SET,处理整型数组。通过成员函数重载运算符“= =”,判断一个数是否属于集合;通过友元重载运算符“= =”,判断两个集合是否相同,即集合中的所有元素相同,但顺序可不同。具体要求如下:(1)私有数据成员: int *a; //数据成员,存放整型数组,集合为数组中的所有元素 int len; //数据成员,数组的长度(2)公有成员函数: SET(int *p,int n):构造函数,以形参初始化数据成员; int operator = =(int m):重载函数,判断m是否属于当前对象所属的集合; friend int operator = =(SET &s1, SET &s2):重载函数,判断s1和s2所属的集合是否相同,可在该函数中调用类的其他函数; void print():输出集合; ~SET():析构函数,释放动态内存。(3)对所定义的类进行测试。以集合a{1,2,3,4,5}、集合b{1,2,3,4,5}、集合c{1,2,3,4,5,6}、集合d{1,3,5,7,9} 为测试数据,判断a与b、a与c、a与d之间是否相同。*/#include<iostream>using namespace std;class set{private: int *a; int len;public: set(int *p, int n); int operator == (int m); friend int operator == (set &s1, set &s2); void print(); ~set();};set::set(int *p, int n){ a = new int; a = p; len = n;}int set::operator==(int m){ int *pp = a; for (int i = 0; i < len; i++) { if (m == *( pp + i )) return 1; } return 0;}int operator==(set &s1, set &s2){ int *qq = s2.a; for (int i = 0; i < s2.len; i++) { if (s1 == *(qq + i)) continue; else return 0; } return 1;}void set::print() //貌似无用{ int *pp = a; for (int i = 0; i < len; i++) { cout << *(pp + i) << '\t'; } cout << endl;}set::~set(){ delete a;}int main(){ int a[] = { 1,2,3,4,5 }, b[] = { 1,2,3,4,5 }, c[] = { 1,2,3,4,5,6 }, d[] = { 1,3,5,7,9 }; set aa(a, 5), bb(b, 5), cc(c, 6), dd(d, 5); if (aa == bb) cout << "a与b相同!"; if (aa == cc) cout << "a与c相同!"; if (aa == dd) cout << "a与d相同!"; system("pause"); return 0;}
阅读全文
0 0
- 运算符重载实现判断集合异同
- 运算符重载 - 判断平行
- 简单实现运算符重载
- 通过运算符重载实现复数运算
- ”^“运算符重载实现幂指数运算
- 重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算
- 重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 友元函数与类重载运算操作符的异同
- 巨型整数运算符重载实现部分
- 运算符重载(实现CString类)
- 矩阵类的实现,运算符重载
- 实现复数类中的运算符重载
- 实现Time类中的运算符重载
- 用运算符重载实现复数相加
- C++实现的复数运算符重载
- 实现复数类的运算符重载
- 实现负数中的运算符的重载
- base64图片存储
- python 按不同维度求和,最值,均值
- css3属性总结(中)
- Java 构造函数
- Myeclipse2014的激活工具run.bat闪退的完美解决
- 运算符重载实现判断集合异同
- 设计模式-创建型软件设计模式(二)
- UVA1600 Patrol Robot
- 机器学习:逻辑回归
- 利用命令部署springBoot项目出现ERROR] No plugin found for prefix ‘spring-boot’ in the current的错误
- Vagrant在,win7/win10系统下搭建使用
- RocketMQ原理解析-broker 6.索引服务
- Hadoop,Java,MySQL,Linux,ML,DL,Spark大数据跳槽班视频下载
- strlen strcmp strcat strcpy strncpy strstr strchar函数的实现