lambda自定义比较规则-sort函数或优先队列

Lambda表达式的一般形式为

[captures](params){body}

对于优先队列的自定义排序规则,常见方法是写成结构体形式

struct cmp{
	bool operator()(pair<int,int> map1,pair<int,int> map2){
		return map1.second>map2.second;
	}
};
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> que;

写成lambda表达式形式有两个注意点,一个是要用decltype声明,并且实例化时也要注意

auto cmp=[](pair<int,int> map1,pair<int,int> map2){return map1.second>map2.second;};
priority_queue<pair<int,int>,vector<pair<int,int>>, decltype(cmp)> que(cmp);

整体测试代码如下:

#include<bits\stdc++.h>
//#include<unordered_map>
//#include<priority_queue>
using namespace std;
struct cmp{
	bool operator()(pair<int,int> map1,pair<int,int> map2){
		return map1.second>map2.second;
	}
};
int main(){
	unordered_map<int,int> map;
	map[0]=3;
	map[1]=2;
	map[2]=6;
	map[3]=7;
	map[4]=8;
	//priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> que;
	auto cmp=[](pair<int,int> map1,pair<int,int> map2){return map1.second>map2.second;};
	priority_queue<pair<int,int>,vector<pair<int,int>>, decltype(cmp)> que(cmp);
	for(auto it:map){
		if(que.size()>3){
			que.pop();
		}
		que.push(it);
	}
	while(!que.empty()){
		cout<<que.top().second<<" ";
		que.pop();
	}
	system("pause");
}

对于sort函数而言,常见做法

bool cmp(int a,int b){
	return a>b;
}
sort(nums.begin(),nums.end(),cmp);

写成lambda表达式:

sort(nums.begin(),nums.end(),[](int a,int b){return a>b;});

如果在自定义函数内要用到外部定义的变量,那么lambda表达式更加合适
比如我们想根据对某个数组进行排序,并且想知道排序完后每个位置的数值对应原来的下标。就需要在自定义比较函数内部获取外部变量

#include<bits\stdc++.h>
using namespace std;
int main(){
	vector<int> nums{1,3,4,6,2,9};
	vector<int> index(nums.size());
	for(int i=0;i<nums.size();i++){
		index[i]=i;
	}
	//对索引数组进行排序,排序的依据是索引对应nums数组中的值
	sort(index.begin(),index.end(),[&](int i, int j){return nums[i]<nums[j];});
	//其中&代表以引用的方式获取外部变量 
	for(int i:index){
		cout<<i<<" ";
	}
	system("pause");
}

Lambda表达式获取外部变量的方式如下:
在这里插入图片描述

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用先进先出队列式分支限界法求解0-1背包问题的完整代码。代码中使用了 Python 3 编写。 ```python import queue class Node: def __init__(self, level, profit, weight, bound): self.level = level self.profit = profit self.weight = weight self.bound = bound def __lt__(self, other): return self.bound > other.bound def bound(node, n, W, items): if node.weight >= W: return 0 else: bound = node.profit j = node.level + 1 totweight = node.weight while (j < n) and (totweight + items[j][1] <= W): totweight += items[j][1] bound += items[j][0] j += 1 if j < n: bound += (W - totweight) * (items[j][0] / items[j][1]) return bound def knapsack_bfs(n, W, items): items.sort(key=lambda x: x[0]/x[1], reverse=True) q = queue.Queue() u = Node(-1, 0, 0, 0) v = None maxprofit = 0 u.bound = bound(u, n, W, items) q.put(u) while not q.empty(): u = q.get() if u.level == -1: v = Node(0, 0, 0, 0) elif u.level == n - 1: continue v.level = u.level + 1 v.weight = u.weight + items[v.level][1] v.profit = u.profit + items[v.level][0] if v.weight <= W and v.profit > maxprofit: maxprofit = v.profit v.bound = bound(v, n, W, items) if v.bound > maxprofit: q.put(v) v = Node(u.level + 1, u.profit, u.weight, 0) v.bound = bound(v, n, W, items) if v.bound > maxprofit: q.put(v) return maxprofit # 测试代码 if __name__ == '__main__': n = 4 W = 16 items = [(40, 2), (30, 5), (50, 10), (10, 5)] maxprofit = knapsack_bfs(n, W, items) print("最大价值为:", maxprofit) ``` 以上代码实现了先进先出队列式分支限界法求解0-1背包问题,其中 `Node` 类表示节点,`bound` 函数计算节点的上界,`knapsack_bfs` 函数使用 BFS 求解最大价值。在测试代码中,我们使用了一个包含 4 个物品、容量为 16 的背包和其价值、重量的列表来测试该算法。运行程序后,将输出最大价值为 90。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值