[英]Getting the smallest element using std::min_element in vector of maps c++ via based-range loop
I'm trying to get min value using std::min_element in a function.我试图在 function 中使用 std::min_element 获得最小值。
std::vector<std::map<int, double>> vectorMap;
std::map<int, double> data;
bool compare(std::pair<int, double> i, std::pair<int, double> j) {
return i.second < j.second;
}
double findMin(std::vector<std::map<int, double>> const &vectorMap){
for (auto const &vec : vectorMap){
for (auto const &mymap : vec){
std::pair<int, double> min = (*std::min_element(mymap.begin(), mymap.end(), compare));
}
}
return static_cast<double>(min.second);
}
While compiling I'm getting these errors:编译时出现以下错误:
main.cpp:59:67: error: no member named 'begin' in 'std::pair<const int, double>'
std::pair<int, double> min = (*std::min_element(mymap.begin(), mymap.end(), compare));
~~~~~ ^
main.cpp:59:82: error: no member named 'end' in 'std::pair<const int, double>'
std::pair<int, double> min = (*std::min_element(mymap.begin(), mymap.end(), compare));
~~~~~ ^
main.cpp:62:32: error: use of undeclared identifier 'min'
return static_cast<double>(min.second);
But when using the std::min_element in the main function it works fine.但是当在主 function 中使用 std::min_element 时,它工作正常。
std::vector<std::map<int, double>> vectorMap;
std::map<int, double> data;
...
...
std::pair<int, double> min = (*std::min_element(data.begin(), data.end(), compare));
std::cout << "min value is " << min.second << std::endl; //works well
What am I doing wrong?我究竟做错了什么? Thank you.谢谢你。
You have confusing naming here:你在这里有令人困惑的命名:
double findMin(std::vector<std::map<int, double>> const &vectorMap){
for (auto const &vec : vectorMap){
for (auto const &mymap : vec){
std::pair<int, double> min = (*std::min_element(mymap.begin(), mymap.end(), compare));
}
}
return static_cast<double>(min.second);
}
In the for (auto const &vec: vectorMap){
line you iterate over elements of the vector, so each element of it is a map (it is obvious from its type - std::vector<std::map<int, double>>
).在for (auto const &vec: vectorMap){
行中,您遍历向量的元素,因此它的每个元素都是 map (从它的类型很明显 - std::vector<std::map<int, double>>
)。 So each element of the vector is a map<int, double>
.所以向量的每个元素都是一个map<int, double>
。 But you call each element as a vec
and this name may mess things for you.但是您将每个元素都称为vec
,而这个名称可能会让您一头雾水。 Then you iterate over each map:然后遍历每个 map:
for (auto const &mymap : vec)
and here naming is also wrong.而且这里的命名也是错误的。 As you iterate over map then each element of it is a pair<int, double>
which map consists of.当您遍历 map 时,它的每个元素都是 map 组成的pair<int, double>
。 That's why pair
doesn't have begin/end
methods.这就是为什么pair
没有begin/end
方法。 As as mentioned by Margarit Alexandru you don't need a second loop, as your vec
is already a map
that can be passed to std::min_element
.正如Margarit Alexandru所提到的,您不需要第二个循环,因为您的vec
已经是可以传递给std::min_element
map
Just use correct naming)只需使用正确的命名)
You are using min_element on each element of the map (of type std::pair<int, double>).您在 map(std::pair<int, double> 类型)的每个元素上使用 min_element。 You should have only one for instead of 2.你应该只有一个而不是 2。
double findMin(std::vector<std::map<int, double>> const &vectorMap){
for (auto const &vec : vectorMap){
std::pair<int, double> min = (*std::min_element(vec.begin(), vec.end(), compare));
}
return static_cast<double>(min.second);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.