Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在std::map<;中找到最小值;标准::字符串,浮点值>;?_C++_Algorithm_Comparator_Stdmap_Minimum - Fatal编程技术网

C++ 如何在std::map<;中找到最小值;标准::字符串,浮点值>;?

C++ 如何在std::map<;中找到最小值;标准::字符串,浮点值>;?,c++,algorithm,comparator,stdmap,minimum,C++,Algorithm,Comparator,Stdmap,Minimum,我正在尝试获取std::map中的最小值。我有一个函数,它来自 当我运行这个函数时,我得到一个错误显示 Called object type 'bool (Tool::*)(std::__1::pair<std::__1::basic_string<char>, float>, std::__1::pair<std::__1::basic_string<char>, float>)' is not a function or function po

我正在尝试获取
std::map
中的最小值。我有一个函数,它来自

当我运行这个函数时,我得到一个错误显示

Called object type 'bool (Tool::*)(std::__1::pair<std::__1::basic_string<char>, float>, std::__1::pair<std::__1::basic_string<char>, float>)' is not a function or function pointer
被调用的对象类型“bool(工具::*)(std::\u 1::pair,std::\u 1::pair)”不是函数或函数指针
您提供的比较功能不是一个

应该是

using Pair = std::pair<const std::string, float>; // just a alias type for convenience
struct Tool
{
    bool operator()(const Pair& i, const Pair& j)const 
        /*^^^^^^^^*/                            /*^^^*/
    {
        return (i.second < j.second);
    }
};

但是,如果您可以访问C++11或更高版本,只需使用lambda,它将帮助您在调用的行上查看比较函数(二进制谓词)的定义

min = *min_element(direction.begin(), direction.end(), 
                   [](const auto& lhs, const auto& rhs){ return lhs.second < rhs.second;});
min=*min\u元素(direction.begin(),direction.end(),
[](const auto&lhs,const auto&rhs){返回lhs.second
如果您使用的是C++11或更高版本,只需使用lambda而不是成员函数即可。
using Pair = std::pair<const std::string, float>; // just a alias type for convenience
struct Tool
{
    bool operator()(const Pair& i, const Pair& j)const 
        /*^^^^^^^^*/                            /*^^^*/
    {
        return (i.second < j.second);
    }
};
min = *min_element(direction.begin(), direction.end(), Tool());
                                                       ^^^^^^
min = *min_element(direction.begin(), direction.end(), 
                   [](const auto& lhs, const auto& rhs){ return lhs.second < rhs.second;});