使用*宽度放大器; precision说明符用升压::格式 [英] Using * Width & Precision Specifiers With boost::format

查看:108
本文介绍了使用*宽度放大器; precision说明符用升压::格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的宽度和precision符与的boost ::格式,就像这样:

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

#include <boost\format.hpp>
#include <string>

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

但是,这并不工作,因为的boost ::格式不支持 * 说明。解析字符串时加速抛出异常。

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

有没有办法来完成相同的目标,preferably使用简易替换?

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

推荐答案

试试这个:

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

组(),您可以封装一个或多个IO操纵一个参数。

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

这篇关于使用*宽度放大器; precision说明符用升压::格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆