本文介绍了C ++-如何重设输出流操纵器标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一行代码将填充值设置为输出中的-"字符,但需要将setfill标志重置为其默认的空白字符.我该怎么办?
I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that?
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
我认为这可能有效:
cout.unsetf(ios::manipulatorname) // Howerver I dont see a manipulator called setfill
我走错了路吗?
推荐答案
看看 Boost.IO_State_Savers ,为iostream的标志提供RAII风格的范围保护.
Have a look at the Boost.IO_State_Savers, providing RAII-style scope guards for the flags of an iostream.
示例:
#include <boost/io/ios_state.hpp>
{
boost::io::ios_all_saver guard(cout); // Saves current flags and format
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
// dtor of guard here restores flags and formats
}
库中还包含更多专门的警戒(仅用于填充,宽度或精度等.有关详细信息,请参阅文档.
More specialized guards (for only fill, or width, or precision, etc... are also in the library. See the docs for details.
这篇关于C ++-如何重设输出流操纵器标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!