zoukankan      html  css  js  c++  java
  • c++重载运算符

    **运算符重载的意义

    重载运算符,能够让本来繁琐的程序变得简单,让代码模块化,更容易理解。还有,有些STL容器必须定义小于号,所以当该容器存储元素类型为自定义的结构体时,就需要重载运算符,定义小于号,例如 $set$,$priority_queue$

    语法格式如下:

    <返回类型> 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!
    ";
    15     else cout<<"a>=b!
    ";
    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$,表示坐标。

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

  • 相关阅读:
    mysql索引、group by、explain、insert delayed
    mysql学习笔记
    Handler
    文件操作
    RatingBar
    ListView(二)
    文件管理函数的草稿
    SAX解析XML
    phpunit
    Service(一)
  • 原文地址:https://www.cnblogs.com/very-beginning/p/12491061.html
Copyright © 2011-2022 走看看