![](/img/trans.png)
[英]Does setting a bit collide with concurrent sets of other bits on the same word?
[英]why bits sets are decreased after setting flag manipulator?
代码
#include<iostream>
#include<bitset>
int main()
{
std::ios_base::fmtflags flag=std::ios::showpos;
std::cout<<std::cout.flags()<<"\n";
std::cout<<flag<<"\n";
std::cout.flags(flag);
std::cout<<std::cout.flags()<<"\n";
std::cout<<59.20<<"\n";
std::cout<<std::bitset<16>(4098)<<"\n";
std::cout<<std::bitset<16>(2048)<<"\n";
}
输出
4098
2048
+2048
+59.2
0001000000000010
0000100000000000
看到这里我设置了showpos
标志操纵器,然后我由cout.flags()
返回的值(当前格式设置)减少了位设置。
代码
#include<iostream>
#include<bitset>
int main()
{
std::cout<<std::cout.flags()<<"\n";
std::cout<<std::showpos;
std::cout<<std::cout.flags()<<"\n";
std::cout<<59.20<<"\n";
std::cout<<std::bitset<16>(4098)<<"\n";
std::cout<<std::bitset<16>(6146)<<"\n";
}
输出
4098
+6146
+59.2
0001000000000010
0001100000000010
这里作为预期的位设置值增加了1
因为我添加了一个标志操纵器。
为什么当我在cout.flags()
函数的帮助下设置标志位集减少而不是增加?
flags
函数采用新值,而不是要添加的标志。 您正在做的是完全用showpos
替换标志。
如果你想用flags
来做,你需要自己添加标志:
std::cout.flags(std::cout.flags() | std::ios::showpos);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.