恢复std :: cout的状态后,操作它 [英] Restore the state of std::cout after manipulating it

查看:163
本文介绍了恢复std :: cout的状态后,操作它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的代码:

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 ..)?

My question is if there is any way to 'restore' the state of cout to its original one after returning from the function? (Somewhat like std::boolalpha and std::noboolalpha..) ?

谢谢,
Iyer

Thanks, Iyer

推荐答案

Googling给了我这个:

Googling gave me this:

  ios::fmtflags f( cout.flags() );

  //Your code here...

  cout.flags( f );

你可能想把它放在函数的头部和末尾。

You'd probably want to put that at the head and end of your function.

这篇关于恢复std :: cout的状态后,操作它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆