首页 > 编程语言 > 详细

c++重载运算符

时间:2020-03-14 12:10:29      阅读:52      评论:0      收藏:0      [点我收藏+]

 

语法格式如下:

<返回类型> operator <运算符符号>(<参数>)
{
    <定义>;
}

实际应用比如:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 struct newint{
 7     int num;
 8     bool operator < (newint v){
 9         return num>v.num; 
10     }    
11 }a,b;
12 int main(){
13     a.num=2,b.num=1;
14     if (a<b)cout<<"a<b!\n";
15     else cout<<"a>=b!\n";
16     return 0;
17 }

在上面代码中,因为满足a.num>b.num(2>1)这个条件,所以a<b

再来举个例子,如下代码,因为 a.value<b.value,所以编译器认为a<b

这时,小于号的判定与 name 无关,即无论 name 是什么,只要满足 a.value<b.value,那么 a 就小于 b

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 struct fruit{
 7     string name;
 8     int value;
 9     bool operator < (fruit c){
10         return value<c.value; 
11     }    
12 }a,b;
13 int main(){
14     a.name="apple",a.value=1;
15     b.name="banana",b.value=2;
16     if (a<b)cout<<a.name<<"<"<<b.name;
17     else cout<<a.name<<">="<<b.name;
18     return 0;
19 }
重载四则运算符,例如“+”

type_name operator + (type_name name){

  加法运算法则

}

如70=a.a+b.a=15+55,253=a.b+b.b=20+233

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 struct newint{
 7     int a,b;
 8     newint operator + (newint y){
 9         newint z;
10         z.a=a+y.a;
11         z.b=b+y.b;
12         return z;
13     }    
14 }a,b;
15 int main(){
16     a.a=15,a.b=20;
17     b.a=55,b.b=233;
18     a=a+b;
19     cout<<a.a<<" "<<a.b<<endl;
20     return 0;
21 }

乘法和加法一样定义,来个加强版

如下,880=a.a*b.a+b.a=15*55+55,4893=a.b*b.b+b.b=20*233+233

通过运算符重载,我们也可以任意更改结构体四则运算的规则

3850=(a.a+b.a)*b.a=(15+55)*55,58949=(a.b+b.b)*b.b=(20+233)*233

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 struct newint{
 7     int a,b;
 8     newint operator + (newint y){
 9         newint z;
10         z.a=a+y.a;
11         z.b=b+y.b;
12         return z;
13     }    
14     newint operator * (newint y){
15         newint z;
16         z.a=a*y.a;
17         z.b=b*y.b;
18         return z;
19     }    
20 }a,b;
21 int main(){
22     a.a=15,a.b=20;
23     b.a=55,b.b=233;
24     a=a*b+b;
25     cout<<a.a<<" "<<a.b<<endl;
26     return 0;
27 }

友元运算符重载

和之前差不多,不过更容易理解

5=a.a+b.b=1+4,6=a.b*b.a=2*3

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 struct newint{
 7     int a,b;
 8     friend newint operator + (newint x,newint y){
 9         newint z;
10         z.a=x.a+y.b,z.b=x.b*y.a;
11         return z;
12     }
13 }x,y;
14 int main(){
15     x.a=1,x.b=2,y.a=3,y.b=4;
16     x=x+y;
17     cout<<x.a<<" "<<x.b<<endl;
18     return 0;
19 }

在优先队列(priority_queue)中,存储的元素较大的会被放到堆顶。如果存的是int或者string等类型还好办(因为他们本身就可以互相比较大小),如果是我们自定义的结构体类型,那就需要重载<运算符。

struct node
{
    int id;
    double x,y;
}//定义结构体
bool operator <(const node &a,const node &b)
{
    return a.x<b.x && a.y<b.y;
}//重载运算符“<”

*注:这里的结构体保存了一个整型变量id,两个长浮点变量x,y,表示坐标。

这里的重载运算符先比横坐标后比纵坐标。

 

c++重载运算符

原文:https://www.cnblogs.com/very-beginning/p/12491061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!