带有 lambda 比较器错误的 C++ priority_queue

2023-06-30C/C++开发问题
1

本文介绍了带有 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数

无法访问 C++ std::set 中对象的非常量成员函数

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数

从 lambda 构造 std::function 参数

Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现

STL BigInt 类实现

STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠

使用 std::atomic 和 std::condition_variable 同步不可靠

Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾

在 STL 中将列表元素移动到末尾

Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?

Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6

热门文章

1LNK2038:检测到“RuntimeLibrary"不匹配:值“MT_StaticRelease" 2c++ 编译错误:ISO C++ 禁止指针和整数之间的比较 3CMake 找不到源文件 (add_executable) 4Cmake 链接库目标链接错误 5如何使用 VideoWriter 从 OpenCV 打开 GStreamer 管道 6CMakeLists.txt:30 (project) 中的 CMake 错误:找不到 CMAKE_C_COMPILER 7CMake 无法确定目标的链接器语言 8如何解决munmap_chunk():C++中的无效指针错误

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序