Код: Выделить всё
#include
#include
#include
#include
#include
struct Values
{
uint32_t a = 0;
uint32_t b = 0;
Values(uint32_t x, uint32_t y): a(x), b(y) {}
};
template
auto print_top_k(std::size_t k, MapType&& map)
{
std::vector v;
v.reserve(map.size());
std::transform(map.begin(), map.end(), std::back_inserter(v), [](const auto& kv) { return kv; });
std::sort(v.begin(), v.end(), [](const auto& lhs, const auto& rhs){ return lhs.second.a > rhs.second.b; });
v.erase(v.begin() + k, v.end());
for(const auto& kv: v)
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79382676/how-to-pick-top-k-elements-from-stdmap[/url]