boost库之format

std::string是个好东西,但是估计用过的人都有体会,就是没有像CString的format函数,当然,可以用CString来做一个中转,比如:

CString strTmp = "";

strTmp.Format("%s%d", ***);

std::string str = strTmp;

 

不过由于MFC与平台有关,不能移植到非Windows平台下。借助boost库中的format,我们可以很方便的达到这个目的,且是平台无关的。除了格式看起来怪怪的,其他都还好,不过适应就好了。下面看代码。

 

 

#include <boost/format.hpp>
#include <string>
#include <iostream>

using boost::format;


int _tmain(int argc, _TCHAR* argv[])
{
 std::string str = "";
 std::string strName = "";

 

 std::cout<<"enter your name"<<std::endl;
 std::cin>>strName;

 str = boost::str(boost::format("%1%%2%") %"my name is :" %strName);

 std::cout<<str<<std::endl;                                          // if input stan , output: my name is stan


 str = boost::str(format("%1%  %2%") % 11 % 22);    // 11 22

 

 std::cout<<boost::format("%1% %2%") % "hello" % "world" <<std::endl;  //hello, world

 

 system("pause");
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值