相关疑难解决方法(0)

哪个iomanip操纵器"粘"?

我最近遇到了一个问题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()
  • 最后有一个在线参考,清楚地记录了这种行为?我的供应商文档(MS Visual Studio 2005)似乎没有清楚地显示这一点.

c++ c++-faq

134
推荐指数
3
解决办法
3万
查看次数

真的,"固定"I/O操纵器的反义词是什么?

这可能是这个问题的重复,但我觉得它实际上没有得到正确回答.注意:

#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在设置之后回到第一个版本的行为?

c++ floating-point cout iomanip

4
推荐指数
1
解决办法
1456
查看次数

标签 统计

c++ ×2

c++-faq ×1

cout ×1

floating-point ×1

iomanip ×1