Advertisement
kutuzzzov

Урок 5 функции с длинным списком аргументов

Jun 28th, 2023
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <random>
  4. #include <string>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. class RandomContainer {
  10. public:
  11.     void Insert(int val) {
  12.         values_pool_.insert(val);
  13.     }
  14.     void Remove(int val) {
  15.         values_pool_.erase(val);
  16.     }
  17.     bool Has(int val) const {
  18.         return values_pool_.count(val);
  19.     }
  20.     int GetRand() const {
  21.         uniform_int_distribution<int> distr(0, values_pool_.size() - 1);
  22.         int rand_index = distr(engine_);
  23.         return *next(values_pool_.begin(), rand_index);
  24.     }
  25.  
  26. private:
  27.     set<int> values_pool_;
  28.     mutable mt19937 engine_;
  29. };
  30.  
  31. int main() {
  32.     RandomContainer container;
  33.     int query_num = 0;
  34.     cin >> query_num;
  35.     for (int query_id = 0; query_id < query_num; query_id++) {
  36.         string query_type;
  37.         cin >> query_type;
  38.         if (query_type == "INSERT"s) {
  39.             int value = 0;
  40.             cin >> value;
  41.             container.Insert(value);
  42.         }
  43.         else if (query_type == "REMOVE"s) {
  44.             int value = 0;
  45.             cin >> value;
  46.             container.Remove(value);
  47.         }
  48.         else if (query_type == "HAS"s) {
  49.             int value = 0;
  50.             cin >> value;
  51.             if (container.Has(value)) {
  52.                 cout << "true"s << endl;
  53.             }
  54.             else {
  55.                 cout << "false"s << endl;
  56.             }
  57.         }
  58.         else if (query_type == "RAND"s) {
  59.             cout << container.GetRand() << endl;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement