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

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

这里向大家介绍一个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://pewvl.baihongyu.com/

你可能感兴趣的文章
《SolidWorks 2016中文版完全自学手册)》——2.4 尺寸标注
查看>>
HBase 事务支持 Omid
查看>>
《程序员的呐喊》一一1.3 作者手记:名词王国里的执行
查看>>
维护 VS Code 开源项目背后的那些事情
查看>>
亿级用户平台的大数据实践
查看>>
《IPv6精髓(第2版)》——3.5 全局路由前缀
查看>>
Parse SDK:里面到底有什么宝贝?
查看>>
《Java遗传算法编程》—— 2.8 交叉实现
查看>>
《实用软件架构:从系统环境到软件部署 》——导读
查看>>
机器学习领域的几种主要学习方式
查看>>
数据库存储时间的时区问题
查看>>
《Python Cookbook(第2版)中文版》——1.16 替换字符串中的子串
查看>>
《Python Cookbook(第2版)中文版》——1.15 扩展和压缩制表符
查看>>
使用DNSCrypt来加密您与OpenDNS之间的通信
查看>>
支付宝体验设计精髓
查看>>
如何在 Linux 上永久挂载一个 Windows 共享
查看>>
《MapReduce 2.0源码分析与编程实战》一2.2 数据操作
查看>>
springboot(七):springboot+mybatis多数据源最简解决方案
查看>>
《jQuery移动开发》—— 第 1 章 理解jQuery
查看>>
使用Docker做开发的建议团队工作流
查看>>