Skip to main content
Tweaked spelling. Added link.
Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132

I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up here for peer review first, and see ifwhether there are clear improvements that can be made first.

This is (at least intended to be) pretty much a drop-in replacement for std::ostream_iteratorstd::ostream_iterator, the sole difference being that (at least in normal use) it only printprints out delimiters between items instead of after every item. Code using it looks something like:

I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up for peer review first, and see if there are clear improvements that can be made first.

This is (at least intended to be) pretty much a drop-in replacement for std::ostream_iterator, the sole difference being that (at least in normal use) it only print out delimiters between items instead of after every item. Code using it looks something like:

I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up here for peer review first, and see whether there are clear improvements that can be made first.

This is (at least intended to be) pretty much a drop-in replacement for std::ostream_iterator, the sole difference being that (at least in normal use) it only prints out delimiters between items instead of after every item. Code using it looks something like:

Added improved code.
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145

Edit: In case anybody cares, here's the new version incorporating input from both @Konrad and @Loki. Thanks to both of you.

// infix_iterator.h #if !defined(INFIX_ITERATOR_H_) #define INFIX_ITERATOR_H_ #include <ostream> #include <iterator> #include <string> 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; std::basic_string<charT> delimiter; std::basic_string<charT> real_delim; 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) {} infix_ostream_iterator(ostream_type &s, charT const *d) : os(&s), real_delim(d) {} infix_ostream_iterator<T, charT, traits> &operator=(T const &item) { *os << delimiter << item; delimiter = real_delim; 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 

Edit: In case anybody cares, here's the new version incorporating input from both @Konrad and @Loki. Thanks to both of you.

// infix_iterator.h #if !defined(INFIX_ITERATOR_H_) #define INFIX_ITERATOR_H_ #include <ostream> #include <iterator> #include <string> 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; std::basic_string<charT> delimiter; std::basic_string<charT> real_delim; 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) {} infix_ostream_iterator(ostream_type &s, charT const *d) : os(&s), real_delim(d) {} infix_ostream_iterator<T, charT, traits> &operator=(T const &item) { *os << delimiter << item; delimiter = real_delim; 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 
Tweeted twitter.com/#!/StackCodeReview/status/218608579823939585
edited tags; edited title
Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Please review this infix_iterator code

Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145
Loading