Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a list of elements separated by commas?

I know how to do this in other languages, but not in C++, which I am forced to use here.

I have a set of strings (keywords) that I'm printing to out as a list, and the strings need a comma between them, but not a trailing comma. In Java, for instance, I would use a StringBuilder and just delete the comma off the end after I've built my string. How can I do it in C++?

auto iter = keywords.begin(); for (iter; iter != keywords.end( ); iter++ ) {     out << *iter << ", "; } out << endl; 

I initially tried inserting the following block to do it (moving the comma printing here):

if (iter++ != keywords.end())     out << ", "; iter--; 
like image 846
quandrum Avatar asked Aug 16 '10 20:08

quandrum


People also ask

How do you print a comma-separated list in Python?

Use the join() Function to Convert a List to a Comma-Separated String in Python. The join() function combines the elements of an iterable and returns a string. We need to specify the character that will be used as the separator for the elements in the string.

How do I print a separated list?

When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.

How do you print the characters in a string separated by a comma?

You can use the Python string split() function to split a string (by a delimiter) into a list of strings. To split a string by comma in Python, pass the comma character "," as a delimiter to the split() function. It returns a list of strings resulting from splitting the original string on the occurrences of "," .


2 Answers

Use an infix_iterator:

// infix_iterator.h  //  // Lifted from Jerry Coffin's 's prefix_ostream_iterator  #if !defined(INFIX_ITERATOR_H_)  #define  INFIX_ITERATOR_H_  #include <ostream>  #include <iterator>  template <class T,            class charT=char,            class traits=std::char_traits<charT> >  class infix_ostream_iterator :      public std::iterator<std::output_iterator_tag,void,void,void,void>  {      std::basic_ostream<charT,traits> *os;      charT const* delimiter;      bool first_elem;  public:      typedef charT char_type;      typedef traits traits_type;      typedef std::basic_ostream<charT,traits> ostream_type;      infix_ostream_iterator(ostream_type& s)          : os(&s),delimiter(0), first_elem(true)      {}      infix_ostream_iterator(ostream_type& s, charT const *d)          : os(&s),delimiter(d), first_elem(true)      {}      infix_ostream_iterator<T,charT,traits>& operator=(T const &item)      {          // Here's the only real change from ostream_iterator:          // Normally, the '*os << item;' would come before the 'if'.          if (!first_elem && delimiter != 0)              *os << delimiter;          *os << item;          first_elem = false;          return *this;      }      infix_ostream_iterator<T,charT,traits> &operator*() {          return *this;      }      infix_ostream_iterator<T,charT,traits> &operator++() {          return *this;      }      infix_ostream_iterator<T,charT,traits> &operator++(int) {          return *this;      }  };      #endif  

Usage would be something like:

#include "infix_iterator.h"  // ... std::copy(keywords.begin(), keywords.end(), infix_iterator(out, ",")); 
like image 137
Jerry Coffin Avatar answered Sep 19 '22 04:09

Jerry Coffin


In an experimental C++17 ready compiler coming soon to you, you can use std::experimental::ostream_joiner:

#include <algorithm> #include <experimental/iterator> #include <iostream> #include <iterator>  int main() {     int i[] = {1, 2, 3, 4, 5};     std::copy(std::begin(i),               std::end(i),               std::experimental::make_ostream_joiner(std::cout, ", ")); } 

Live examples using GCC 6.0 SVN and Clang 3.9 SVN

like image 41
TemplateRex Avatar answered Sep 23 '22 04:09

TemplateRex