我最近遇到了一个问题stringstream,因为我错误地认为std::setw()这会影响每次插入的字符串流,直到我明确地更改它.但是,插入后总是未设置.
// With timestruct with value of 'Oct 7 9:04 AM'
std::stringstream ss;
ss.fill('0'); ss.setf(ios::right, ios::adjustfield);
ss << setw(2) << timestruct.tm_mday;
ss << timestruct.tm_hour;
ss << timestruct.tm_min;
std::string filingTime = ss.str(); // BAD: '0794'
Run Code Online (Sandbox Code Playgroud)
所以,我有很多问题:
setw()这样?std::ios_base::width()和std::setw()?这可能是这个问题的重复,但我觉得它实际上没有得到正确回答.注意:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
float p = 1.00;
cout << showpoint << setprecision(3) << p << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出: 1.00
现在,如果我们将该行更改为:
cout << fixed << showpoint << setprecision(3) << p << endl;
Run Code Online (Sandbox Code Playgroud)
我们得到: 1.000
如果我们使用固定的"相反",我们会得到完全不同的东西:
cout << scientific << showpoint << setprecision(3) << p << endl;
Run Code Online (Sandbox Code Playgroud)
输出: 1.000e+00
如何fixed在设置之后回到第一个版本的行为?