#include <iostream>
#include <vector>
template <typename Iter, typename FEach, typename FJoin>
void for_each_and_join(Iter iter, Iter end, FEach&& feach, FJoin&& fjoin)
{
if (iter == end)
return;
while (true)
{
feach(*iter);
Iter curr = iter;
if (++iter==end)
return;
fjoin(*curr, *iter);
}
}
int main() {
std::vector<int> values = { 1, 2, 3, 4, 5 };
for_each_and_join(values.begin(), values.end()
, [](auto v) { std::cout << v; }
, [](auto, auto) { std::cout << ","; }
);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdGVtcGxhdGUgPHR5cGVuYW1lIEl0ZXIsIHR5cGVuYW1lIEZFYWNoLCB0eXBlbmFtZSBGSm9pbj4Kdm9pZCBmb3JfZWFjaF9hbmRfam9pbihJdGVyIGl0ZXIsIEl0ZXIgZW5kLCBGRWFjaCYmIGZlYWNoLCBGSm9pbiYmIGZqb2luKQp7CiAgICBpZiAoaXRlciA9PSBlbmQpCiAgICAgICAgIHJldHVybjsKICAgIHdoaWxlICh0cnVlKQogICAgewogICAgICAgIGZlYWNoKCppdGVyKTsKICAgICAgICBJdGVyIGN1cnIgPSBpdGVyOwogICAgICAgIGlmICgrK2l0ZXI9PWVuZCkKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIGZqb2luKCpjdXJyLCAqaXRlcik7CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgc3RkOjp2ZWN0b3I8aW50PiB2YWx1ZXMgPSB7IDEsIDIsIDMsIDQsIDUgfTsKICAgIGZvcl9lYWNoX2FuZF9qb2luKHZhbHVlcy5iZWdpbigpLCB2YWx1ZXMuZW5kKCkKICAgICwgIFtdKGF1dG8gdikgeyBzdGQ6OmNvdXQgPDwgdjsgfQogICAgLCAgW10oYXV0bywgYXV0bykgeyBzdGQ6OmNvdXQgPDwgIiwiOyB9CiAgICApOwp9