fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <iterator>
  7.  
  8.  
  9. int main ()
  10. {
  11. std::srand(std::time(nullptr));
  12. std::multimap<std::string, int> mymm;
  13.  
  14. mymm.emplace("Hello", 50);
  15. mymm.emplace("Bye", 100);
  16. mymm.emplace("Bye", 150);
  17. mymm.emplace("Bye", 200);
  18. mymm.emplace("?", 250);
  19. mymm.emplace("?", 300);
  20.  
  21. auto range = mymm.equal_range("Bye");
  22. auto count = std::distance(range.first, range.second);
  23. if (count != 0) {
  24. auto it = std::next(range.first, std::rand() % count);
  25. std::cout << it -> second;
  26. }
  27. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
200