fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <typename Iter, typename FEach, typename FJoin>
  5. void for_each_and_join(Iter iter, Iter end, FEach&& feach, FJoin&& fjoin)
  6. {
  7. if (iter == end)
  8. return;
  9. while (true)
  10. {
  11. feach(*iter);
  12. Iter curr = iter;
  13. if (++iter==end)
  14. return;
  15. fjoin(*curr, *iter);
  16. }
  17. }
  18.  
  19. int main() {
  20. std::vector<int> values = { 1, 2, 3, 4, 5 };
  21. for_each_and_join(values.begin(), values.end()
  22. , [](auto v) { std::cout << v; }
  23. , [](auto, auto) { std::cout << ","; }
  24. );
  25. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1,2,3,4,5