简体   繁体   English

使用Lambda函数在std :: unordered_map中查找最小值

[英]Using lambda function to find a minimum value in a std::unordered_map

I am trying to find the element with the minimum value in the map. 我正在尝试在地图中找到具有最小值的元素。 For example, if my map has 例如,如果我的地图有

 { (1, 12.3),(2, 6.51), (3, 1.24)}

I would like to find the element (3, 1.24) . 我想找到元素(3, 1.24)


I wrote the following code, which tries to write a comparator in the lambda format 我编写了以下代码,该代码试图以lambda格式编写比较器

std::pair<int, double> min = *std::min_element(
    my_map.begin(), my_map.end(),
    [](std::unordered_map<int, double> a, std::unordered_map<int, double> b) { return a.second < b.second; });

But I got the following errors: 但是我遇到了以下错误:

error: no matching function for call to object of type '(lambda at my_code.cpp:118:9)'
            if (__comp(*__i, *__first))
                ^~~~~~
my_code.cpp:116:40: note: in instantiation of function template specialization 'std::__1::min_element<std::__1::__hash_map_iterator<std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<int, double>, void *> *> >, (lambda at my_code.cpp:118:9)>' requested here
    std::pair<int, double> min = *std::min_element(
                                       ^
my_code.cpp:118:9: note: candidate function not viable: no known conversion from 'std::__1::__hash_map_iterator<std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<int, double>, void *> *> >::value_type' (aka 'pair<const int, double>') to 'std::unordered_map<int, double>' for 1st argument
        [](std::unordered_map<int, double> a, std::unordered_map<int, double> b) { return a.second < b.second; });
        ^
my_code.cpp:118:9: note: conversion candidate of type 'void (*)(std::unordered_map<int, double>, std::unordered_map<int, double>)'
3 errors generated.

Any idea what I did wrong and what's the proper way to fix this? 知道我做错了什么以及解决此问题的正确方法是什么? Thanks! 谢谢!

Your lambda should be binary predicate which takes two pairs of std::pair<const int, double> . 您的lambda应该是二进制谓词,它需要两对std::pair<const int, double>

Change the lambda to as follows: 将lambda更改为如下:

std::pair<int, double> min = *std::min_element(
    my_map.begin(), my_map.end(),
    [](const auto &a, const auto &b) { return a.second < b.second; });

or more explicitly: 或更明确地:

std::pair<int, double> min = *std::min_element(
    my_map.begin(), my_map.end(),
    [](const std::pair<const int, double> &a, const std::pair<const int, double> &b) { return a.second < b.second; });
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You are trying to use lambda that accepts maps themselves, not elements of it: 您正在尝试使用本身接受地图而非其元素的lambda:

[](std::unordered_map<int, double> a, std::unordered_map<int, double> b)

so it's either (verbose): 所以要么(冗长):

[](std::unordered_map<int, double>::value_type a, std::unordered_map<int, double>::value_type b)

or simpler: 或更简单:

[](std::pair<int, double> a, std::pair<int, double> b)

or if you have c++14 or later: 或者,如果您拥有c ++ 14或更高版本:

[](auto a, auto b)

you may also consider to pass elements by const reference not to copy them for every iteration. 您也可以考虑通过const引用传递元素,而不是每次迭代都复制它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM