shushus

Спринт 13. Функции с длинным списком аргументов.

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