![](/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.