I am trying to use the backspace control character \'\\b\' to erase trailing commas at the end of line. Although it works in cases where there is no other outpu
\'\\b\'
Or, if you're fond of C+11 hacks:
adjacent_difference(a.begin(), a.end(), ostream_iterator(std::cout), [](int x, int)->int { return std::cout << ",", x; });