关于结构体重载,初始化,优先队列的排序(杂谈)


struct node{
	int ed,l,cost;
	node(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
//	node(int _v=0,int _c=0){
//		v=_v;
//		c=_c;
//	}
//	bool operator < (const node &a)const{
//		return
//	}
};
struct cmp{
	bool operator ()(const node &a,const node &b){
		if(a.l==b.l)
			return a.cost>b.cost;
		return a.l>b.l;
	}
};
struct Edge{//邻接表 
	int ed,l,cost;
	Edge(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
};
 

首先事结构体的直接引用,初始化赋值:

struct node{
	int a,b;
	node(int _a=0,int _b=0):a(_a),b(_b){}
    //结构体的直接赋值
};

//使用方便:可以直接进行结构体赋值
//例:
vector<node> n;


int main()
{
	n.push_back(node(1,2));//直接将a=1,b=2放入结构体
	cout<<n[0].a<<" "<<n[0].b;//尝试输出一下试试
    //output:1 2
    //是不是很方便?
}
struct node{
	int a,b;
	node(int _a=0,int _b=0){
		a=_a;
		b=_b;
	}
    //结构体的初始化赋值,样例大意:a,b初始化赋值0
};

结构体符号的重载:

样例1:

struct node{
	int a,b;//重载<,含义:结构体中a元素小的结构体优先
	bool operator < (const node &x)const{
		return x.a<a;
	}
};

样例2:

struct node{
	int a,b;//a元素相等的时候,b元素小的结构体优先
	bool operator < (const node &x)const{
		if(x.a==a)
			return x.b<b;
		return x.a<a;
	}
};

优先队列的排序问题:

首先要引用优先队列:priority_queue<node> que;

其中node是要引用的数据类型;

首先是优先队列的默认排序:

从大到小,例:

priority_queue<int> que;
//应用一个int类型的优先队列
int main()
{
	que.push(1);
	que.push(3);
	que.push(2);
	while(que.size())
	{
		cout<<que.top()<<" ";每次取出首元素
		que.pop();
	}
    //output:3 2 1
}

然后是系统自带的一些库排序:

less<node>,greater<node>

注意引用头文件<vector>,因为优先队列也是一个动态数组

priority_queue<int,vector<int>,less<int> > que;
//引用的一个less,从大到小,和默认一样
int main()
{
	que.push(1);
	que.push(3);
	que.push(2);
	while(que.size())
	{
		cout<<que.top()<<" ";
		que.pop();
	}
    //optput:3 2 1
}
priority_queue<int,vector<int>,greater<int> > que;
//引用的是greater,从小到大
int main()
{
	que.push(1);
	que.push(3);
	que.push(2);
	while(que.size())
	{
		cout<<que.top()<<" ";
		que.pop();
	}
    //output:1 2 3
}

其实这两个都不是很常用,毕竟我们还是习惯自定义排序规则,用着用着也舒服,安心(据说程序员总是蜜汁自信??

struct node{
	int a,b;
	node(int _a=0,int _b=0):a(_a),b(_b){}
};

struct cmp{
    //a大的优先
	bool operator()(const node &x,const node &y){
		return x.a<y.a;
	}
};

priority_queue<node,vector<node>,cmp> que;//cmp自定义排序

int main()
{
	que.push(node(1,2));
	que.push(node(5,8));
	que.push(node(2,3));
	while(que.size())
	{
		cout<<que.top().a<<" "<<que.top().b<<endl;;
		que.pop();
	}
    /*output:
            5 8
            2 3
            1 2
    */
}

当然,也可以有第二优先级的排序:

struct node{
	int a,b;
	node(int _a=0,int _b=0):a(_a),b(_b){}
};

struct cmp{
	bool operator()(const node &x,const node &y){
		if(x.a==y.a)//a元素相等时,b元素大的优先
			return x.b<y.b;
		return x.a<y.a;
	}
};

priority_queue<node,vector<node>,cmp> que;

int main()
{
	que.push(node(1,2));
	que.push(node(5,8));
	que.push(node(5,9));
	que.push(node(2,3));
	while(que.size())
	{
		cout<<que.top().a<<" "<<que.top().b<<endl;;
		que.pop();
	}
    /*output:
        5 9
        5 8
        2 3
        1 2
    */
}

好了,总结的差不多了,如果以后还学到东西,就再补充一下,如果有什么错误,请指出,博主表示感谢!

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81239448