用C ++正确对齐输出流 [英] Right Justifying output stream in C++
本文介绍了用C ++正确对齐输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用C ++。我给了一个10位数字的字符串(字符数组),其中可以包含3个破折号(最多可以包含13个字符)。
I'm working in C++. I'm given a 10 digit string (char array) that may or may not have 3 dashes in it (making it up to 13 characters). Is there a built in way with the stream to right justify it?
我将如何打印右对齐的流?是否有内置的函数/方式来执行此操作,还是我需要在字符数组的开头填充3个空格?
How would I go about printing to the stream right justified? Is there a built in function/way to do this, or do I need to pad 3 spaces into the beginning of the character array?
我正在处理ostream以具体来说,不确定是否重要。
I'm dealing with ostream to be specific, not sure if that matters.
推荐答案
您需要使用 std :: setw
与 std :: right
。
You need to use std::setw
in conjunction with std::right
.
#include <iostream>
#include <iomanip>
int main(void)
{
std::cout << std::right << std::setw(13) << "foobar" << std::endl;
return 0;
}
这篇关于用C ++正确对齐输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文