假设我有这样的代码:
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
..)?
谢谢。
原文由 UltraInstinct 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要
#include <iostream>
或#include <ios>
然后在需要时:您可以将它们放在函数的开头和结尾,或者查看有关如何将其与 RAII 一起使用的 答案。