Home:ALL Converter>Using lambda function to find a minimum value in a std::unordered_map

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

Ask Time:2019-01-31T07:55:21         Author:Edamame

Json Formatter

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


I wrote the following code, which tries to write a comparator in the lambda format

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!

Author:Edamame,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54451381/using-lambda-function-to-find-a-minimum-value-in-a-stdunordered-map
JeJo :

Your lambda should be binary predicate which takes two pairs of std::pair<const int, double>.\n\nChange the lambda to as follows:\n\nstd::pair<int, double> min = *std::min_element(\n my_map.begin(), my_map.end(),\n [](const auto &a, const auto &b) { return a.second < b.second; });\n\n\nor more explicitly:\n\nstd::pair<int, double> min = *std::min_element(\n my_map.begin(), my_map.end(),\n [](const std::pair<const int, double> &a, const std::pair<const int, double> &b) { return a.second < b.second; });\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
2019-01-30T23:59:38
yy