博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++的字符串格式化库 | 酷壳 - CoolShell.cn
阅读量:6875 次
发布时间:2019-06-26

本文共 1245 字,大约阅读时间需要 4 分钟。

C++的字符串格式化库

2010年11月2日
3,098 人阅读    

这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入。看上去很不错,只不过其是基于boost库的。

下面是一个例子:

1
2
3
4
5
6
7
8
// The text template
wstring text = L
"I heart {$place}!"
;
// Data to feed the template engine
cpptempl::data_map data ;
// {$place} => Okinawa
data[L
"place"
] = cpptempl::make_data(L
"Okinawa"
);
// parse the template with the supplied data dictionary
wstring result = cpptempl::parse(text, data) ;

输出结果是:

I heart Okinawa!

是不是很方便?让我们看一个更复杂的例子:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// You'd probably load this template from a file in real life.
wstring text = L
"<h3>Locations</h3>\n<ul>\n"
    
L
"{% for place in places %}"
    
L
"<li>{$place}</li>\n"
    
L
"{% endfor %}"
    
L
"</ul>"
;
// Create the list of items
cpptempl::data_list places;
places.push_back(cpptempl::make_data(L
"Okinawa"
));
places.push_back(cpptempl::make_data(L
"San Francisco"
));
// Now set this in the data map
cpptempl::data_map data ;
data[L
"places"
] = cpptempl::make_data(places);
// parse the template with the supplied data dictionary
wstring result = cpptempl::parse(text, data) ;

输出结果是:

<h3>Locations</h3>

<ul>
<li>Okinawa</li>
<li>San Francisco</li>
</ul>

更为详细的说明请到这里:。

Google也有一个类似的库叫ctemplate: 提供相似的方法,你也可以试试看。与Google相对应的Java库叫Hapax:。

转载地址:http://jjofl.baihongyu.com/

你可能感兴趣的文章
「镁客·请讲」梦之墨陈柏炜:基于液态金属的价值点,让电子制造打破时空限制...
查看>>
Java学习笔记--可变参数
查看>>
Splunk:勒索病毒之战尚未成功
查看>>
全球数据分析之王:中国数据分析市场正在爆发
查看>>
「镁客·请讲」维睛视空赵金辉:技术是初心,硬件是未来
查看>>
Facebook将未来押注在AR/VR
查看>>
「镁客·请讲」阿凡达机器人黄婷钰:定位儿童机器人,从硬件到软件、内容构建全面生态系统...
查看>>
郁金香商业辅助教程 2016 笔记 1~5
查看>>
区块链每日一问丨怎样防止一个比特币被使用两次?
查看>>
招商银行为什么选用MySQL
查看>>
PostgreSQL删除父表
查看>>
“风口”只是一方面,看家电电商如何玩转立体化营销问鼎第一渠道
查看>>
Linux下使用make install安装的软件如何卸载
查看>>
【阿里云流计算】- 电商每天成交总额案例
查看>>
ansible安装配置及实例
查看>>
<转>VC之获取CPU序列号
查看>>
LAMP纯源码编译安装日志
查看>>
什么是网站pv值
查看>>
用VS调试嵌入在MFC程序里的WPF View代码
查看>>
fdisk 命令详解
查看>>