C++ priority_queue with lambda comparator error(带有 lambda 比较器错误的 C++ priority_queue)
问题描述
我尝试在 VC2010 中编译以下错误代码,但出现错误 C2974 这只发生在我包含 lambda 表达式时,所以我猜它与此有关.
typedef pair, int>adjlist_edge;优先队列,[](adjlist_edge a, adjlist_edge b) ->布尔{if(a.second > b.second){ 返回真;} else { 返回假;}}>adjlist_pq; 我知道模板定义的形式是正确的
priority_queue、greater>pq; 按预期工作.任何想法我做错了什么?lambda 是否有明显的错误,看起来我可能忽略了?感谢阅读!
首先定义 lambda 对象,然后使用 decltype 将其传递给模板的类型,并直接将其传递给构造函数.
>
auto comp = []( adjist a, adjlist b ) { return a.second >b.第二;};优先队列,decltype(comp)>adjlist_pq(comp);I have the following erroneous code which I am trying to compile in VC2010, but I'm getting the error C2974 this only occurs when I include the lambda expression, so I'm guessing it has something to do with that.
typedef pair<pair<int, int>, int> adjlist_edge;
priority_queue< adjlist_edge , vector<adjlist_edge>,
[](adjlist_edge a, adjlist_edge b) -> bool {
if(a.second > b.second){ return true; } else { return false; }
}> adjlist_pq;
I know the form of the template definition is correct as
priority_queue<int , vector<int>, greater<int>> pq;
Works as expected. Any ideas what I'm doing wrong? Is there something obviously wrong with the lambda that looks wrong that I might be overlooking? Thanks for reading!
First define the lambda object, then pass it to the template's type using decltype and also pass it directly to the constructor.
auto comp = []( adjist a, adjlist b ) { return a.second > b.second; };
priority_queue< adjlist_edge , vector<adjlist_edge>, decltype( comp ) >
adjlist_pq( comp );
这篇关于带有 lambda 比较器错误的 C++ priority_queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 lambda 比较器错误的 C++ priority_queue
基础教程推荐
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
