Formatting output in C++

前端 未结 6 1174
既然无缘
既然无缘 2020-12-01 09:52

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 solutio

6条回答
  •  春和景丽
    2020-12-01 10:02

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

    template
    std::string RightAligned(int size, const T & val)
    {
        std::string x = boost::lexical_cast(val);
        if (x.size() < size)
            x = std::string(size - x.size(), ' ') + x;
        return x;
    }
    

提交回复
热议问题