csv_wrap(std::cout, ",") << 1 << 2 << 3;
で
1,2,3
と表示できると幸せと感じるので以下のクラスを使っています:
class csv_wrap {
std::ostream &ost;
std::string comma, endl;
bool is_first;
bool is_flush;
public:
csv_wrap(std::ostream &ost, std::string comma, bool is_flush = true,
std::string endl = "\n")
: ost(ost), comma(comma), endl(endl), is_first(true), is_flush(is_flush) {
}
~csv_wrap() {
if (is_flush) {
ost << endl << std::flush;
} else {
ost << endl;
}
}
template <typename T> csv_wrap &operator<<(const T &val) {
if (is_first) {
ost << val;
is_first = false;
} else {
ost << comma << val;
}
return *this;
}
csv_wrap &operator<<(std::ostream &(*pf)(std::ostream &)) {
pf(ost);
return *this;
}
};
別の方法を御存じの方は教えて下さい。