Formatting output in C++

浪尽此生 提交于 2019-11-26 13:52:05

问题


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?


回答1:


Off the top of my head, you can use setw(int) to specify the width of the output.

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



回答2:


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:

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.




回答3:


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



回答4:


Take a look at stream manipulators, especially std::setw and 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



回答5:


Try using setw manipulator. Please refer http://www.cplusplus.com/reference/iostream/manipulators/setw/ for further information




回答6:


There is a way using i/o manipulators, but I find it unwieldy. I would just write a function like this:

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;
}


来源:https://stackoverflow.com/questions/11226143/formatting-output-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!