SHARE
TWEET

Untitled

a guest May 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <random>
  3. #include <chrono>
  4. #include <functional>
  5. #include <vector>
  6. #include <memory>
  7. using namespace std;
  8.  
  9. class WordBag
  10. {
  11.     default_random_engine gene;
  12.     function<int(void)> roll;
  13.     void upD();
  14.     vector<unique_ptr<string>> vec;
  15. public:
  16.     WordBag();
  17.     void add(unique_ptr<string> ptrToadd);
  18.     unique_ptr<string> take();
  19. };
  20.  
  21. WordBag::WordBag()
  22. {
  23.     unsigned int seed = chrono::system_clock::now().time_since_epoch().count();
  24.     gene=default_random_engine(seed);
  25. }
  26. void WordBag::upD()
  27. {
  28.     uniform_int_distribution<int> distributionn(0,vec.size()-1);
  29.     roll=bind(distributionn,gene);
  30. }
  31. void WordBag::add(unique_ptr<string> ptrToadd)
  32. {
  33.     vec.push_back(move(ptrToadd));
  34.     upD();
  35. }
  36. unique_ptr<string> WordBag::take()
  37. {
  38.     if(vec.empty())
  39.     {
  40.        throw out_of_range("NAni nani na");
  41.     }
  42.     else
  43.     {
  44.      size_t index=roll();
  45.      unique_ptr<string> toReturn(move(vec[index]));
  46.      auto it=vec.begin()+index;
  47.      vec.erase(it);
  48.      upD();
  49.      return toReturn;
  50.     }
  51.  
  52. }
  53.  
  54. int main()
  55. {
  56.     WordBag wb;
  57.     wb.add(make_unique<string>("ala"));
  58.     wb.add(make_unique<string>("ma"));
  59.     wb.add(make_unique<string>("kota"));
  60.  
  61.     try {
  62.  
  63.         cout<<*(wb.take())<<endl;
  64.         cout<<*(wb.take())<<endl;
  65.         cout<<*(wb.take())<<endl;
  66.         cout<<*(wb.take())<<endl;
  67.  
  68.     } catch (out_of_range) {
  69.         cout<<"nie dla psa"<<endl;
  70.     }
  71.  
  72.  
  73.     return 0;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top