Q:

C++ Find N-th highest element of a map

I have a string vector std::vector<string> list and I am trying to find N-th highest repeated element of the vector. 
I got a map include elements of the vector and repetition number of them.
std::map<std::string , int> mapa;
for(int i = 0 ; i<list.size() ; i++)
  mapa[list[i]]++;

How can find N-th highest of them from the map?
Example vector:
qwe asd qwe asd zxc asd zxc qwe qwe asd sdf asd fsd 

if N is 2, I need an out put like    
asd 5
qwe 4

A:

You might use std::partial_sort:
std::map<std::string, std::size_t>
compute_frequencies(const std::vector<std::string>& words)
{
    std::map<std::string, std::size_t> res;
    for(const auto& word : words) {
        res[word]++;
    }
    return res;    
}

std::vector<std::pair<std::string, std::size_t>>
as_vector(const std::map<std::string, std::size_t>& m)
{
    return {m.begin(), m.end()};
}

int main() {
    const std::vector<std::string> words{
        "qwe", "asd", "qwe", "asd", "zxc", "asd",
        "zxc", "qwe", "qwe", "asd", "sdf", "asd", "fsd"
    };
    auto frequencies = as_vector(compute_frequencies(words));
    std::partial_sort(frequencies.begin(), frequencies.end(), frequencies.begin() + 2,
        [](const auto& lhs, const auto& rhs) {
            return lhs.second > rhs.second;    
        });
    for (std::size_t i = 0; i != 2; ++i) {
        std::cout << frequencies[i].first << " " << frequencies[i].second << std::endl;  
    }
}

Demo

