Also put iterator into dhandy namespace. I don't remember why I left it out, and it's probably a sign I should have not done so.
56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
// see http://stackoverflow.com/questions/3496982/printing-lists-with-commas-c/3497021#3497021
|
|
// infix_iterator.h
|
|
//
|
|
// Lifted from Jerry Coffin's 's prefix_ostream_iterator
|
|
#if !defined(INFIX_ITERATOR_H_)
|
|
#define INFIX_ITERATOR_H_
|
|
#include <ostream>
|
|
|
|
namespace dhandy {
|
|
template <class T,
|
|
class charT=char,
|
|
class traits=std::char_traits<charT> >
|
|
class infix_ostream_iterator
|
|
{
|
|
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;
|
|
|
|
using iterator_category = std::output_iterator_tag;
|
|
using value_type = void;
|
|
using difference_type = void;
|
|
using pointer = void;
|
|
using reference = void;
|
|
|
|
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;
|
|
}
|
|
};
|
|
} //namespace dhandy
|
|
#endif
|