本文介绍了C ++ priority_queue与lambda比较器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下错误的代码,我试图在VC2010编译,但我得到错误这只发生在我包含lambda表达式,所以我猜它与这有关。

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;

按预期工作。任何想法我做错了什么?有没有明显错误的lambda,看起来错误,我可能会忽视?感谢您阅读!

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!

推荐答案

首先定义lambda对象,然后使用 decltype 并将其直接传递给构造函数。

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 );

这篇关于C ++ priority_queue与lambda比较器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 18:30