《C++ Primer》14.3.1节练习

练习14.16:

//为strBlob定义==和!=
class strBlob
{friend bool operator==(const strBlob &lhs,const strBlob &rhs);friend bool operator!=(const strBlob &lhs,const strBlob &rhs);
};bool operator==(const strBlob &lhs,const strBlob &rhs)
{return lhs.data==rhs.data;
}bool operator!=(const strBlob &lhs,const strBlob &rhs)
{return !(lhs==rhs);
}//为strBlobPtr定义==和!=
class strBlobPtr
{friend bool operator==(const strBlobPtr &lhs,const strBlobPtr &rhs);friend bool operator!=(const strBlobPtr &lhs,const strBlobPtr &rhs);
};bool operator==(const strBlobPtr &lhs,const strBlobPtr &rhs)
{auto l = lhs.wptr.lock(),r = rhs.wptr.lock();if (l==r)//两个指针都为空,或指向相同的vector且curr指向相同元素时,相等,否则不等return (!r || lhs.curr==rhs.curr);else return false;
}bool operator!=(const strBlobPtr &lhs,const strBlobPtr &rhs)
{return !(lhs==rhs);
}//为strVec定义==和!=
class strVec
{friend bool operator==(const strVec &lhs,const strVec &rhs);friend bool operator!=(const strVec &lhs,const strVec &rhs);
};bool operator==(const strVec &lhs,const strVec &rhs);
{if (lhs.size()!=rhs.size()){return false;}for (auto itr1=lhs.begin(),itr2 = rhs.begin();itr1!=lhs.end() && itr2!=rhs.end();itr1++,itr2++){if (*itr1!=*itr2){return false;}}return true;
}bool operator!=(const strVec &lhs,const strVec &rhs)
{return !(lhs==rhs);
}
//为String定义==和!=
class String
{friend bool operator==(const String &lhs,const String &rhs);friend bool operator!=(const String &lhs,const String &rhs);private:const char *str;
}bool operator==(const String &lhs,const String &rhs)
{return strcmp(lhs.str,rhs.str);
}bool operator!=(const String &lhs,const String &rhs)
{return !(lhs==rhs);
}

练习14.17:

在练习7.40中,我们实现了Date类。因为我们可以比较两个日期是否相等,因此需要实现相等运算符。

class Date
{friend bool operator==(const Date &d1,const Date &d2);friend bool operator!=(const Date &d1,const Date &d2);
};bool operator==(const Date &d1,const Date &d2)
{return d1.year==d2.year && d1.month==d2.month && d1.day==d2.day;
}bool operator!=(const Date &d1,const Date &d2)
{return !(d1==d2);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310960.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java中employee_java Employee(雇员)

public class Employee {//类Employee有两个属性,name,sexString name;char sex;Employee(String n,char s){//用new使用时构造该类,把n,s分别赋值namen;sexs;}public String getName(){//获取namereturn name;}public char getSex(){//获取sexreturn sexl;//你这里…

.NET5来了你别慌

近日微软.Net大咖Scott在博客中对外宣传.NET5首个预览版,并且我们可以通过微软的官网下载SDK5和运行库。很多朋友感觉.NetCore3.1还没搞明白,.NET5就来了感觉一下子慌了神。在这里我提醒朋友们,瞬息万变的世界中,总有相对不变的真…

《C++ Primer》14.3.2节练习(部分)

练习14.18: String类的关系运算符就是比较两个字符串字典序的先后。 class String {friend bool operator<(const String &s1,const String &s2);friend bool operator<(const String &s1,const String &s2);friend bool operator>(const String &am…

java8 stream 最大值_JDK8-Stream流常用方法

Stream流的使用流操作是Java8提供一个重要新特性&#xff0c;它允许开发人员以声明性方式处理集合&#xff0c;其核心类库主要改进了对集合类的 API和新增Stream操作。Stream类中每一个方法都对应集合上的一种操作。将真正的函数式编程引入到Java中&#xff0c;能 让代码更加简…

周三晚6点半!盛派首席架构师“苏老师”在线解密内部系统框架!

工作中有些事&#xff0c;看起来只用一会会儿就能完成&#xff0c;但真正完成起来&#xff0c;总会遇到一些意想不到的困难&#xff01;你一定碰到过这样的情况——开发时间 2 周的项目&#xff0c;搭框架就要用 1 周&#xff0c;刚开发完&#xff0c;各种调试和修 bug又花去 2…

《C++ Primer》14.4节练习(部分)

练习14.20: class Sales_data {friend Sales_data operator(const Sales_data &lhs,const Sales_data &rhs);public:Sales_data &operator(const Sales_data &rhs); }Sales_data operator(const Sales_data &lhs,const Sales_data &rhs) {Sales_data …

java 二维高斯_Java Random nextGaussian()用法及代码示例

随机类的nextGaussian()方法返回下一个伪随机数&#xff0c;即与随机数生成器序列的平均值为0.0&#xff0c;标准差为1.0的高斯(正态)分布双精度值。用法:public double nextGaussian()参数&#xff1a;该函数不接受任何参数。返回值&#xff1a;此方法返回平均值为0.0&#xf…

给微软的日志框架写一个基于委托的日志提供者

动手造轮子&#xff1a;给微软的日志框架写一个基于委托的日志提供者Intro微软的日志框架现在已经比较通用&#xff0c;有时候我们不想使用外部的日志提供者&#xff0c;但又希望提供一个比较简单的委托就可以实现日志记录&#xff0c;于是就有了后面的探索和实现。Solution基于…

C++分析使用拷贝控制成员和调用构造函数的时机

我们来分析下面这段代码&#xff1a; #include <iostream> #include <vector>using namespace std;struct X {X() {cout << "构造函数X()" << endl;}X(const X &) {cout << "拷贝构造函数X(const X&)" << en…

mybatis mysql模糊查询_详解MyBatis模糊查询LIKE的三种方式

模糊查询也是数据库SQL中使用频率很高的SQL语句&#xff0c;使用MyBatis来进行更加灵活的模糊查询。直接传参法直接传参法&#xff0c;就是将要查询的关键字keyword,在代码中拼接好要查询的格式&#xff0c;如%keyword%,然后直接作为参数传入mapper.xml的映射文件中。public vo…

《C++ Primer》13.1.4节练习

练习13.14: 这是一个典型的应该定义拷贝控制成员的场合。如果不定义拷贝构造函数和拷贝赋值运算符&#xff0c;依赖合成的版本&#xff0c;则在拷贝构造和赋值时&#xff0c;会简单复制数据成员。对本问题来说&#xff0c;就是将序号简单复制给新对象。 因此&#xff0c;代码中…

十问十答 CDDL 许可证

今天我们来整理一下通用开发和发行许可证 CDDL 的十大问题清单。通用开发与发行许可证&#xff08;Common Development and Distribution License&#xff0c;CDDL&#xff09;由已被甲骨文公司收购的太阳微系统公司&#xff08;Sun Microsystems&#xff09;发布的一种开源许可…

浅谈java spring_浅谈Spring(一)

Spring是当前比较流行的基于Java语言的MVC框架,所谓框架也就是它已经实现好了诸多东西,使java开发人员能把精力尽量放在业务逻辑上.Spring技术的特点是IOC, 即反向注入,主要应用的是XML技术和POJO(简单Java对象),Spring要达到的目的其实很简单,就是尽量简化原来Java中的地层数据…

Http Server API路由请求到web程序

引言接上文&#xff0c;容器内web程序一般会绑定到http://0.0.0.0:{某监听端口}或http://:{某监听端口}&#xff0c;以确保使用容器IP可以访问到web应用。正如我们在ASP.NET Core官方镜像显示的&#xff0c;ASP.NET Core程序在容器内80端口监听请求This image sets the ASPNETC…

mysql 多行拼接注入_MySQL注入汇总

Mysql注释符&#xff1a;单行注释&#xff1a; # 在对URL使用过程中可能遇到Unicode编码问题&#xff0c;可常用%23代替多行注释&#xff1a;/**/单行注释&#xff1a; -- 此处需要注意后面存在空格&#xff0c;否则报错0.万能密码(基于SQL验证&#xff0c;SQL注入)aaa or 1#aa…

《C++ Primer》13.1.6节练习(部分)

练习13.18: #include <iostream> #include <string> using namespace std;class Employee {private:static int sn;public:Employee() {mysn sn;}Employee(const string &s) {name s;mysn sn;}const string &get_name() {return name;}int get_mysn() …

用Azure Custom Vision 零代码创建一个口罩识别模型

新冠肺炎下&#xff0c;地球是一家&#xff0c;不分国籍&#xff0c;不分种族&#xff0c;或者现在只能呆在家中&#xff0c;但是也是一种对抗疫的支持。停课不停学留在家中&#xff0c;不仅是对学生&#xff0c;对于所有人都是有用的。在现阶段&#xff0c;大家可能最需要的不…

C++拷贝构造函数调用时机分析

让我们来分析下面这段代码&#xff1a; #include <iostream> #include <string> using namespace std;class Employee {private:static int sn;public:Employee() {cout << "Employee()" << endl;mysn sn;}Employee(const string &s) …

java开发中准则怎么写_Java开发中通用的方法和准则20条

1. 不要在常量和变量中出现易混淆的字母包名全小写、类名首字母全大写、常量全部大写并下划线分割、变量采用驼峰命名等&#xff0c;这些是最基本的Java编码规范。public class TestDemo {public static void main(String[] args) {long i 1l;System.out.println("i的两倍…

百万年薪程序员的7点能力

作者介绍findyi&#xff0c;腾讯、360码农&#xff0c;前哒哒少儿英语技术VP&#xff0c;现任土豆教育CTO。几周前&#xff0c;微盟爆了个大雷&#xff0c;数据库让内部员工删库跑路。写了篇文章&#xff0c;做了一些我的判断&#xff1a;从微盟36小时故障&#xff0c;谈谈数据…