首页 > 解决方案 > 如何使用 setw 格式化我的 ostringstream 的输出?

问题描述

我一直在尝试使用 setw 方法从我的程序中格式化我的输出但不工作?这是我的输出,国家之间的巨大差距(例如:美国):

在此处输入图像描述

我的代码如下所示:

string itemToString(const Country& i) {
  ostringstream out;
  out.setf(ios::fixed);
  out.setf(ios::showpoint);
  out.precision(2);
  out << i.name
      << setw(20) << "|" << i.population
      << setw(10) << "|" << i.areaSQ
      << setw(10) << "|" << i.popDensitySQ
      << setw(10) << "|" << i.netMigration
      << setw(10) << "|" << i.GDP
      << setw(10) << "|" << i.literacy  
      << setw(10) << "|" << i.birthRate << "\n";
  return out.str();
}

我想看看我能做些什么来弥补美国等国家之间的巨大差距。

标签: c++formatout

解决方案


推荐阅读