basic_base.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. /* basic usage of boost::base_collection */
  9. #include <algorithm>
  10. #include <boost/poly_collection/base_collection.hpp>
  11. #include <random>
  12. #include "rolegame.hpp"
  13. int main()
  14. {
  15. //[basic_base_1
  16. //= #include <boost/poly_collection/base_collection.hpp>
  17. //= ...
  18. //=
  19. boost::base_collection<sprite> c;
  20. std::mt19937 gen{92748}; // some arbitrary random seed
  21. std::discrete_distribution<> rnd{{1,1,1}};
  22. for(int i=0;i<8;++i){ // assign each type with 1/3 probability
  23. switch(rnd(gen)){
  24. case 0: c.insert(warrior{i});break;
  25. case 1: c.insert(juggernaut{i});break;
  26. case 2: c.insert(goblin{i});break;
  27. }
  28. }
  29. //]
  30. auto render=[&](){
  31. //[basic_base_2
  32. const char* comma="";
  33. for(const sprite& s:c){
  34. std::cout<<comma;
  35. s.render(std::cout);
  36. comma=",";
  37. }
  38. std::cout<<"\n";
  39. //]
  40. };
  41. render();
  42. //[basic_base_3
  43. c.insert(goblin{8});
  44. //]
  45. render();
  46. //[basic_base_4
  47. // find element with id==7 and remove it
  48. auto it=std::find_if(c.begin(),c.end(),[](const sprite& s){return s.id==7;});
  49. c.erase(it);
  50. //]
  51. render();
  52. }