偶然的你
使用std :: for_each和lambda可以做一些有趣的事情。#include #include int main(){ int array[] = {1,2,3,4}; std::for_each(std::begin(array), std::end(array), [&std::cout, sep=' '](int x) mutable { out << sep << x; sep=','; });}请参阅我写的一堂小课的这个问题。这不会打印结尾的逗号。同样,如果我们假设C ++ 14将继续为我们提供基于范围的等效算法,如下所示:namespace std { // I am assuming something like this in the C++14 standard // I have no idea if this is correct but it should be trivial to write if it does not appear. template void copy(C const& container, I outputIter) {copy(begin(container), end(container), outputIter);}}using POI = PrefexOutputIterator; int main(){ int array[] = {1,2,3,4}; std::copy(array, POI(std::cout, ",")); // ",".join(map(str,array)) // closer}