Creating reusable polymorphic lambdas

Here are two vectors, one resembling a farm with the number of animals, and the other one mapping countries to their corresponding continent:

auto farm = std::vector<std::pair<std::string, int>>{ 
  {"Bear", 5}, 
  {"Deer", 0}, 
  {"Pig", 4} 
}; 
 
enum class Continent { Europe, Asia, America }; 
auto countries = std::vector<std::pair<std::string, Continent>>{ 
  {"Sweden", Continent::Europe}, 
  {"India", Continent::Asia}, 
  {"Belarus", Continent::Europe}, 
  {"Mexico", Continent::America} 
}; 

Let's say we want to sort the animals in order of how many the farm contains, and the countries in order of the continent they belong to. Speaking in code, we want to sort the vectors according to the std::pair::second member. As the vectors ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.