3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

csv_wrap(std::cout, ",") << 1 << 2 << 3; で1,2,3と出力する

Posted at
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;
  }
};

別の方法を御存じの方は教えて下さい。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?