Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to use width and precision specifiers with boost::format, like this:

#include <boostformat.hpp>
#include <string>

int main()
{
    int n = 5;
    std::string s = (boost::format("%*.*s") % (n*2) % (n*2) % "Hello").str();
    return 0;
}

But this doesn't work because boost::format doesn't support the * specifier. Boost throws an exception when parsing the string.

Is there a way to accomplish the same goal, preferably using a drop-in replacement?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

#include <boost/format.hpp>
#include <iomanip>

using namespace std;
using namespace boost;

int main()
{
    int n = 5;
    string s = (format("%s") % io::group(setw(n*2), setprecision(n*2), "Hello")).str();
    return 0;
}

group() lets you encapsulate one or more io manipulators with a parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...