How can I remove the last comma from a loop in C++ in a simple way?

There is no need to if then else so much:

std::string delim = "";
for( auto&& item : vec )
{
   std::cout << delim << item;
   delim = ",";
}

No checking is needed for all cases, like the vector is empty or not.

If you accept an extra space in the beginning, just replace the string to char, and then the performance will be improved even more.


Don't remove the last comma. Instead insert commas before each entry except the first.