One thing that I have never really properly gotten a handle on is creating comma separated lists of items from a collection. The issue is that the last entry will print a comma: 1, 2, 3, for example. In the past, I would do all kinds of weird code gymnastics to ensure that trailing comma doesn't print, but today I found a wonderful entry on stackoverflow (a rare occurrence for me).
The trick is that you always print the comma, but it's only set as a comma after the first entry. Brilliant!
Commit this one to memory.
The trick is that you always print the comma, but it's only set as a comma after the first entry. Brilliant!
string sep = ""; for(auto i : items) { cout << sep << i; sep = ", "; }
Commit this one to memory.
0 Comments On This Entry
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)