Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

c++ prinintg out lists

This topic is 2586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you handle printing out a list of values in C++, where you want some separator, say a comma, between values but not at the end. The obvious method is to create a copy of the iterator, increment it, and test it against the end iterator, but this is unnecessarily complex and requires boilerplate code. Is there anything like Python's string join?

Share this post


Link to post
Share on other sites
Advertisement
boost string algorithms contains a string join algorithm. If you do it manually, instead of not printing the separator if the iterator is the last one, you can not print the separator if the iterator is the first one, which doesn't require any extra copies. Ex:

template <typename InputIterator>
void print_list(InputIterator first, InputIterator last) {
if (first == last) return;
std::cout << *first++;
while (first != last) {
std::cout << ", " << *first++;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!