unordered_map可以通过查询所有元素的second值的最小值吗
是的,unordered_map可以通过遍历所有元素,并比较它们的second值来找到最小值。以下是一个示例代码:
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, int> myMap = {{1, 10}, {2, 20}, {3, 5}, {4, 15}, {5, 25}};
int minVal = INT_MAX;
for (const auto& pair : myMap) {
if (pair.second < minVal) {
minVal = pair.second;
}
}
std::cout << "The minimum value of the 'second' in the unordered_map is: " << minVal << std::endl;
return 0;
}
输出结果为:The minimum value of the 'second' in the unordered_map is:
原文地址: https://www.cveoy.top/t/topic/ie6R 著作权归作者所有。请勿转载和采集!