Possible Duplicate:
Printing lists with commas C++
Is there a nice way to print the elements of a vector joined with ", "? I am hoping for a simple and clean one-liner, or similar. A quality which I would not attribute to the version I came up so far:
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
int main()
{
std::vector<double> v{0, 1, 1, 2, 3, 5, 8, 13};
if (v.size() > 1) {
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<int>(std::cout, ", "));
}
if (v.size() > 0) {
std::cout << *(v.end() - 1) << "\n";
}
}
Output
0, 1, 1, 2, 3, 5, 8, 13