我正在尝试使用宽度和精度说明符boost::format,如下所示:
#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;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为boost::format不支持说明*符.解析字符串时,Boost会抛出异常.
有没有办法实现相同的目标,最好是使用直接替换?
试试这个:
#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;
}
Run Code Online (Sandbox Code Playgroud)
group()允许您使用参数封装一个或多个io操纵器.