C ++可以setw和setfill填充字符串的结尾? [英] C++ can setw and setfill pad the end of a string?

查看:268
本文介绍了C ++可以setw和setfill填充字符串的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 setw setfill 填充字符串的结尾而不是前面? / p>

我有这样一个打印的情况。

  CONSTANT TEXT variablesizeName1 .....:number1 

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

我想添加一个'。'到$ /

CONSTANT TEXT variablesizeName#所以我可以使:number#排队屏幕。
$ b

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



手动设置 setw like this


$ b

for(int x = 0; x< ARRAYSIZE; x ++)
{
string temp = string(CONSTANT TEXT)+ variabletext [x];
cout<<温度;
cout<< (MAXWIDTH - temp.length)<< setfill('。')<<:;
cout<<数<< \\\
;



$ b $ p
$ b

我想这样做可以做,但是感觉有点笨拙。 p>

想法?

解决方案

您可以使用操纵器 std :: left std :: right code>和 std :: internal 来选择填充字符的位置。



对于您的具体情况,类似这样的事情可以做到这一点:

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

const char * C_TEXT =常量文本;
const size_t MAXWIDTH = 10;

void print(const std :: string& var_text,int num)
{
std :: cout<< C_TEXT
//将输出对齐到左边,填充到右边
<< std :: left<< std :: setw(MAXWIDTH)<< std :: setfill('。')
<< var_text<< :<< num<< \\\
;
}

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



$ b $ p $输出:

 常量文本1234567890:42 
常量文本12345 .....:101



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



这个:

  int32_t i = -1; 
std :: cout<< std :: internal
<< std :: setfill('0')
<< std :: setw(11)//最多10位数字+负号
<<我 \\\
;
i = -123;
std :: cout<< std :: internal
<< std :: setfill('0')
<< std :: setw(11)
<<一世;

会输出

  -0000000001 
-0000000123


Is there a way to make setw and setfill pad the end of a string instead of the front?

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.

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

Or

Should I do it manually by setting setw like this

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?

解决方案

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

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::internal works 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.

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

这篇关于C ++可以setw和setfill填充字符串的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆