C++ cout自动分隔符

Fer*_*eak 46 c++

我想知道是否有std::cout自动在打印序列之间插入一些预定义值的方法.

例如:

std::cout << 2 << 3 << 33 << 45 << std::endl;
Run Code Online (Sandbox Code Playgroud)

输出

233345
Run Code Online (Sandbox Code Playgroud)

我希望它输出

2 3 33 45
Run Code Online (Sandbox Code Playgroud)

我知道,这很容易:

std::cout << 2 << " " << 3 <<  " " << 33 <<  " " << 45 << std::endl;
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有办法实现自动化,例如:

std::cout << set_some_separator(" ") << 2 << 3 << 33 << 45 << std::endl;
Run Code Online (Sandbox Code Playgroud)

任何人都知道这样的事情可能吗?

Que*_*tin 48

好吧,我被打败了.无论如何我会发布这个.

编辑:好吧,在阅读了Nim的答案后,我的确实现了OP所希望的确切语法.

#include <iostream>
#include <algorithm>

struct with_separator {
    with_separator(std::string sep)
    : sep(std::move(sep)) {}

    std::string sep;
};

struct separated_stream {
    separated_stream(std::ostream &stream, std::string sep)
    : _stream(stream), _sep(std::move(sep)), _first(true) {}

    template <class Rhs>
    separated_stream &operator << (Rhs &&rhs) {
        if(_first)
            _first = false;
        else
            _stream << _sep;

        _stream << std::forward<Rhs>(rhs);
        return *this;
    }

    separated_stream &operator << (std::ostream &(*manip)(std::ostream&)) {
        manip(_stream);
        return *this;
    }

    private:
    std::ostream &_stream;
    std::string _sep;
    bool _first;
};

separated_stream operator << (std::ostream &stream, with_separator wsep) {
    return separated_stream(stream, std::move(wsep.sep));
}

int main()
{
    std::cout << with_separator(", ") << 1 << 2 << 3 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

1, 2, 3
Run Code Online (Sandbox Code Playgroud)


Nim*_*Nim 14

简单的答案是不,但是,你可以自己动手......

#include <iostream>
#include <sstream>

using namespace std;

struct set_some_separator{
    set_some_separator(const char* sep) : _sep(sep)
    { };

    template <typename T>
    set_some_separator& operator<<(const T& v)
    {
        _str << v << _sep;
        return *this;
    }

    friend
    ostream& operator<<(ostream& os, const set_some_separator& s)
    { return os << s._str.str(); }

    const char* _sep;
    ostringstream _str;
};

int main()
{
    cout << (set_some_separator(" ") << 2 << 3 << 33 << 45) << endl;
}
Run Code Online (Sandbox Code Playgroud)

奥凯的格式cout稍有不同,嘿,嗬...


Pet*_*ker 8

不太一样,但是:

#include <array>
#include <iostream>
#include <iterator>

int main() {
    std::array<int, 3> data = { 1, 2, 3 };
    std::ostream_iterator<int> out(std::cout, " ");
    std::copy(data.begin(), data.end(), out);
    std::cout << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ork 5

使用 ostream_iterator 怎么样

int main()
{
     std::vector<int> data {2,3,33,45};
     std::copy(std::begin(data), std::end(data),
               std::ostream_iterator(std::cout, " "));
     std::cout << "\n";
}
Run Code Online (Sandbox Code Playgroud)


use*_*370 5

带有逗号运算符的 C++17 折叠表达式可以创建漂亮的单行代码:

[](auto &&...xs){ ((std::cout << xs << ',') , ...); }(2,3,33,45);
Run Code Online (Sandbox Code Playgroud)