假设我有这样的代码:
void printHex(std::ostream& x){
x<<std::hex<<123;
}
..
int main(){
std::cout<<100; // prints 100 base 10
printHex(std::cout); //prints 123 in hex
std::cout<<73; //problem! prints 73 in hex..
}
我的问题是,从函数返回后,是否有任何方法可以将 cout
的状态“恢复”到其原始状态? (有点像 std::boolalpha
和 std::noboolalpha
..)?
谢谢。
最佳答案
您需要 #include <iostream>
或 #include <ios>
然后在需要时:
std::ios_base::fmtflags f( cout.flags() );
//Your code here...
cout.flags( f );
您可以将它们放在函数的开头和结尾,或查看 this answer关于如何将其与 RAII 一起使用.
关于c++ - 操作后恢复 std::cout 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273330/