结构体运算符重载
2017-10-25 09:27
162 查看
C++中,结构体是无法进行==,>,<,>=,<=,!=这些操作的,这也带来了很多不方便的地方,尤其是在使用STL容器的时候,如果我们可以往语句中传入结构体,一些事情将会变得很简单。
比如二分查找,binary_crearch只能对数组进行查找,如果是结构体数组的话,它会报错。但很可惜,实际编程中,大部分时候操作对象是结构体数组。
二分查找结构体数组的程序如下:
[cpp] view plain copy
#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的内部运行原理,也可以只重载它内部使用了的运算符,或者只重载你想要改变的运算符。
比如优先队列程序:
[cpp] view plain copy
#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;
}
我只想让小的元素处于队列顶端,那么就可以只改变<的判断方式,其他不改变,那么就只重载了小于。
又比如,六数码问题中,判断达到目标的条件是结构体的六个元素分别相等。但结构体不能直接写“==”号,于是有
[cpp] view plain copy
#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内部的运行原理,以便能正确重载运算符。
比如二分查找,binary_crearch只能对数组进行查找,如果是结构体数组的话,它会报错。但很可惜,实际编程中,大部分时候操作对象是结构体数组。
二分查找结构体数组的程序如下:
[cpp] view plain copy
#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的内部运行原理,也可以只重载它内部使用了的运算符,或者只重载你想要改变的运算符。
比如优先队列程序:
[cpp] view plain copy
#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;
}
我只想让小的元素处于队列顶端,那么就可以只改变<的判断方式,其他不改变,那么就只重载了小于。
又比如,六数码问题中,判断达到目标的条件是结构体的六个元素分别相等。但结构体不能直接写“==”号,于是有
[cpp] view plain copy
#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内部的运行原理,以便能正确重载运算符。
相关文章推荐
- 结构体运算符重载
- C++STL系列 结构体运算符重载及优先队列的使用
- 结构体运算符重载
- 结构体运算符重载
- 九度OJ 教程31 包含结构体的优先队列及运算符重载之《搬水果》
- 利用运算符重载将结构体排序--uva11729
- 结构体运算符重载
- 结构体运算符重载
- C# 自己写的一个类,用来将结构体或类中的数据打成"数据包",进行网络发送(转载)
- 结构体的定义位置不正确导致的错误
- 结构体和枚举
- 结构体数组及其应用
- C++中运算符重载
- 结构体字节对齐问题
- 分数运算符重载(4)
- 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
- C# Socket 入门5 UPD 结构体 与 C++ 通信
- C语言结构体分配内存问题
- 第13周项目1:分数类中的运算符重载(3)
- 结构体在多线程中用法