Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <chrono>
- #include <functional>
- #include <vector>
- #include <memory>
- using namespace std;
- class WordBag
- {
- default_random_engine gene;
- function<int(void)> roll;
- void upD();
- vector<unique_ptr<string>> vec;
- public:
- WordBag();
- void add(unique_ptr<string> ptrToadd);
- unique_ptr<string> take();
- };
- WordBag::WordBag()
- {
- unsigned int seed = chrono::system_clock::now().time_since_epoch().count();
- gene=default_random_engine(seed);
- }
- void WordBag::upD()
- {
- uniform_int_distribution<int> distributionn(0,vec.size()-1);
- roll=bind(distributionn,gene);
- }
- void WordBag::add(unique_ptr<string> ptrToadd)
- {
- vec.push_back(move(ptrToadd));
- upD();
- }
- unique_ptr<string> WordBag::take()
- {
- if(vec.empty())
- {
- throw out_of_range("NAni nani na");
- }
- else
- {
- size_t index=roll();
- unique_ptr<string> toReturn(move(vec[index]));
- auto it=vec.begin()+index;
- vec.erase(it);
- upD();
- return toReturn;
- }
- }
- int main()
- {
- WordBag wb;
- wb.add(make_unique<string>("ala"));
- wb.add(make_unique<string>("ma"));
- wb.add(make_unique<string>("kota"));
- try {
- cout<<*(wb.take())<<endl;
- cout<<*(wb.take())<<endl;
- cout<<*(wb.take())<<endl;
- cout<<*(wb.take())<<endl;
- } catch (out_of_range) {
- cout<<"nie dla psa"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement