我正在使用ANSI颜色代码在Unix控制台中格式化我的输出。处理iostream操纵器和ANSI控制台颜色代码
const auto& getCode(Color mColor)
{
static std::map<Color, std::string> codes;
// ...
return codes[mColor]
}
cout << getCode(Color::Red) << "red text";
当使用操纵器如std::setw
或std::left
,然而,结果由颜色代码受影响,因为它是一串字符。
我应该如何处理这个问题?有没有办法让流操纵器忽略颜色代码?
你的意思是将它们通过改变但不影响什么吗? 'getCode'的定义是什么? – Deduplicator 2014-09-23 13:17:44
你的函数是否返回int或char?请与我们分享getCode原型。 – 2014-09-23 13:18:52
getCode是一个输出流操作符的结构吗?也许你有兴趣设置和恢复iostream标志:http://stackoverflow.com/questions/4217704/roll-back-or-undo-any-manipulators-applied-to-a-stream-without-knowing-what-th – Sven 2014-09-23 13:32:27