fork download
  1. #include <iostream> // for std::cout and std::endl
  2. #include <cstdio> // for getchar()
  3. #include <memory> // for std::shared_ptr
  4. #include <deque> // for std::deque
  5. #include <algorithm> // for std::earse and std::remove_if
  6.  
  7. class A
  8. {
  9. private:
  10. int _i;
  11. double _d;
  12. public:
  13. A(int i, double d)
  14. {
  15. _i = i;
  16. _d = d;
  17. }
  18. int geti()const
  19. {
  20. return _i;
  21. }
  22. double getValueOnWhichToCheck()const
  23. {
  24. return _d;
  25. }
  26. };
  27.  
  28. typedef std::shared_ptr<A> A_shared_ptr;
  29. typedef std::deque<A_shared_ptr> list_type;
  30.  
  31. void PrintDeque(list_type & dq)
  32. {
  33. if (0 == dq.size())
  34. {
  35. std::cout << "Empty deque." << std::endl;
  36. }
  37. else
  38. {
  39. for (int i = 0 ; i < dq.size() ; ++i)
  40. {
  41. std::cout << i+1 << "\t" << dq[i] << std::endl;
  42. }
  43. }
  44. }
  45.  
  46. class B
  47. {
  48. public:
  49. double getThreshold() // Non constant for a reason as in real code it isn't
  50. {
  51. return 24.987; // comes from a calculation not needed here so I return a constant.
  52. }
  53. bool Predicate(const std::shared_ptr<A>& a)
  54. {
  55. return a->getValueOnWhichToCheck() >= getThreshold();
  56. }
  57. void DoStuff()
  58. {
  59. A_shared_ptr pT1 = std::make_shared<A>(A(2, -6.899987));
  60. A_shared_ptr pT2 = std::make_shared<A>(A(876, 889.878762));
  61. A_shared_ptr pT3 = std::make_shared<A>(A(-24, 48.98924));
  62. A_shared_ptr pT4 = std::make_shared<A>(A(78, -6654.98980));
  63. A_shared_ptr pT5 = std::make_shared<A>(A(6752, 3.141594209));
  64. list_type dq = {pT1,pT2,pT3,pT4,pT5};
  65. PrintDeque(dq);
  66. dq.erase(std::remove_if(dq.begin(), dq.end(), std::bind(&B::Predicate, this, std::placeholders::_1)), dq.end());
  67. PrintDeque(dq);
  68. }
  69. };
  70.  
  71. int main()
  72. {
  73. B * pB = new B();
  74. pB->DoStuff();
  75. getchar();
  76. }
Success #stdin #stdout 0s 3484KB
stdin
Standard input is empty
stdout
1	0x865ba2c
2	0x865ba4c
3	0x865ba6c
4	0x865ba8c
5	0x865baac
1	0x865ba2c
2	0x865ba8c
3	0x865baac