1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <map>
#include <iostream>
#include <string>
#include <functional>
#include <iterator>
#include <random>
std::mt19937 rng(std::random_device {}());
struct PLAY {
std::string name;
};
using MYMAP = std::multimap<int, PLAY, std::greater<int>>;
auto top(const MYMAP& mp) {
const auto high_score { mp.equal_range(mp.begin()->first) };
std::uniform_int_distribution<ptrdiff_t> song(ptrdiff_t(0), std::distance(high_score.first, high_score.second) - 1);
auto chose { mp.begin() };
std::advance(chose, song(rng));
return chose;
}
int main() {
const MYMAP myMap { {4, {"qwet"}}, {3, {"asdf"}}, {4, {"poiu"}}, {2, {"lkj"}}, {4, {"mnbv"}} };
std::cout << "Chosen top song is " << top(myMap)->second.name << '\n';
}
| |