一、boost::format
boost::format类提供了类似C语言里'printf'功能的格式化输出能力,功能更强大。
要先include <boost/format.hpp>这个头文件。
1、一般格式
cout << boost::format("%2.3f, %d") %1.23456 %12;
printf( "%2.3f, %d\n" , 1.23456, 12 );
/*
1.234,12
*/
2、占位符
%X%表示占位符,%1%就是第一个占位符,%2%就是第二个。
cout<<boost::format("%2% \n %1% \n %3%" ) %"first" %"second" %"third";
/*
second
first
third
*/
3、boost::format对象的操作
# 对象的形式
boost::format fmt("%2% \n %1% \n %3%" );
fmt %"first";
fmt %"second";
fmt %"third";
cout<<fmt<<endl;
4、转成字符串
1、string s=fmt.str();
2、string s2=boost::str(fmt);
/*
second,first,third
*/
总结:
方式1
cout<<boost::format("%s") % "输出内容" <<endl;
方式2
boost::format fmt("%s");
fmt % "输出内容";
string s=fmt.str();
cout<<s<<endl;
方式3
string s;
s=str(boost::format("%s") % "输出内容");
cout<<s <<endl;
解析点云拼接boost::format 部分
# 拼接出格式化的图片文件名./color/1.png
# ./当前目录 ../父目录 /根目录
vector<cv::Mat> colorimages, depthimages;
vector<Eigen::Isometry3d,Eigen::aligned_allocator<Eigen::Isometry3d>> pose;
boost::format fmt("./s/d./s");
colorimages.push_back(cv::imread((fmt%"color" %"(i+1)" %"png").str()));//str()装字符串
depthimages.push_back(cv::imread((fmt%"depth" % (i+1) % "pgm").str(), -1));//使用-1读取原图
二、for 在C++11的新特性
为遍历容器提供了很大的便利,再也不用写那么长的for循环了
1、遍历字符串
string str="hello,world";
for(auto ch:str)
{
cout<< ch <<endl;
}
2、遍历数组
int arr[]={1,2,3,4};
for(auto ch:arr)
{
cout<< ch <<endl;
}
3、遍历stl容器
# 返回引用,修改容器内容。
vector <string> str={"i","like","google"};
for(auto& ch:str)
{
ch="C++";
}
解析点云拼接for 部分
# fin读取txt中参数
ifstream fin("./pose.txt");
if (!fin)
{
cerr << "请在有pose.txt的目录下运行此程序" << endl;
return 1;
}
# 遍历修改数组data的值
double data[7]={0};
for(auto& d:data)
fin>>d;