Elegantly exclude part of an operation for the last iteration in a loop
If you are just doing a single-shot printing of a container, just print the first element and then print delim-value pairs.  This reduces the iterator requirements to being equality comparable, dereferenceable, and incrementable.

if (!vec.empty()) {  // invert to return immediately if this is a function
    auto first = vec.cbegin();
    std::cout &lt;&lt; *first;

    while (vec.cend() != ++first) {
        std::cout &lt;&lt; ", " &lt;&lt; *first;
    }
}


There is also a stateful approach that reduces the operation strength from branching on whether to print the delimiter to a pointer assignment.

auto actual_delim = ", ";
auto delim = "";

for (const auto&amp; elem : vec) {
    std::cout &lt;&lt; delim &lt;&lt; elem;
    delim = actual_delim;
}


See infix_iterator where this stateful approach is used.