【问题标题】:How do I use iomanip's setw, setfill, and left/right? Setfill isn't stopping its output如何使用iomanip 的setw、setfill 和left/right? Setfill 没有停止它的输出
【发布时间】:2014-03-26 03:41:01
【问题描述】:

我试图让我的输出看起来像这样:

size       time1       time2
-------------------------------
10         4           8
100        48          16
1000       2937        922
10000      123011      3902
100000     22407380    830722

我知道我需要使用setw()setfill()left。但是,我的尝试不断给我不正确的输出。这是我的代码的一个示例:

std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n";
std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
run = 10;
for(int i = 0; i < 5; i++) {
    std::cout << run;
    run *= 10;
    std::cout << std::setw(20) << std::left << time1[i];
    std::cout << std::setw(20) << std::left << time2[i] << "\n";
}

这是输出:

size    time1    time2
------------------------------------------------------------
103-------------------13------------------
100171-----------------199-----------------
100013183---------------667-----------------
10000670130--------------8205----------------
10000014030798-------------1403079888---------

我尝试更改我使用setw()setfill()left 的顺序,但我现在只是盲目地飞行。我还搜索了 iomanip 教程。我正在按照指示进行操作——据我所知——但我仍然没有得到它。

如何阻止setfill() 溢出?我如何证明左对齐但使用setw() 阻止数字相互碰撞?

【问题讨论】:

    标签: c++ iomanip setw


    【解决方案1】:

    怎么样:

    std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n";
    std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
    run = 10;
    std::cout << std::setfill(' ');  //fill with spaces
    for(int i = 0; i < 5; i++) {
        std::cout << std::setw(20) << std::left << run;  // fill the run column
        run *= 10;
        std::cout << std::setw(20) << std::left << time1[i];
        std::cout << std::setw(20) << std::left << time2[i] << "\n";
    }
    

    【讨论】:

      【解决方案2】:

      sj0h 的回答很好,只是标题不太一致。为了解决这个问题,我以“left”和“setw”开始标题行,我还必须以“endl”而不是“\n”结尾。

        std::cout << std::left << std::setw(20) << "size" << std::setw(20) << "time" << std::setw(20) << "time2" << std::endl;
      
        std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
        run = 10;
        std::cout << std::setfill(' ');  //fill with spaces
        for(int i = 0; i < 10; i++) {
            std::cout << std::setw(20) << std::left << run;  // fill the run column
            run *= 10;
            std::cout << std::setw(20) << std::left << time1[i];
            std::cout << std::setw(20) << std::left << time2[i] << std::endl;
        }
      

      【讨论】:

        【解决方案3】:

        在 C++20 中,您将能够使用 std::format 来执行此操作:

          std::cout << std::format("{:20}{:20}{:20}\n", "size", "time", "time2");
          std::cout << std::format("{:-<60}\n", "");
          int run = 10;
          for(int i = 0; i < 5; i++) {
            std::cout << std::format("{:<20}{:<20}{:<20}\n", run, time[i], time2[i]);
            run *= 10;
          } 
        

        输出:

        size                time                time2               
        ------------------------------------------------------------
        10                  4                   8                   
        100                 48                  16                  
        1000                2937                922                 
        10000               123011              3902                
        100000              22407380            830722              
        

        同时你可以使用the {fmt} librarystd::format是基于。 {fmt} 还提供了print 函数,使这更容易和更高效(godbolt):

          fmt::print("{:20}{:20}{:20}\n", "size", "time", "time2");
          fmt::print("{:-<60}\n", "");
          int run = 10;
          for(int i = 0; i < 5; i++) {
            fmt::print("{:<20}{:<20}{:<20}\n", run, time[i], time2[i]);
            run *= 10;
          }
        

        免责声明:我是 {fmt} 和 C++20 std::format 的作者。

        【讨论】:

          猜你喜欢
          • 2016-04-16
          • 2017-07-20
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 2021-02-05
          • 2013-07-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多