C++ 如何使用 iomanip 的 setw、setfill 和 left/right?Setfill 不会停止输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22651041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:09:11  来源:igfitidea点击:

How do I use iomanip's setw, setfill, and left/right? Setfill isn't stopping its output

c++iomanipsetw

提问by user3448821

I'm trying to get my output to look like this:

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

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

And I know I need to use setw(), setfill(), and left. However, my attempts keep giving me incorrect output. Here is one example of my code:

我知道我需要使用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";
}

And here's the output:

这是输出:

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

I've tried changing the order that I'm using setw(), setfill(), and left, but I'm just flying blind right now. I've also searched iomanip tutorials. I'm following the directions--as far as I can tell--but I'm still not getting it.

我试过,我使用改变顺序setw()setfill()left,但我只是盲目飞行现在。我还搜索了 iomanip 教程。我正在遵循指示——据我所知——但我仍然没有明白。

How do I stop the setfill()from running over? How do I justify left but use setw()to stop the numbers from running into each other?

我如何阻止setfill()跑过?我如何证明左对齐但用于setw()阻止数字相互碰撞?

采纳答案by sj0h

How about:

怎么样:

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";
}

回答by oneofthesixbillion

sj0h's answer is great except the titles don't quite line up. To fix it I started the title line with "left" and "setw", I also had to end with "endl" instead of "\n".

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;
  }