Hacker News new | ask | show | jobs
by Wehrdo 3105 days ago
I'll bite.

  std::vector<std::pair<float, std::string>> in_order;
  std::transform(data.begin(), data.end(), std::back_inserter(in_order),
                 [](auto& item) {return std::make_pair(item.weight, item.name);});
  std::sort(in_order.begin(), in_order.end(), std::greater<>());
While I won't claim it to be as elegant as Python, it doesn't seem too ugly. Does anybody have an idea about how `auto` could be utilized to avoid the long vector<pair...> type declaration?

For the adventurous: https://repl.it/repls/RepentantGentleKudu

1 comments

Templates, auto, and type toys can give you some serious build-your-own syntactic sugar: https://repl.it/repls/PleasingLovingQueenslandheeler

Pulling things out of tuples isn't great, though.