当前位置: 首页 > news >正文

建成学校网站重庆百度推广开户

建成学校网站,重庆百度推广开户,广州市网站建设 骏域动力,印鸽定制软件appC20增加了三路比较运算符<>&#xff08;戏称航天飞机运算符&#xff09;&#xff0c;用于对类的比较运算符进行统一的设计。有两种使用方式&#xff1a;默认比较对于某些类&#xff0c;如果按照其成员逐一比较即可决定比较运算符的值&#xff0c;那么可以使用默认的三路运…

C++20增加了三路比较运算符<=>(戏称航天飞机运算符),用于对类的比较运算符进行统一的设计。

有两种使用方式:

  1. 默认比较

对于某些类,如果按照其成员逐一比较即可决定比较运算符的值,那么可以使用默认的三路运算符,编译器为为类生成==,!=,<,>,<=,>=

#include <iostream>
#include <string>
using namespace std;class A{
public:A(int d, string s):m_d(d), m_s(s) {}ostream& pOut(ostream &os) const{os<<"A("<<m_d<<","<<m_s<<")";return os;}auto operator<=>(const A&) const = default;
private:int m_d;string m_s;
};ostream& operator <<(ostream &os, const A &a)
{return a.pOut(os);
}void doCompare(const A& d1, const A& d2)
{if(d1 > d2){cout<<d1<<" is bigger than "<<d2<<endl;}else if(d1 == d2){cout<<d1<<" is equal to "<<d2<<endl;}else if(d1 < d2){cout<<d1<<" is smaller than "<<d2<<endl;}
}int main()
{A a1(1, "hi"), a2(1, "hello"), a3(2, "a");doCompare(a1, a2);doCompare(a1, a1);doCompare(a1, a3);return 0;
}
运行程序输出:
A(1,hi) is bigger than A(1,hello)
A(1,hi) is equal to A(1,hi)
A(1,hi) is smaller than A(2,a)

默认的比较运算符将按照成员的顺序先比较m_d然后再比较m_s

如果将m_d与m_s定义的顺序反过来,那么会影响到比较的结果:

#include <iostream>
#include <string>
using namespace std;class A{
public:A(int d, string s):m_d(d), m_s(s) {}ostream& pOut(ostream &os) const{os<<"A("<<m_s<<","<<m_d<<")";return os;}auto operator<=>(const A&) const = default;
private:string m_s;int m_d; 
};ostream& operator <<(ostream &os, const A &a)
{return a.pOut(os);
}void doCompare(const A& d1, const A& d2)
{if(d1 > d2){cout<<d1<<" is bigger than "<<d2<<endl;}else if(d1 == d2){cout<<d1<<" is equal to "<<d2<<endl;}else if(d1 < d2){cout<<d1<<" is smaller than "<<d2<<endl;}
}int main()
{A a1(1, "hi"), a2(1, "hello"), a3(2, "a");doCompare(a1, a2);doCompare(a1, a1);doCompare(a1, a3);return 0;
}
运行程序输出:
A(hi,1) is bigger than A(hello,1)
A(hi,1) is equal to A(hi,1)
A(hi,1) is bigger than A(a,2)
  1. 定制比较

当默认的比较规则不合适时,可以通过定义<=>为类生成统一的比较运算符。

定制比较时可以分为三个级别,对应的返回值类型分别为:

返回类型

是否有无法比较的值

说明

(强序)std::strong_ordering

所有元素都可比较,且有严格的顺序关系(可比较,等值唯一),比如数值

(弱序)std::weak_ordering

所有元素都可比较,但存在近似相等的情况(可比较,等值不唯一),比如大小写字符

(偏序)std::partial_ordering

元素存在不可比较的情况(非全可比较,等值也不唯一),比如void*指针

以下实现一个不区分大小写的字符串比较:

#include <iostream>
#include <string>
using namespace std;auto strToUpper = [](string str)  
{string s = str;transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return toupper(c); });return s;
};class A{
public:A(string s):m_s(s) {}ostream& pOut(ostream &os) const{os<<"A("<<m_s<<")";return os;}weak_ordering operator<=>(const A& a) const{string sThis = strToUpper(m_s);string sA = strToUpper(a.m_s);if(sThis > sA){return weak_ordering::greater;}else if(sThis < sA){return weak_ordering::less;}return weak_ordering::equivalent;}string m_s;
};ostream& operator <<(ostream &os, const A &a)
{return a.pOut(os);
}void doCompare(const A& d1, const A& d2)
{if(d1 > d2){cout<<d1<<" is bigger than "<<d2<<endl;}else if(d1 < d2){cout<<d1<<" is smaller than "<<d2<<endl;}else{cout<<d1<<" is equal to "<<d2<<endl;}
}int main()
{A a1("hi"), a2("hello"), a3("Hi"), a4("ho");doCompare(a1, a2);doCompare(a1, a3);doCompare(a1, a4);return 0;
}
运行程序输出:
A(hi) is bigger than A(hello)
A(hi) is equal to A(Hi)
A(hi) is smaller than A(ho)

可以看到通过实现了<=>编译器会由此实现其他运算符。

http://www.hkea.cn/news/100791/

相关文章:

  • 基层建设论文收录在哪个网站百度统计api
  • 购买主机可以做网站吗楚雄今日头条新闻
  • 深圳专业网站建设公司哪家好宁波网络营销公司
  • ps做电商网站流程图百度图片识别搜索引擎
  • 做电影网站程序好用武汉网站建设推广公司
  • 如何做b2c网站下列关于友情链接说法正确的是
  • 网站开发中网页上传seo在线网站推广
  • 网站建设报价流程免费培训网站
  • 聊城网站建设服务好最新网域查询入口
  • 一般网站建设流程有哪些步骤官方百度app下载安装
  • 档案网站建设的步骤八大营销方式有哪几种
  • 手机网站免费制作平台semseo
  • 周口市做网站小红书seo排名
  • 网站开始怎么做的ps培训
  • 去哪个网站找题目给孩子做保定百度首页优化
  • 五道口网站建设惠州seo外包服务
  • 如何查看网站是否备案企业宣传册
  • 好的网站建设公司排名互联网宣传推广
  • 上海买二手房做哪个网站好seo快速排名优化
  • 十大中国网站制作免费广告
  • wordpress 文章主题关键词推广seo
  • 佛山网站建设佛山网络推广代运营公司靠谱吗
  • 贵阳疫情最新消息站内seo优化
  • wordpress相关问题深圳百度关键字优化
  • 做国珍新时代 网站陕西seo顾问服务
  • 建立网站怎么做关键字搜索引擎营销的作用
  • 广州手机网站建设宁波seo优化费用
  • 怎么设置网站服务器宁德市教育局官网
  • 查看网站源代码建站可以牛排seo系统
  • 政府网站建设的基本原则百度网盘电脑版