在 C++ 中格式化输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11226143/
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
Formatting output in C++
提问by lovespeed
In a C++ code I have a matrix of double variables which I print out. However because all of them have different number of digits, the output format is destroyed. One solution is to do
cout.precision(5)
but I want different columns have a different precision. Also, because there are negative values in some cases, the presence of the -
sign also causes problems. How to get around this and produce a properly formatted output?
在 C++ 代码中,我有一个打印出来的双变量矩阵。然而,因为它们都有不同的位数,所以输出格式被破坏了。一种解决方案是这样做,
cout.precision(5)
但我希望不同的列具有不同的精度。另外,由于在某些情况下存在负值,因此-
符号的存在也会引起问题。如何解决这个问题并生成格式正确的输出?
采纳答案by stanri
Off the top of my head, you can use setw(int) to specify the width of the output.
在我的头顶上,您可以使用 setw(int) 来指定输出的宽度。
like this:
像这样:
std::cout << std::setw(5) << 0.2 << std::setw(10) << 123456 << std::endl;
std::cout << std::setw(5) << 0.12 << std::setw(10) << 123456789 << std::endl;
gives this:
给出了这个:
0.2 123456
0.12 123456789
回答by James Kanze
The key is, as others have said, to use manipulators. What they
neglected to say is that you normally use manipulators that you write
yourself. An FFmt
manipulator (which corresponds to the F
format in
Fortran is fairly easy:
正如其他人所说,关键是使用操纵器。他们忽略的是,您通常使用自己编写的操纵器。一个FFmt
操纵器(对应F
于 Fortran 中的格式相当简单:
class FFmt
{
int myWidth;
int myPrecision;
public:
FFmt( int width, int precision )
: myWidth( width )
, myPrecision( precision )
{
}
friend std::ostream&
operator<<( std::ostream& dest, FFmt const& fmt )
{
dest.setf( std::ios_base::fixed, std::ios_base::formatfield );
dest.precision( myPrecision );
dest.width( myWidth );
return dest;
}
};
This way, you can define a variable for each column, say:
这样,您可以为每一列定义一个变量,例如:
FFmt col1( 8, 2 );
FFmt col2( 6, 3 );
// ...
and write:
和写:
std::cout << col1 << value1
<< ' ' << col2 << value2...
In general, except in the simplest programs, you should probably not be
using the standard manipulators, but rather custom manipulators based on
your application; e.g. temperature
and pressure
if that's the sort of
thing your dealing with. In this way, it's clear in the code what
you're formatting, and if the client suddenly asks for one more digit in
the pressure, you know exactly where to make the change.
一般来说,除了在最简单的程序中,您可能不应该使用标准操纵器,而应该使用基于您的应用程序的自定义操纵器;例如temperature
,pressure
如果那是您要处理的事情。通过这种方式,在代码中很清楚你要格式化什么,如果客户突然要求在压力中再增加一位,你就知道在哪里进行更改。
回答by John Dibling
Use manipulators.
使用操纵器。
From sample here:
从这里的样本:
#include <iostream>
#include <iomanip>
#include <locale>
int main()
{
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "Left fill:\n" << std::left << std::setfill('*')
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << std::hex << std::showbase << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Internal fill:\n" << std::internal
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Right fill:\n" << std::right
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << '\n';
}
Output:
输出:
Left fill:
-1.23*******
0x2a********
USD *1.23***
Internal fill:
-*******1.23
0x********2a
USD ****1.23
Right fill:
*******-1.23
********0x2a
***USD *1.23
回答by jrok
Take a look at stream manipulators, especially std::setw
and std::setfill
.
看看流操纵器,尤其是std::setw
和std::setfill
。
float f = 3.1415926535;
std::cout << std::setprecision(5) // precision of floating point output
<< std::setfill(' ') // character used to fill the column
<< std::setw(20) // width of column
<< f << '\n'; // your number
回答by Premkumar U
Try using setw manipulator. Please refer http://www.cplusplus.com/reference/iostream/manipulators/setw/for further information
尝试使用 setw 操纵器。请参阅http://www.cplusplus.com/reference/iostream/manipulators/setw/了解更多信息
回答by Benjamin Lindley
There is a way using i/o manipulators, but I find it unwieldy. I would just write a function like this:
有一种使用 i/o 操纵器的方法,但我觉得它很笨拙。我只想写一个这样的函数:
template<typename T>
std::string RightAligned(int size, const T & val)
{
std::string x = boost::lexical_cast<std::string>(val);
if (x.size() < size)
x = std::string(size - x.size(), ' ') + x;
return x;
}