C ++ cout自动分隔符 [英] C++ cout auto separator

查看:170
本文介绍了C ++ cout自动分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如:

  std :: cout< 2<< 3<< 33<< 45 < std :: endl; 

输出

  233345 

,我想输出

  2 3 33 45 

知道,很容易:

  std :: cout< 2<< < 3<< < 33<< < 45 < std :: endl; 

但我想知道是否有自动化的方法,例如:

  std :: cout< set_some_separator()< 2<< 3<< 33<< 45 < std :: endl;任何人都知道这样的东西是可能的?


=

h2_lin>解决方案

好吧,我受到了打击。



编辑:好,在阅读Nim的回答后,我的 p>

  #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 ),_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;
}

输出:

  1,2,3 


I am wondering if there is way that std::cout automatically will insert some predefined value between printed sequences.

For example:

std::cout << 2 << 3 << 33 << 45 << std::endl;

outputs

233345

and I would like it to output

2 3 33 45

and I know, that it's easy to:

std::cout << 2 << " " << 3 <<  " " << 33 <<  " " << 45 << std::endl;

But I'am wondering if there is a way to automate this, such as:

std::cout << set_some_separator(" ") << 2 << 3 << 33 << 45 << std::endl;

Anyone aware of something like this being possible?

解决方案

Well, I got beaten to it. I'll post this anyway.

Edit : well, after reading Nim's answer, mine does achieve the exact syntax OP wished for.

#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;
}

Output :

1, 2, 3

这篇关于C ++ cout自动分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆