C++ 可以 setw 和 setfill 填充字符串的末尾吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12062318/
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-27 15:50:30  来源:igfitidea点击:

C++ can setw and setfill pad the end of a string?

c++formattingsetw

提问by Dan

Is there a way to make setwand setfillpad the end of a string instead of the front?

有没有办法制作setwsetfill填充字符串的末尾而不是前面?

I have a situation where I'm printing something like this.

我有一种情况,我正在打印这样的东西。

 CONSTANT TEXT variablesizeName1 .....:number1 

 CONSTANT TEXT varsizeName2 ..........:number2

I want to add a variable amount of '.'to the end of

我想'.'在末尾添加一个可变数量的

"CONSTANT TEXT variablesizeName#"so I can make ":number#"line up on the screen.

"CONSTANT TEXT variablesizeName#"所以我可以":number#"在屏幕上排队。

Note: I have an array of "variablesizeName#"so I know the widest case.

注意:我有一个数组,"variablesizeName#"所以我知道最广泛的情况。

Or

或者

Should I do it manually by setting setwlike this

我应该通过setw这样的设置手动进行吗

for( int x= 0; x < ARRAYSIZE; x++)
{
string temp = string("CONSTANT TEXT ")+variabletext[x];
cout <<  temp;
cout << setw(MAXWIDTH - temp.length) << setfill('.') <<":";
cout << Number<<"\n";
}

I guess this would do the job but it feels kind of clunky.

我想这可以完成这项工作,但感觉有点笨重。

Ideas?

想法?

回答by jrok

You can use manipulators std::left, std::right, and std::internalto choose where the fill characters go.

您可以使用操纵器std::leftstd::rightstd::internal来选择填充字符的位置。

For your specific case, something like this could do:

对于您的具体情况,可以这样做:

#include <iostream>
#include <iomanip>
#include <string>

const char* C_TEXT = "Constant text ";
const size_t MAXWIDTH = 10;

void print(const std::string& var_text, int num)
{
    std::cout << C_TEXT
              // align output to left, fill goes to right
              << std::left << std::setw(MAXWIDTH) << std::setfill('.')
              << var_text << ": " << num << '\n';
}

int main()
{
    print("1234567890", 42);
    print("12345", 101);
}

Output:

输出:

Constant text 1234567890: 42
Constant text 12345.....: 101

EDIT: As mentioned in the link, std::internalworks only with integer, floating point and monetary output. For example with negative integers, it'll insert fill characters between negative sign and left-most digit.

编辑:如链接中所述,std::internal仅适用于整数、浮点数和货币输出。例如对于负整数,它会在负号和最左边的数字之间插入填充字符。

This:

这个:

int32_t i = -1;
std::cout << std::internal
          << std::setfill('0')
          << std::setw(11)  // max 10 digits + negative sign
          << i << '\n';
i = -123;
std::cout << std::internal
          << std::setfill('0')
          << std::setw(11)
          << i;

will output

会输出

-0000000001
-0000000123

回答by Wug

Something like:

就像是:

cout << left << setw(MAXWIDTH) << setfill('.') << temp << ':' << Number << endl;

Produces something like:

产生类似的东西:

derp..........................:234
herpderpborp..................:12345678

回答by Chad

#include <iostream>
#include <iomanip>

int main()
{
   std::cout
      << std::setiosflags(std::ios::left) // left align this section
      << std::setw(30)                    // within a max of 30 characters
      << std::setfill('.')                // fill with .
      << "Hello World!"
      << "\n";
}

//Output:
Hello World!..................