fork download
  1. #include <algorithm>
  2. #include <functional> // C++11 or C++14 std::mem_fn
  3. #include <vector>
  4. #include <iostream>
  5.  
  6.  
  7. #define OVERLOAD_SET(func)\
  8.   static struct { template<typename... Args> \
  9.   auto operator()(Args&&...args) -> decltype(func(std::forward<Args>(args)...)) const { \
  10.   return func(std::forward<Args>(args)...); \
  11.   } \
  12.   }
  13.  
  14. #define APPLY_ON_WRAPPER(func) \
  15. template<class Containter, typename... Args> \
  16. auto func ##_on(Containter& c, Args... args) \
  17. -> decltype(apply_on(c, func, args...)) \
  18. { \
  19. return apply_on(c, func, args...); \
  20. }
  21.  
  22. namespace stx {
  23. OVERLOAD_SET(std::all_of) all_of;
  24. OVERLOAD_SET(std::any_of) any_of;
  25. OVERLOAD_SET(std::none_of) none_of;
  26. OVERLOAD_SET(std::for_each) for_each;
  27. OVERLOAD_SET(std::find) find;
  28. OVERLOAD_SET(std::find_if) find_if;
  29. OVERLOAD_SET(std::find_if_not) find_if_not;
  30. OVERLOAD_SET(std::find_end) find_end;
  31. OVERLOAD_SET(std::find_first_of)find_first_of;
  32. OVERLOAD_SET(std::adjacent_find)adjacent_find;
  33. OVERLOAD_SET(std::count) count;
  34. OVERLOAD_SET(std::count_if) count_if;
  35. OVERLOAD_SET(std::mismatch) mismatch;
  36. OVERLOAD_SET(std::equal) equal;
  37. OVERLOAD_SET(std::is_permutation)is_permutation;
  38. OVERLOAD_SET(std::search) search;
  39. OVERLOAD_SET(std::search_n) search_n;
  40. OVERLOAD_SET(std::copy) copy;
  41. OVERLOAD_SET(std::copy_n) copy_n;
  42. OVERLOAD_SET(std::copy_if) copy_if;
  43. OVERLOAD_SET(std::copy_backward)copy_backward;
  44. OVERLOAD_SET(std::move) move;
  45. OVERLOAD_SET(std::move_backward)move_backward;
  46. OVERLOAD_SET(std::swap) swap;
  47. OVERLOAD_SET(std::swap_ranges) swap_ranges;
  48. OVERLOAD_SET(std::iter_swap) iter_swap;
  49. OVERLOAD_SET(std::transform) transform;
  50. OVERLOAD_SET(std::replace) replace;
  51. OVERLOAD_SET(std::replace_if) replace_if;
  52. OVERLOAD_SET(std::replace_copy) replace_copy;
  53. OVERLOAD_SET(std::replace_copy_if)replace_copy_if;
  54. OVERLOAD_SET(std::fill) fill;
  55. OVERLOAD_SET(std::fill_n) fill_n;
  56. OVERLOAD_SET(std::generate) generate;
  57. OVERLOAD_SET(std::generate_n) generate_n;
  58. OVERLOAD_SET(std::remove) remove;
  59. OVERLOAD_SET(std::remove_if) remove_if;
  60. OVERLOAD_SET(std::remove_copy) remove_copy;
  61. OVERLOAD_SET(std::remove_copy_if)remove_copy_if;
  62. OVERLOAD_SET(std::unique) unique;
  63. OVERLOAD_SET(std::unique_copy) unique_copy;
  64. OVERLOAD_SET(std::reverse) reverse;
  65. OVERLOAD_SET(std::reverse_copy) reverse_copy;
  66. OVERLOAD_SET(std::rotate) rotate;
  67. OVERLOAD_SET(std::rotate_copy) rotate_copy;
  68. OVERLOAD_SET(std::random_shuffle)random_shuffle;
  69. OVERLOAD_SET(std::shuffle) shuffle;
  70. OVERLOAD_SET(std::is_partitioned)is_partitioned;
  71. OVERLOAD_SET(std::partition) partition;
  72. OVERLOAD_SET(std::stable_partition)stable_partition;
  73. OVERLOAD_SET(std::partition_copy)partition_copy;
  74. OVERLOAD_SET(std::partition_point)partition_point;
  75. OVERLOAD_SET(std::sort) sort;
  76. OVERLOAD_SET(std::stable_sort) stable_sort;
  77. OVERLOAD_SET(std::partial_sort) partial_sort;
  78. OVERLOAD_SET(std::partial_sort_copy)partial_sort_copy;
  79. OVERLOAD_SET(std::is_sorted) is_sorted;
  80. OVERLOAD_SET(std::is_sorted_until)is_sorted_until;
  81. OVERLOAD_SET(std::nth_element) nth_element;
  82. OVERLOAD_SET(std::lower_bound) lower_bound;
  83. OVERLOAD_SET(std::upper_bound) upper_bound;
  84. OVERLOAD_SET(std::equal_range) equal_range;
  85. OVERLOAD_SET(std::binary_search)binary_search;
  86. OVERLOAD_SET(std::merge) merge;
  87. OVERLOAD_SET(std::inplace_merge)inplace_merge;
  88. OVERLOAD_SET(std::includes) includes;
  89. OVERLOAD_SET(std::set_union) set_union;
  90. OVERLOAD_SET(std::set_intersection)set_intersection;
  91. OVERLOAD_SET(std::set_difference)set_difference;
  92. OVERLOAD_SET(std::set_symmetric_difference)set_symmetric_difference;
  93. OVERLOAD_SET(std::push_heap) push_heap;
  94. OVERLOAD_SET(std::pop_heap) pop_heap;
  95. OVERLOAD_SET(std::make_heap) make_heap;
  96. OVERLOAD_SET(std::sort_heap) sort_heap;
  97. OVERLOAD_SET(std::is_heap) is_heap;
  98. OVERLOAD_SET(std::is_heap_until)is_heap_until;
  99. OVERLOAD_SET(std::min) min;
  100. OVERLOAD_SET(std::max) max;
  101. OVERLOAD_SET(std::minmax) minmax;
  102. OVERLOAD_SET(std::min_element) min_element;
  103. OVERLOAD_SET(std::max_element) max_element;
  104. OVERLOAD_SET(std::minmax_element)minmax_element;
  105. OVERLOAD_SET(std::lexicographical_compare)lexicographical_compare;
  106. OVERLOAD_SET(std::next_permutation) next_permutation;
  107. OVERLOAD_SET(std::prev_permutation) prev_permutation;
  108.  
  109. template<class Containter, typename Func, typename... Args>
  110. auto apply_on(Containter& c, Func func, Args... args)
  111. -> decltype(func(c.begin(), c.end(), args...))
  112. {
  113. return func(c.begin(), c.end(), args...);
  114. }
  115.  
  116. APPLY_ON_WRAPPER(all_of);
  117. APPLY_ON_WRAPPER(any_of);
  118. APPLY_ON_WRAPPER(none_of);
  119. APPLY_ON_WRAPPER(for_each);
  120. APPLY_ON_WRAPPER(find);
  121. APPLY_ON_WRAPPER(find_if);
  122. APPLY_ON_WRAPPER(find_if_not);
  123. APPLY_ON_WRAPPER(find_end);
  124. APPLY_ON_WRAPPER(find_first_of);
  125. APPLY_ON_WRAPPER(adjacent_find);
  126. APPLY_ON_WRAPPER(count);
  127. APPLY_ON_WRAPPER(count_if);
  128. APPLY_ON_WRAPPER(mismatch);
  129. APPLY_ON_WRAPPER(equal);
  130. APPLY_ON_WRAPPER(is_permutation);
  131. APPLY_ON_WRAPPER(search);
  132. APPLY_ON_WRAPPER(search_n);
  133. APPLY_ON_WRAPPER(copy);
  134. APPLY_ON_WRAPPER(copy_n);
  135. APPLY_ON_WRAPPER(copy_if);
  136. APPLY_ON_WRAPPER(copy_backward);
  137. APPLY_ON_WRAPPER(move);
  138. APPLY_ON_WRAPPER(move_backward);
  139. APPLY_ON_WRAPPER(swap);
  140. APPLY_ON_WRAPPER(swap_ranges);
  141. APPLY_ON_WRAPPER(iter_swap);
  142. APPLY_ON_WRAPPER(transform);
  143. APPLY_ON_WRAPPER(replace);
  144. APPLY_ON_WRAPPER(replace_if);
  145. APPLY_ON_WRAPPER(replace_copy);
  146. APPLY_ON_WRAPPER(replace_copy_if);
  147. APPLY_ON_WRAPPER(fill);
  148. APPLY_ON_WRAPPER(fill_n);
  149. APPLY_ON_WRAPPER(generate);
  150. APPLY_ON_WRAPPER(generate_n);
  151. APPLY_ON_WRAPPER(remove);
  152. APPLY_ON_WRAPPER(remove_if);
  153. APPLY_ON_WRAPPER(remove_copy);
  154. APPLY_ON_WRAPPER(remove_copy_if);
  155. APPLY_ON_WRAPPER(unique);
  156. APPLY_ON_WRAPPER(unique_copy);
  157. APPLY_ON_WRAPPER(reverse);
  158. APPLY_ON_WRAPPER(reverse_copy);
  159. APPLY_ON_WRAPPER(rotate);
  160. APPLY_ON_WRAPPER(rotate_copy);
  161. APPLY_ON_WRAPPER(random_shuffle);
  162. APPLY_ON_WRAPPER(shuffle);
  163. APPLY_ON_WRAPPER(is_partitioned);
  164. APPLY_ON_WRAPPER(partition);
  165. APPLY_ON_WRAPPER(stable_partition);
  166. APPLY_ON_WRAPPER(partition_copy);
  167. APPLY_ON_WRAPPER(partition_point);
  168. APPLY_ON_WRAPPER(sort);
  169. APPLY_ON_WRAPPER(stable_sort);
  170. APPLY_ON_WRAPPER(partial_sort);
  171. APPLY_ON_WRAPPER(partial_sort_copy);
  172. APPLY_ON_WRAPPER(is_sorted);
  173. APPLY_ON_WRAPPER(is_sorted_until);
  174. APPLY_ON_WRAPPER(nth_element);
  175. APPLY_ON_WRAPPER(lower_bound);
  176. APPLY_ON_WRAPPER(upper_bound);
  177. APPLY_ON_WRAPPER(equal_range);
  178. APPLY_ON_WRAPPER(binary_search);
  179. APPLY_ON_WRAPPER(merge);
  180. APPLY_ON_WRAPPER(inplace_merge);
  181. APPLY_ON_WRAPPER(includes);
  182. APPLY_ON_WRAPPER(set_union);
  183. APPLY_ON_WRAPPER(set_intersection);
  184. APPLY_ON_WRAPPER(set_difference);
  185. APPLY_ON_WRAPPER(set_symmetric_difference);
  186. APPLY_ON_WRAPPER(push_heap);
  187. APPLY_ON_WRAPPER(pop_heap);
  188. APPLY_ON_WRAPPER(make_heap);
  189. APPLY_ON_WRAPPER(sort_heap);
  190. APPLY_ON_WRAPPER(is_heap);
  191. APPLY_ON_WRAPPER(is_heap_until);
  192. APPLY_ON_WRAPPER(min);
  193. APPLY_ON_WRAPPER(max);
  194. APPLY_ON_WRAPPER(minmax);
  195. APPLY_ON_WRAPPER(min_element);
  196. APPLY_ON_WRAPPER(max_element);
  197. APPLY_ON_WRAPPER(minmax_element);
  198. APPLY_ON_WRAPPER(lexicographical_compare);
  199. APPLY_ON_WRAPPER(next_permutation);
  200. APPLY_ON_WRAPPER(prev_permutation);
  201. }
  202.  
  203. #undef APPLY_ON_WRAPPER
  204. #undef OVERLOAD_SET
  205.  
  206. struct Bob {
  207. void stuff()
  208. {
  209. std::cout << "stuffing..." << std::endl;
  210. }
  211. };
  212.  
  213. int main()
  214. {
  215. std::vector<Bob> bobs(3);
  216. stx::apply_on(bobs, stx::for_each, std::mem_fn(&Bob::stuff));
  217.  
  218. stx::for_each_on(bobs, std::mem_fn(&Bob::stuff));
  219.  
  220. std::vector<int> ints{3,4,1,6};
  221. auto min_el = stx::apply_on(ints, stx::min_element);
  222. std::cout << *min_el << std::endl;
  223.  
  224. auto max_el = stx::max_element_on(ints);
  225. std::cout << *max_el << std::endl;
  226.  
  227. return 0;
  228. }
  229.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
stuffing...
stuffing...
stuffing...
stuffing...
stuffing...
stuffing...
1
6