有关结构体内重载操作的最详细的讲解~~

时间:2022-07-24
本文章向大家介绍有关结构体内重载操作的最详细的讲解~~,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

C++中,结构体是无法进行==,>,<,>=,<=,!=这些操作的,这也带来了很多不方便的地方,尤其是在使用STL容器的时候,如果我们可以往语句中传入结构体,一些事情将会变得很简单 。 我们来举一个栗子: 身高,分数,序号

我们默认都是从小到大排序的!

#include<bits/stdc++.h>//C++万能头文件 
#define maxn 1000000+5
using namespace std;


struct  node{
	int high;
	int age;
	int num;
}a[maxn];

int cmpp(node a,node b){
	if(a.high == b.high){
		if(a.age == b.age)
			return a.num < b.num;
		else
			return a.age < b.age; 
	}
	else
		return a.high < b.high;
		
	
}



int main(){
	int N;
	cin>>N;//scnaf("%d",&N); 
	for(int i=1;i<=N;i++){
		cin>>a[i].high>>a[i].age;
		a[i].num = i;
	}
	sort(a,a+N+1,cmpp);
	
	for(int i=1;i<=N;i++){
		cout<<a[i].num<<" ";//printf("%d ",a[i].num);
	}
	return 0;
}

可以看到我们不用结构体内重载的时候,我们可以在外面定义一个函数,然后用到这个!

#include<bits/stdc++.h>//C++万能头文件
#define maxn 1000000+5
using namespace std;


struct  node{
	int high;
	int age;
	int num;
	bool operator <(const node &a)const{
	if(high == a.high){
		if(age == a.age){
			return num < a.num;
		}
		else{
			return age < a.age;
		}
	}
	return high < a.high;
}
}a[maxn];


int main(){
	int N;
	cin>>N;//scnaf("%d",&N);
	for(int i=1;i<=N;i++){
		cin>>a[i].high>>a[i].age;
		a[i].num = i;
	}
	sort(a,a+N+1);

	for(int i=1;i<=N;i++){
		cout<<a[i].num<<" ";//printf("%d ",a[i].num);
	}
	return 0;
}

当然还有对别的符号操作进行重载!!如下

#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("Yesn");
		}
		else
		{
			printf("Non");
		}
	}
	return 0;

a是结构体数组,里面包含元素elem,我们想按elem的值进行二分查找。 重载运算符的格式如下: bool operator 运算符 (const 结构体名称 b) const { return(什么时候这个运算符对结构体成立);//注意对此运算符使用this->元素名; }

bool为函数类型,operator<一起为函数的名字(const Inf r)定义了一个不可以修改的参数,大括号{}里面是返回值

并要注意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("%lldn", 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("Yesn");
		}
		else
		{
			printf("Non");
		}
	}
	return 0;

}