您的位置:首页 > 其它

结构体中运算符的重载

2016-04-25 15:15 453 查看
C++中,结构体是无法进行==,>,<,>=,<=,!=这些操作的,这也带来了很多不方便的地方,尤其是在使用STL容器的时候,如果我们可以往语句中传入结构体,一些事情将会变得很简单。
比如二分查找,binary_crearch只能对数组进行查找,如果是结构体数组的话,它会报错。但很可惜,实际编程中,大部分时候操作对象是结构体数组。
二分查找结构体数组的程序如下:
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

struct point
{
int elem;
bool operator==(const point b) const
{
return this->elem == b.elem;
}
bool operator!=(const point b) const
{
return this->elem != b.elem;
}
bool operator<=(const point b) const
{
return this->elem <= b.elem;
}
bool operator<(const point b) const
{
return this->elem < b.elem;
}
bool operator>=(const point b) const
{
return this->elem >= b.elem;
}
bool operator>(const point b) const
{
return this->elem > b.elem;
}
}a[10002],now;

int main()
{
bool flag;
int i, n, k;
scanf("%d", &n);
for (i = 0; i <= n - 1; i++)
{
scanf("%d", &a[i].elem);
}
scanf("%d", &k);
for (i = 0; i <= k - 1; i++)
{
scanf("%d", &now.elem);
flag = binary_search(a, a + n , now);
if (flag == true)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}

a是结构体数组,里面包含元素elem,我们想按elem的值进行二分查找。
重载运算符的格式如下:
bool operator 运算符 (const 结构体名称 b) const
{

    return(什么时候这个运算符对结构体成立);//注意对此运算符使用this->元素名;
}
并要注意binary_search的第三个参数也要写成结构体。
这样就可以顺利实现结构体数组的二分查找了。

重载的时候,如果你不知道STL内部使用了哪些运算符,就最好把上面六种运算符全部重载了,当让,如果你知道了STL的内部运行原理,也可以只重载它内部使用了的运算符,或者只重载你想要改变的运算符。
比如优先队列程序:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

struct point
{
unsigned long long elem;
bool operator<(const point b)const
{
return this->elem>b.elem;
}
};

priority_queue <point> q;
point create, now;

int n;

void clearqueue()
{
while (!q.empty())
{
q.pop();
}
return;
}

int main()
{
while (1)
{
scanf("%d", &n);
clearqueue();
if (n == 0)
{
break;
}
now.elem = 1;
q.push(now);
while (!q.empty())
{
now = q.top();
if (now.elem%n == 0)
{
break;
}
else
{
q.pop();
create.elem = now.elem * 10;
q.push(create);
if (now.elem % 10 == 0)
{
create.elem = now.elem + 1;
q.push(create);
}
}
}
printf("%lld\n", now);
}
return 0;
}

我只想让小的元素处于队列顶端,那么就可以只改变<的判断方式,其他不改变,那么就只重载了小于。
又比如,六数码问题中,判断达到目标的条件是结构体的六个元素分别相等。但结构体不能直接写“==”号,于是有
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>

using namespace std;

struct state
{
int a, b, c, d, e, f;
bool operator==(const state t)const
{
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);
}
}s, now, temp, t;

bool vis[6][6][6][6][6][6];
queue<state> q;

state alpha(state s)
{
int t;
t = s.a;
s.a = s.d;
s.d = s.e;
s.e = s.b;
s.b = t;
return s;
}

state beita(state s)
{
int t;
t = s.b;
s.b = s.e;
s.e = s.f;
s.f = s.c;
s.c = t;
return s;
}

void clearqueue()
{
while (!q.empty())
{
q.pop();
}
return;
}

int main()
{
int a, b, c, d, e, f, flag;
t.a = 1;
t.b = 2;
t.c = 3;
t.d = 4;
t.e = 5;
t.f = 6;
while (scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f) != EOF)
{
s.a = a;
s.b = b;
s.c = c;
s.d = d;
s.e = e;
s.f = f;
flag = 0;
clearqueue();
q.push(s);
memset(vis, false, sizeof(vis));
vis[a][b][c][d][e][f] = true;
while (!q.empty())
{
now = q.front();
if (now==t)//****************now和t两个结构体的相等
{
flag = 1;
break;
}
q.pop();
temp = alpha(now);
if (vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] == false)
{
q.push(temp);
vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] = true;
}
temp = beita(now);
if (vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] == false)
{
q.push(temp);
vis[temp.a][temp.b][temp.c][temp.d][temp.e][temp.f] = true;
}
}
if (flag == 1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
利用重载可以简化代码,和STL一起运用会产生很大的威力,并且最好了解STL内部的运行原理,以便能正确重载运算符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: