结构体中运算符的重载

来源:互联网 发布:阿里云yum安装mysql 编辑:程序博客网 时间:2024/05/17 21:57
C++中,结构体是无法进行==,>,<,>=,<=,!=这些操作的,这也带来了很多不方便的地方,尤其是在使用STL容器的时候,如果我们可以往语句中传入结构体,一些事情将会变得很简单。
比如二分查找,binary_crearch只能对数组进行查找,如果是结构体数组的话,它会报错。但很可惜,实际编程中,大部分时候操作对象是结构体数组。
二分查找结构体数组的程序如下:
[cpp] view plain copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <algorithm>  
  4.   
  5. using namespace std;  
  6.   
  7. struct point  
  8. {  
  9.     int elem;  
  10.     bool operator==(const point b) const  
  11.     {  
  12.         return this->elem == b.elem;  
  13.     }  
  14.     bool operator!=(const point b) const  
  15.     {  
  16.         return this->elem != b.elem;  
  17.     }  
  18.     bool operator<=(const point b) const  
  19.     {  
  20.         return this->elem <= b.elem;  
  21.     }  
  22.     bool operator<(const point b) const  
  23.     {  
  24.         return this->elem < b.elem;  
  25.     }  
  26.     bool operator>=(const point b) const  
  27.     {  
  28.         return this->elem >= b.elem;  
  29.     }  
  30.     bool operator>(const point b) const  
  31.     {  
  32.         return this->elem > b.elem;  
  33.     }  
  34. }a[10002],now;  
  35.   
  36. int main()  
  37. {  
  38.     bool flag;  
  39.     int i, n, k;  
  40.     scanf("%d", &n);  
  41.     for (i = 0; i <= n - 1; i++)  
  42.     {  
  43.         scanf("%d", &a[i].elem);  
  44.     }  
  45.     scanf("%d", &k);  
  46.     for (i = 0; i <= k - 1; i++)  
  47.     {  
  48.         scanf("%d", &now.elem);  
  49.         flag = binary_search(a, a + n , now);  
  50.         if (flag == true)  
  51.         {  
  52.             printf("Yes\n");  
  53.         }  
  54.         else  
  55.         {  
  56.             printf("No\n");  
  57.         }  
  58.     }  
  59.     return 0;  
  60. }  
a是结构体数组,里面包含元素elem,我们想按elem的值进行二分查找。
重载运算符的格式如下:
bool operator 运算符 (const 结构体名称 b) const
{
    return(什么时候这个运算符对结构体成立);//注意对此运算符使用this->元素名;
}
并要注意binary_search的第三个参数也要写成结构体。
这样就可以顺利实现结构体数组的二分查找了。
重载的时候,如果你不知道STL内部使用了哪些运算符,就最好把上面六种运算符全部重载了,当让,如果你知道了STL的内部运行原理,也可以只重载它内部使用了的运算符,或者只重载你想要改变的运算符。
比如优先队列程序:
[cpp] view plain copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <math.h>  
  4. #include <string.h>  
  5. #include <queue>  
  6.   
  7. using namespace std;  
  8.   
  9. struct point  
  10. {  
  11.     unsigned long long elem;  
  12.     bool operator<(const point b)const  
  13.     {  
  14.         return this->elem>b.elem;  
  15.     }  
  16. };  
  17.   
  18. priority_queue <point> q;  
  19. point create, now;  
  20.   
  21. int n;  
  22.   
  23. void clearqueue()  
  24. {  
  25.     while (!q.empty())  
  26.     {  
  27.         q.pop();  
  28.     }  
  29.     return;  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     while (1)  
  35.     {  
  36.         scanf("%d", &n);  
  37.         clearqueue();  
  38.         if (n == 0)  
  39.         {  
  40.             break;  
  41.         }  
  42.         now.elem = 1;  
  43.         q.push(now);  
  44.         while (!q.empty())  
  45.         {  
  46.             now = q.top();  
  47.             if (now.elem%n == 0)  
  48.             {  
  49.                 break;  
  50.             }  
  51.             else  
  52.             {  
  53.                 q.pop();  
  54.                 create.elem = now.elem * 10;  
  55.                 q.push(create);  
  56.                 if (now.elem % 10 == 0)  
  57.                 {  
  58.                     create.elem = now.elem + 1;  
  59.                     q.push(create);  
  60.                 }  
  61.             }  
  62.         }  
  63.         printf("%lld\n", now);  
  64.     }  
  65.     return 0;  
  66. }  
我只想让小的元素处于队列顶端,那么就可以只改变<的判断方式,其他不改变,那么就只重载了小于。
又比如,六数码问题中,判断达到目标的条件是结构体的六个元素分别相等。但结构体不能直接写“==”号,于是有
[cpp] view plain copy
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <queue>  
  4. #include <string.h>  
  5.   
  6. using namespace std;  
  7.   
  8. struct state  
  9. {  
  10.     int a, b, c, d, e, f;  
  11.     bool operator==(const state t)const  
  12.     {  
  13.         return(this->a == t.a&&this->b == t.b&&this->c == t.c&&this->d == t.d&&this->e == t.e&&this->f == t.f);  
  14.     }  
  15. }s, now, temp, t;  
  16.   
  17. bool vis[6][6][6][6][6][6];  
  18. queue<state> q;  
  19.   
  20. state alpha(state s)  
  21. {  
  22.     int t;  
  23.     t = s.a;  
  24.     s.a = s.d;  
  25.     s.d = s.e;  
  26.     s.e = s.b;  
  27.     s.b = t;  
  28.     return s;  
  29. }  
  30.   
  31. state beita(state s)  
  32. {  
  33.     int t;  
  34.     t = s.b;  
  35.     s.b = s.e;  
  36.     s.e = s.f;  
  37.     s.f = s.c;  
  38.     s.c = t;  
  39.     return s;  
  40. }  
  41.   
  42. void clearqueue()  
  43. {  
  44.     while (!q.empty())  
  45.     {  
  46.         q.pop();  
  47.     }  
  48.     return;  
  49. }  
  50.   
  51. int main()  
  52. {  
  53.     int a, b, c, d, e, f, flag;  
  54.     t.a = 1;  
  55.     t.b = 2;  
  56.     t.c = 3;  
  57.     t.d = 4;  
  58.     t.e = 5;  
  59.     t.f = 6;  
  60.     while (scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f) != EOF)  
  61.     {  
  62.         s.a = a;  
  63.         s.b = b;  
  64.         s.c = c;  
  65.         s.d = d;  
  66.         s.e = e;  
  67.         s.f = f;  
  68.         flag = 0;  
  69.         clearqueue();  
  70.         q.push(s);  
  71.         memset(vis, falsesizeof(vis));  
  72.         vis[a][b][c][d][e][f] = true;  
  73.         while (!q.empty())  
  74.         {  
  75.             now = q.front();  
  76.             if (now==t)//****************now和t两个结构体的相等  
  77.             {  
  78.                 flag = 1;  
  79.                 break;  
  80.             }  
  81.             q.pop();  
  82.             temp = alpha(now);  
  83.             if (vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] == false)  
  84.             {  
  85.                 q.push(temp);  
  86.                 vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] = true;  
  87.             }  
  88.             temp = beita(now);  
  89.             if (vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] == false)  
  90.             {  
  91.                 q.push(temp);  
  92.                 vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] = true;  
  93.             }  
  94.         }  
  95.         if (flag == 1)  
  96.         {  
  97.             printf("Yes\n");  
  98.         }  
  99.         else  
  100.         {  
  101.             printf("No\n");  
  102.         }  
  103.     }  
  104.     return 0;  
  105. }  
利用重载可以简化代码,和STL一起运用会产生很大的威力,并且最好了解STL内部的运行原理,以便能正确重载运算符。

原创粉丝点击