当前位置: 代码迷 >> 综合 >> boost::format学习
  详细解决方案

boost::format学习

热度:79   发布时间:2023-11-10 17:47:33.0

boost::format学习

语法:

boost::format(format-string)%arg1%arg2%...%argN;

format与printf的区别:

   printf(s, x1, x2);cout << format(s) % x1 % X2;

使用size()成员函数,得到format字符串的字符个数
使用str()成员函数,将format字符串转化为string字符串

三种常用的书写风格:

 <1>简单风格cout<<format("%1% %2% %3% %2% %1% \n") %"11"%"22"%"333";//It prints : "11 22 333 22 11 \n"<2>Posix-Printf风格cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35;//It prints : "(x,y) = ( -23, +35) \n"<3>经典风格cout <<format("writing %s,x=%s:%d-th step\n") %"toto" % 40.23% 50;//It prints:"writing toto, x=40.23 : 50-th step \n"

printf格式说明符:

不同于c printf,这里还支持位置参数Note:在同一个format string中,位置格式说明符不能与非位置格式说明符混用一个格式说明符spec有如下格式:[ N$ ] [ flags ] [ width ] [ . precision ] [ argument-type ] conversion-specifierN$:位置格式说明符,指出格式说明符应用于第N个参数如果没有,参数是按顺序对应格式说明符的flags: '-':左对齐'=':中心对齐'_':内部对齐'+':显示数字的正负号'#':显示小数点'0':对齐时用0填充' ':如果字符串不以+或-开头,则在转换的字符串前插入空格width:为转化后的字符串设置一个最小宽度precision: 设置流的精度,如一个float型数据,设置数字个数的最大值argument-type:为了处理可变参数;hh,h,l,ll,j,z,and L are recognizedconversion-specifier:b:输出布尔字符串p,x:输出十六进制o:输出八进制a:十六进制指数计数e:科学浮点格式f:固定浮点格式g:通用,默认为浮点格式X,A,E,F,G:大写输出d,i,u:输出十进制s,S:输出字符串c,C:输出单个字符%:输出%字符

一个小例子


#include<boost/format.hpp>
#include<opencv/core/core.hpp>boost::format fmt("./data/%s%d.%s");
cv::imread((fmt%"color"%1%"png").str(),-1);  //读入图像./data/color1.png
  相关解决方案