簡體   English   中英

為什么設置標志操縱器后位集減少?

[英]why bits sets are decreased after setting flag manipulator?

1

代碼

#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()返回的值(當前格式設置)減少了位設置。

2

代碼

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM