【问题标题】:C++ cout aligning output numbersC++ cout 对齐输出数字
【发布时间】:2018-03-15 09:05:16
【问题描述】:
#include <iostream>

using namespace std;

int main()
{
    cout << -0.152454345 << " " << -0.7545 << endl;
    cout << 0.15243 << " " << 0.9154878774 << endl;
}

输出:

-0.152454 -0.7545
0.15243 0.915488

我希望输出如下所示:

-0.152454 -0.754500
 0.152430  0.915488

我的解决方案:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout << fixed << setprecision(6) << setw(9) << setfill(' ') << -0.152454345 << " ";
    cout << fixed << setprecision(6) << setw(9) << setfill(' ') << -0.7545 << endl;
    cout << fixed << setprecision(6) << setw(9) << setfill(' ') << 0.15243 << " ";
    cout << fixed << setprecision(6) << setw(9) << setfill(' ') << 0.9154878774 << endl;
}

输出很好,但代码很糟糕。可以做什么? 这是我的代码https://ideone.com/6MKd31

【问题讨论】:

  • 使用 stdio.h。 iostream 不利于格式化。
  • 你可以为它写一个函数吗?

标签: c++ formatting cout


【解决方案1】:

指定输出格式总是很糟糕。无论如何,您可以省略重复在输入/输出中保留的流修饰符,而只重复那些瞬态 (setw):

// change state of the stream
cout << fixed << setprecision(6) << setfill(' ');
// output data
cout << setw(9) << -0.152454345  << " ";
cout << setw(9) << -0.7545       << endl;
cout << setw(9) << 0.15243       << " ";
cout << setw(9) <<  0.9154878774 << endl;

【讨论】:

  • @Aconcagua 你为什么这么说?
【解决方案2】:

这可能不是你想要的,但我想我还是会把它扔掉,因为它很简单。

如果您可以容忍非负数的前导+,那么您可以使用

std::cout << std::showpos << /*ToDo - the rest of your output here*/

至少一切都排好了,用最少的努力。

【讨论】:

    【解决方案3】:

    曾经遇到过类似的流问题(但更复杂),您可以使用单独的格式化程序对象来避免代码重复:

    class F
    {
        double v;
    public:
        F(double v) : v(v) { };
        friend ostream& operator<<(ostream& s, F f)
        {
            s << setw(9) << v;
        }
    };
    

    用法:

    std::cout << fixed << setprecision(6) << setfill(' '); // retained, see Jean's answer
    std::cout << F(12.10) << ' ' << F(-10.12) << std::endl;
    

    根据您的需求以及您使用它的频率,这可能是矫枉过正 - 自己决定...

    【讨论】:

      【解决方案4】:

      你可以为它写一个函数:

      std::ostream& format_out(std::ostream& os, int precision, int width, char fill, double value)
      {
          return os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(fill)
              << value;
      }
      
      int main()
      {
          format_out(std::cout, 6, 9, ' ', -0.152454345) << '\n';
          format_out(std::cout, 6, 9, ' ', -0.7545) << '\n';
          format_out(std::cout, 6, 9, ' ', 0.15243) << '\n';
          format_out(std::cout, 6, 9, ' ', 0.9154878774) << '\n';
      }
      

      【讨论】:

        【解决方案5】:

        有点死灵贴,但它可能对将来的某人仍然有用。我们可以使用 lamda 表达式(或等效函数)来减少格式化的痛苦。

        #include <iostream>
        #include <iomanip>
        
        int main()
        {
          using namespace std;
        
          auto format = [](std::ostream &os) -> std::ostream& {
            return os << fixed << setprecision(6) << setw(9) << setfill(' ');
          };
        
        
          cout << format << -0.152454345 << " " << format << -0.7545      << "\n";
          cout << format << 0.15243      << " " << format << 0.9154878774 << endl;
        }
        

        输出:

        -0.152454 -0.754500
         0.152430  0.915488
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-17
          • 2011-01-29
          • 1970-01-01
          相关资源
          最近更新 更多