【问题标题】:Cpp/C++ Output allingment in one line from right AND leftCpp/C++输出从右到左一行
【发布时间】:2022-01-05 22:31:25
【问题描述】:

我需要将右侧的整数和左侧的字符串写入一行并让它们正确排列(查看代码下方的输出)。

基本上我只需要一种仅使用 iostreamiomanip 来编写表格的方法,然后将 ints 的右对齐更改为字符串的左对齐。

其他提示也值得赞赏:)

#include <iostream>
#include <iomanip>
using namespace std;

class foo
{
public:
    int i;
    std::string s;
    int j;
    foo(int i1,std::string s1,int j1) : i(i1), s(s1),j(j1) {};
};

int main()
{
    foo f1(1, "abc",50);
    foo f2(100, "abcde",60);

    cout << resetiosflags(ios::adjustfield);
    cout << setiosflags(ios::right);
    cout << setw(6) << "i" << setw(15) << "s" << setw(15) << "j"<<endl;

    cout << setw(8) << f1.i << setw(15) 
        << resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f1.s <<setw(5)
        << resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f1.j << endl;

    cout << setw(8) << f2.i << setw(15) 
        << resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f2.s <<setw(5
        << resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f2.j << endl;

    /*i              s              j
          1abc                         50
        100abcde                       60*/

    return 0;
}

这是输出:

        i              s              j
          1abc                         50
        100abcde                       60

这就是我需要的:

        i              s              j
          1             abc            50
        100             abcde          60

【问题讨论】:

    标签: c++ io iostream cout iomanip


    【解决方案1】:

    在同一行中使用leftright 不是问题。看起来您遇到的问题是在第一个值之后不允许有空格。看起来setw(5) 可能是为了那个,但由于它之后没有打印任何内容,所以没有效果。我用 7 来匹配用于字符串宽度的总共 15 个。

    也许这样的事情会起作用?可能最好将幻数提取为常数,以便您可以在一个地方轻松调整所有它们。您也可以将其包装在 operator&lt;&lt; 中,以便将所有格式化代码包含在一个位置。

    第一行标题向左偏移一个对我来说看起来很奇怪,但它与您的示例相匹配并且在必要时很容易调整。

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class foo
    {
    public:
        int i;
        std::string s;
        int j;
        foo(int i1, std::string s1, int j1) : i(i1), s(s1), j(j1)
        {};
    };
    
    int main()
    {
        foo f1(1, "abc", 50);
        foo f2(100, "abcde", 60);
    
        cout 
            << right << setw(7) << "i" << setw(7) << ' '
            << left << setw(15) << "s"
            << right << "j"
            << endl;
    
        cout 
            << right << setw(8) <<  f1.i << setw(7) << ' '
            << left << setw(15) << f1.s
            << right << f1.j
            << endl;
    
        cout 
            << right << setw(8) << f2.i << setw(7) << ' '
            << left << setw(15) << f2.s
            << right << f2.j
            << endl;
    
        return 0;
    }
    

    输出:

          i       s              j
           1       abc            50
         100       abcde          60
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2021-08-08
      相关资源
      最近更新 更多