运算符重载 与 sort()

运算符重载与sort()

二话不说上代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 struct type_1
 5 {
 6     int a,b;
 7     type_1(int x=0,int y=0):a(x),b(y){}
 8 };
 9 bool compare(type_1 a,type_1 b)
10 {
11     return a.a<b.a;
12 }
13 bool operator < (const type_1 &a,const type_1 &b)
14 {
15     return a.a<b.a;
16 }
17 ostream& operator<<(ostream &cout,const type_1 &a)
18 {
19     cout<<"("<<a.a<<","<<a.b<<")";
20 }
21 int main()
22 {
23     type_1 vec[10]={type_1(3,4),type_1(2,4),type_1(1,4),type_1(9,4),type_1(31,4),type_1(25,4),type_1(2,4),type_1(9,4),type_1(2,4),type_1(1,4)};
24     sort(vec,vec+10);//通过定义小于<
25     for(int i=0;i<10;i++)
26         cout<<vec[i]<<" ";
27         cout<<endl;
28 
29     return 0;
30 }

此函数 展示了:

①"<"逻辑符的重载

②"cout"输出的重载

③“sort()”的两种排序规则的定义:一种是通过定义逻辑函数"compare",另一种是通过重载逻辑运算符"<"

④更明白了 compare 是定义一种小于

2017-03-11 22:53:34

 

转载于:https://www.cnblogs.com/Twobox/p/6536556.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值