考慮以下代碼:輸入流的std :: setfill和std :: setw?
int xx;
std::cin >> std::setfill('0') >> std::setw(4) >> xx;
當發送12
標準輸入我期待的xx
值是1200
和發送12345
時,我希望它是1234
。
但是,看起來std::setfill
和std::setw
沒有效果,我分別得到12
和12345
。
這是一個錯誤還是根據標準?有沒有一種很好的方法來獲得預期的功能?
另請注意,當我將xx
的類型更改爲std::string
時std::setw
生效,而std::setfill
仍然不生效。
我的編譯器是gcc-7.0.1
。
也許我錯過了。因爲什麼時候在* input *流操作上支持'std :: setfill'?我知道'std :: setw'是,但現在'std :: setfill'也是? – WhozCraig
不行,這是惡意代碼。 –
我不認爲輸入流支持'std :: setfill'。 http://en.cppreference.com/w/cpp/io/manip/setfill – Shravan40