【问题标题】:C++ how to right justify multiple pieces of dataC ++如何正确证明多条数据
【发布时间】:2017-04-04 01:23:36
【问题描述】:

所以我必须向std::cout 发送一列数据,我还必须在数据周围显示一些字符,例如:

    Plot Points
   (0,0), (2,3) 
(1,10), (12,14)

我必须右对齐列标题中"Points" 中字母"s" 下的最后一个右括号。

我将数据输出如下:

cout << right << setw(12) << "(" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ")";

但是我看到的所有例子似乎都表明,右和setw似乎只影响我发送给cout的下一条数据,所以在这种情况下,只有"("

有没有办法将所有这些字符和变量组合在一起,以使它们在输出列中全部对齐?

我只是在学习 C++,所以希望有一些我还没有学过的简单解决方案?

【问题讨论】:

    标签: c++ justify


    【解决方案1】:

    有没有办法将所有这些字符和变量组合在一起,以使它们在输出列中全部对齐?

    是的,你可以使用一个小辅助函数来构建一个字符串:

    std::string parentized_pair(int x, int y) {
        std::ostringstream oss;
        oss << "(" << x "," << y << ")";
        return oss.str();
    }
    

    并在最终输出中使用那个:

    cout << right << setw(12) << parentized_pair(x1,y1) 
         << right << setw(12) << parentized_pair(x2,y2);
    

    【讨论】:

      【解决方案2】:

      一种可能性是使用字符串流格式化组成字段的项目,然后写出该字符串右对齐。

      【讨论】:

        【解决方案3】:

        关于adjustfield,你可以如下使用它。

        cout.setf(ios::right, ios::adjustfield);
        

        关于widthsetw 很遗憾,您必须在每次输出之前定义它。

        参考下面的例子:

        #include <iostream>
        
        using namespace std;
        
        int main () {
            cout<<"Current fill character: " << cout.fill() <<", code: " <<(int)cout.fill()<<endl;
            cout<<"Current field width : " << cout.width() <<endl;
            cout<<1<<2<<3<<endl;
            // changing width;
            cout.width(3);
            cout<<"Field width after change : " << cout.width() <<endl;
            cout<<1<<2<<3<<endl;
            cout<<"Width after output : " << cout.width() <<endl;
            // changing width and fill
            cout.width(3);
            cout.fill('_');
            cout<<"Current fill character: " << cout.fill() <<", code: " <<(int)cout.fill()<<endl;
            cout<<1<<2<<3<<endl<<endl;
        
            cout<<"Setting width before each write:\n";
            cout<<"adjustfield - not set/default:\n";
            for(unsigned i=1; i <= 3; ++i) {
                cout.width(3);
                cout<<i;
            }
            cout<<endl;
        
            cout<<"adjustfield - left:\n";
            cout.setf(ios::left, ios::adjustfield);
            for(unsigned i=1; i <= 3; ++i) {
                cout.width(3);
                cout<<i;
            }
            cout<<endl;
        
            cout<<"adjustfield - internal:\n";
            cout.setf(ios::internal, ios::adjustfield);
            for(unsigned i=1; i <= 3; ++i) {
                cout.width(3);
                cout<<i;
            }
            cout<<endl;
        
            cout<<"adjustfield - right:\n";
            cout.setf(ios::right, ios::adjustfield);
            for(unsigned i=1; i <= 3; ++i) {
                cout.width(3);
                cout<<i;
            }
            cout<<endl;
            return 0;
        }
        /*
        Console output:
        
        Current fill character:  , code: 32
        Current field width : 0
        123
        Field width after change : 3
        123
        Width after output : 0
        Current fill character: _, code: 95
        123
        
        Setting width before each write:
        adjustfield - not set/default:
        __1__2__3
        adjustfield - left:
        1__2__3__
        adjustfield - internal:
        __1__2__3
        adjustfield - right:
        __1__2__3
        */
        

        如果您可以提供有关您使用的数据结构的更多信息,或者您可以重新设计您的数据结构,您可以在 πάντα ῥεῖ 的答案中使用这个想法,例如您定义一个 Point 类/结构并覆盖 &lt;&lt; 方法以进行更好的格式化。

        【讨论】:

        • 感谢您的建议..现在尝试消化它们..我想我必须保持简单,因为这是入门课程...
        【解决方案4】:

        C++如何对多条数据进行右对齐?

        快速回答:每件作品按顺序和分开。


        熟悉I/O manipulators 及其属性是一个好的开始。在您应用setw 的情况下,它会设置一个格式标志,并且该标志的值不是持久的(“粘性”),但它会重置为其初始值。要更深入地分析为什么 setw 在使用后似乎会重置,see here.

        有没有办法将所有这些字符和变量组合在一起,以使它们在输出列中全部对齐?

        现在,针对您的具体问题,您可以尝试将所有数据插入到 std::stringstream 对象中,然后只应用一次所需的属性,如其他答案中所指出的那样。

        我只是在学习 C++,所以希望有一些我还没有学过的简单解决方案?

        最简单的解决方案是编写一个辅助函数来执行所需的任务,没有更简单的(对我而言)C++ 语言工具来执行你想要的。


        【讨论】:

          猜你喜欢
          • 2020-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多