如何在具有唯一指针值的地图上使用 std::min_element? (C++)
How to use std::min_element on map with unique pointer values? (C++)
我有一个具有唯一指针值的映射,并且想要获得对具有最小值的对的引用。我正在使用下面的代码来执行此操作,但在调用 std::min_element
的行中出现错误。错误信息是:no matching function for call to object...
。我应该怎么做才能解决这个问题?
using pair_type = std::pair<std::string, std::unique_ptr<int>>;
std::map<std::string, std::unique_ptr<int>> map;
map.insert(std::make_pair("a", std::make_unique<int>(1)));
map.insert(std::make_pair("b", std::make_unique<int>(2)));
map.insert(std::make_pair("c", std::make_unique<int>(3)));
pair_type& min_pair = std::min_element(std::begin(map), std::end(map),
[](pair_type &p1, pair_type &p2) {
return *p1.second < *p2.second;
});
std::map
的值类型是std::pair<const Key, Value>
,当键在地图中时,您不能编辑它。
using map_type = std::map<std::string, std::unique_ptr<int>>;
map_type map;
map.insert(std::make_pair("a", std::make_unique<int>(1)));
map.insert(std::make_pair("b", std::make_unique<int>(2)));
map.insert(std::make_pair("c", std::make_unique<int>(3)));
map_type::iterator it = std::min_element(std::begin(map), std::end(map),
[](map_type::const_reference p1, map_type::const_reference p2) {
return *p1.second < *p2.second;
});
我有一个具有唯一指针值的映射,并且想要获得对具有最小值的对的引用。我正在使用下面的代码来执行此操作,但在调用 std::min_element
的行中出现错误。错误信息是:no matching function for call to object...
。我应该怎么做才能解决这个问题?
using pair_type = std::pair<std::string, std::unique_ptr<int>>;
std::map<std::string, std::unique_ptr<int>> map;
map.insert(std::make_pair("a", std::make_unique<int>(1)));
map.insert(std::make_pair("b", std::make_unique<int>(2)));
map.insert(std::make_pair("c", std::make_unique<int>(3)));
pair_type& min_pair = std::min_element(std::begin(map), std::end(map),
[](pair_type &p1, pair_type &p2) {
return *p1.second < *p2.second;
});
std::map
的值类型是std::pair<const Key, Value>
,当键在地图中时,您不能编辑它。
using map_type = std::map<std::string, std::unique_ptr<int>>;
map_type map;
map.insert(std::make_pair("a", std::make_unique<int>(1)));
map.insert(std::make_pair("b", std::make_unique<int>(2)));
map.insert(std::make_pair("c", std::make_unique<int>(3)));
map_type::iterator it = std::min_element(std::begin(map), std::end(map),
[](map_type::const_reference p1, map_type::const_reference p2) {
return *p1.second < *p2.second;
});