[英]Formatting C++ setw
I am working on formatting and I am not getting the correct formatting. 我正在格式化,但没有得到正确的格式。
Here is my code and here is what I am getting for the output 这是我的代码,这是我得到的输出
cout << fixed << setprecision(2) << setw(3) << right << source << setw(20)
<<right <<vec[source].cityname
<< setw(15) << left<< sink << setw(15) << left <<vec[sink].cityname
<< setw(10) << left << vdist[ij] << "miles\n";
I am getting this: 我得到这个:
30 Novosibirsk_RU13 Tokyo_JP 10497.68 miles
And I would like to be getting this: 我想得到这个:
30 Novosibirsk_RU 13 Tokyo_JP 10497.67 miles
How can I get that space between the name and the number? 如何获得姓名和电话号码之间的空格?
I've been working on this for a while but I just can't get the right formatting 我已经为此工作了一段时间,但我无法获得正确的格式
You have used the std::right
IO manipulator wrongly in your cout
statement. 您在
cout
语句中错误地使用了std::right
IO机械手。 To achieve the output as shown in your sample you want vec[source].cityname
and vec[sink].cityname
obviously left aligned: 要获得示例中所示的输出,您希望
vec[source].cityname
和vec[sink].cityname
显然保持对齐:
cout << fixed << setprecision(2) << setw(3) << right << source <<
setw(20) << left <<vec[source].cityname <<
// ^^^^
setw(3) << left<< sink <<
// ^
setw(15) << left <<vec[sink].cityname <<
setw(10) << left << vdist[ij] << "miles\n";
Also note, if you use setw(n)
and n
is smaller than the length of the next field ( string
) variable, it won't be cutoff, but shifts the whole output to the right for the number of additional characters. 还要注意,如果使用
setw(n)
且n
小于下一个字段( string
)变量的长度,则不会将其截断,而是将整个输出向右移动其他字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.