[英]How does std::setw work with string output?
I am trying to use set width setw
for string output to an output file, however, I am not able to make it work. 我正在尝试将set width setw
用于将字符串输出到输出文件,但是,我无法使其工作。 I have with me the following example. 我有以下示例。
// setw example
#include <iostream>
#include <iomanip>
#include <fstream>
int main () {
std::ofstream output_file;
output_file.open("file.txt");
output_file << "first" <<std::setw(5)<< "second"<< std::endl;
output_file.close();
return 0;
}
Edit: With the above lines I expected to have many spaces between first
and second
, something like first second
编辑:通过以上几行,我希望first
和second
之间有很多空格,例如first second
I hardly see any spaces, the output just comes like firstsecond
I think I missed the working of setw()
我几乎看不到任何空格,输出就像firstsecond
一样,我想我错过了setw()
的工作
Note: For integers, it works fine just: 注意:对于整数,它仅能正常工作:
output_file << 1 <<std::setw(5)<< 2 << std::endl;
What I am doing wrong??. 我做错了什么?
I suspect your understanding of std::setw
is simply not correct. 我怀疑您对std::setw
理解根本不正确。 I think you need something more along the lines of a combination of: 我认为您需要以下组合的更多功能:
std::setw
for setting field width std::setw
用于设置字段宽度 std::setfill
for setting the fill character std::setfill
用于设置填充字符 std::left
, std::right
, std::internal
for setting the write position within the specified field width . std::left
, std::right
, std::internal
用于在指定字段宽度内设置写入位置。 What is happening in your code: 您的代码中发生了什么:
std::setw(5)
to establish a field width of five characters. 使用std::setw(5)
建立五个字符的字段宽度。 "first"
to the stream, which is five characters long, so the established field width is completely consumed. 将"first"
发送到流,该流的长度为5个字符,因此已建立的字段宽度将被完全消耗。 No additional filling takes place. 不会进行其他填充。 "second"
to the stream, which is six characters long, so again, the entire field width is consumed (and in-fact breached). 将"second"
发送到长度为六个字符的流中,因此再次消耗了整个字段宽度(实际上违反了)。 Again, no filling takes place 同样,没有填充发生 If you're intent is to have something like this (with column numbers above to show positions): 如果您有这样的想法(上面的列号显示位置):
col: 0123456789012345678901234567890123456789
first second third fourth
Notice how each word starts on an even multiple of 10 boundary. 注意每个单词如何以10的偶数倍开始。 One way to do that is by using : 一种方法是使用:
std::left
(so the fill, if any goes on the right to achieve the desired width). 输出位置std::left
(因此,填充(如果有的话)在右侧获得所需的宽度)。 This the default for strings, but it never hurts to be sure. 这是字符串的默认设置,但是可以肯定地说。 std::setfill(' ')
. std::setfill(' ')
填充字符。 Again, the default. 再次,默认。 std::setw(10)
Why such a large number? 一个字段宽度std::setw(10)
为什么这么大? See below 见下文 Example 例
#include <iostream>
#include <iomanip>
int main ()
{
std::cout << std::left << std::setfill(' ')
<< std::setw(10) << "first"
<< std::setw(10) << "second"
<< std::setw(10) << "third"
<< std::setw(10) << "fourth" << '\n';
return 0;
}
Output (column numbers added) 输出 (添加列号)
0123456789012345678901234567890123456789
first second third fourth
So what happens if we change the output location to std::right
? 那么,如果我们将输出位置更改为std::right
什么? Well, with the identical program, changing only the first line to : 好吧,使用相同的程序,只需将第一行更改为:
std::cout << std::right << std::setfill(' ')
we get 我们得到
0123456789012345678901234567890123456789
first second third fourth
Finally, one constructive way of seeing where the fill characters are being applied is by simply changing the fill char to something visible (ie. something besides a space). 最后,一种查看填充字符应用位置的方法是通过简单地将填充字符更改为可见的字符(即除空格之外的字符)来构造的。 The last two examples output, changing the fill char to std::setfill('*')
produces the following output: 最后两个示例输出,将fill char更改为std::setfill('*')
产生以下输出:
First 第一
first*****second****third*****fourth****
Second 第二
*****first****second*****third****fourth
Notice in both cases, since none of the individual output items breached the std::setw
value, the total output line size for each is the same. 注意,在两种情况下,由于没有一个输出项违反std::setw
值,因此每个输出项的总输出行大小是相同的。 All that changed was where the fills were applied and the output aligned within the std::setw
specification. 所做的所有更改就是在std::setw
规范内应用填充并对齐输出的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.