好的,所以这绝对不是最干净/最短的解决方案,但这是一种方法:
namespace custom
{
struct sep
{
sep(const std::string & s)
:separator(s)
{
}
std::string separator;
};
}
typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
typedef CoutType& (*StandardEndLine)(CoutType&);
class SeparatorWrap
{
public:
SeparatorWrap(std::ostream & _ofs, const custom::sep & s)
: ofs(_ofs)
, separator(s)
{}
template <class W>
SeparatorWrap& operator << (W && w)
{
ofs << separator.separator << w;
return (*this);
}
ostream & operator << (const StandardEndLine &)
{
//writing std::endl will remove the separator
return ofs << std::endl;
}
protected:
std::ostream & ofs;
custom::sep separator;
};
class SeparatorWrapFirst
{
public:
SeparatorWrapFirst(std::ostream & _ofs, const custom::sep & s)
: ofs(_ofs)
, separator(s)
{}
template <class W>
SeparatorWrap operator << (W && w)
{
ofs << w;
return SeparatorWrap(ofs, separator);
}
ostream & operator << (const StandardEndLine &)
{
//writing std::endl will remove the separator
return ofs << std::endl;
}
protected:
std::ostream & ofs;
custom::sep separator;
};
SeparatorWrapFirst operator << (std::ostream & ofs,const custom::sep & s)
{
return SeparatorWrapFirst(ofs, s);
}
int main()
{
std::cout << custom::sep(", ") << 1 << "two" << 3 << std::endl;
}
下面是它的工作原理:
std::cout << custom::sep(", ")
返回一类类型SeparatorWrapFirst
(使用 global operator <<
),用于在输出中写入一个不带分隔符的值。这是因为如果您有一个元素,则不需要编写分隔符。
在调用第一个运算符<<
from后SeparatorWrapFirst
,返回该类SeparatorWrap
并使用分隔符打印。这适用于多个值。
编辑:
所以从评论(@gexicide)看来,我可以把自定义操纵器放在里面std::cout
。这可以让您执行以下操作:
std::cout << custom::sep(", ");
std::cout << 1 << "two" << 3 << std::endl;
上面的第一个解决方案不适用于此。