Template Function otyca::top_n_by_value

Function Documentation

template<typename KeyType, typename ValueType>
std::vector<std::pair<KeyType, ValueType>> otyca::top_n_by_value(const std::map<KeyType, ValueType> &input_map, size_t n)

Returns the top N entries from a map, sorted by value in descending order.

This function takes a map of key-value pairs and returns the top N entries, sorted by the values in descending order. If the map contains fewer than N entries, the entire map is returned.

Note

If there are fewer than N entries in the map, all entries will be returned.

Template Parameters:
  • KeyType – Type of the keys in the input map.

  • ValueType – Type of the values in the input map.

Parameters:
  • input_map – The map to extract top N entries from.

  • n – The number of top entries to return.

Returns:

A vector of the top N key-value pairs from the input map, sorted by value in descending order.