Guest User

Iterating over vector and calling functions

a guest
Feb 26th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. class Small
  2. {
  3. public:
  4. void foo();
  5. void bar(int x);
  6. // and many more functions
  7. };
  8.  
  9. class Big
  10. {
  11. public:
  12. void foo()
  13. {
  14. for (size_t i = 0; i < VectorOfSmalls.size(); i++)
  15. VectorOfSmalls[i]->foo();
  16. }
  17. void bar(int x)
  18. {
  19. for (size_t i = 0; i < VectorOfSmalls.size(); i++)
  20. VectorOfSmalls[i]->bar(x);
  21. }
  22. // and many more functions
  23. private:
  24. vector<Small*> VectorOfSmalls;
  25. };
  26.  
  27. void Big::call_command(const string & command)
  28. {
  29. for (size_t i = 0; i < VectorOfSmalls.size(); i++)
  30. {
  31. if (command == "foo")
  32. VectorOfSmalls[i]->foo();
  33. else if (command == "bar")
  34. VectorOfSmalls[i]->bar();
  35. }
  36. }
  37. void Big::foo()
  38. {
  39. call_command("foo");
  40. }
  41.  
  42. void foo() {
  43. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::foo));
  44. }
  45.  
  46. void bar() {
  47. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::bar));
  48. }
  49.  
  50. class Big {
  51. public:
  52. enum Command {
  53. DO_FOO,
  54. DO_BAR
  55. };
  56.  
  57. void doit(Command cmd) {
  58. switch(cmd) {
  59. case DO_FOO:
  60. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::foo));
  61. break;
  62. case DO_BAR:
  63. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(&Small::bar));
  64. break;
  65. }
  66. };
  67.  
  68. class Big {
  69. public:
  70. template<void (Small::*fn)()>
  71. void doit() {
  72. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(fn));
  73. }
  74. };
  75.  
  76. Big b;
  77. b.doit<&Small::foo>();
  78. b.doit<&Small::bar>();
  79.  
  80. #include <algorithm>
  81. #include <functional>
  82. #include <iostream>
  83. #include <vector>
  84.  
  85. class Small {
  86. public:
  87. void foo() { std::cout << "foo" << std::endl; }
  88. void bar(int x) { std::cout << "bar" << std::endl; }
  89. };
  90.  
  91.  
  92. class Big {
  93. public:
  94. template<void (Small::*fn)()>
  95. void doit() {
  96. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::mem_fun(fn));
  97. }
  98.  
  99. template<class T, void (Small::*fn)(T)>
  100. void doit(T x) {
  101. std::for_each(VectorOfSmalls.begin(), VectorOfSmalls.end(), std::bind2nd(std::mem_fun(fn), x));
  102. }
  103. public:
  104. std::vector<Small *> VectorOfSmalls;
  105. };
  106.  
  107. int main() {
  108. Big b;
  109. b.VectorOfSmalls.push_back(new Small);
  110. b.VectorOfSmalls.push_back(new Small);
  111.  
  112. b.doit<&Small::foo>();
  113. b.doit<int, &Small::bar>(5);
  114. }
  115.  
  116. void Big::call_command(const boost::function<void (Small*)>& f)
  117. {
  118. for (size_t i = 0; i < VectorOfSmalls.size(); i++)
  119. {
  120. f(VectorOfSmalls[i]);
  121. }
  122. }
  123.  
  124. int main()
  125. {
  126. Big b;
  127. b.call_command(boost::bind(&Small::foo, _1));
  128. b.call_command(boost::bind(&Small::bar, _1, 5));
  129. }
Add Comment
Please, Sign In to add comment