fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. struct A
  7. {
  8. A(std::initializer_list<int> il) : _values(il) {}
  9.  
  10. std::vector<int>& get_values() { return _values; }
  11.  
  12. private:
  13. std::vector<int> _values;
  14. };
  15.  
  16. struct B
  17. {
  18. B(A* a) : _a(a) {}
  19.  
  20. void apply_filter(std::function<bool(int)> filter)
  21. {
  22. std::vector<int>& v = _a->get_values();
  23.  
  24. v.erase(std::remove_if(v.begin(), v.end(), filter), v.end());
  25. }
  26.  
  27. private:
  28. A* _a;
  29. };
  30.  
  31. void print(const std::vector<int>& v)
  32. {
  33. std::cout << "{ ";
  34. for (auto e : v)
  35. std::cout << e << ' ';
  36. std::cout << "}\n";
  37. }
  38.  
  39. int main()
  40. {
  41. A a({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  42. print(a.get_values());
  43.  
  44. B b(&a);
  45. b.apply_filter([](int val) { return val % 2 == 0; });
  46.  
  47. print(a.get_values());
  48. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
{ 1 2 3 4 5 6 7 8 9 10 }
{ 1 3 5 7 9 }