我正在使用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