这里向大家介绍一个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>

更为详细的说明请到这里:http://bitbucket.org/ginstrom/cpptemplate/wiki/Home

Google也有一个类似的库叫ctemplate:http://code.google.com/p/google-ctemplate/ 提供相似的方法,你也可以试试看。与Google相对应的Java库叫Hapax:http://code.google.com/p/hapax/

C++的字符串格式化库的更多相关文章

  1. C++字符串格式化库:CPPFormatLibrary

    这个是很久之前写的,去年总结了一下,将其单独提取出来,作为一个开源库放到了GitHub上,然而CPPFormat之类的名字都已经被抢注了,结果只好注册了一个这么蛋疼的名字:CPPFormatLibra ...

  2. boost::format(字符串格式化库)

    这段时间学习boost库的使用,撰文一方面留以备用,另一方面就是shared精神. format主要是用来格式化std::string字符串以及配合std::cout代替C语言printf() 使用f ...

  3. (二)boost库之字符串格式化

    (二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...

  4. Lua 中的string库(字符串函数库)总结

    (字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...

  5. Python系列之模块、和字符串格式化

    Python 模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 模块分为 ...

  6. sscanf的字符串格式化用法

    sscanf()为C语言标准库函数,用于从指定字符串中读入与指定格式相符的数据.函数原型声明在stdio.h头文件中: int sscanf(const char *str, const char * ...

  7. python笔记3-输出输入、字符串格式化

    输入.输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 2 name=i ...

  8. Haskell语言学习笔记(86)字符串格式化与插值

    String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...

  9. string.Format对C#字符串格式化[转]

    string.Format对C#字符串格式化 String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 ...

随机推荐

  1. Linux FastDFS 分布式文件系统安装

    Linux FastDFS 分布式文件系统安装 2013 年 3 月 11 日 – 09:21 | 930 views | 收藏  (No Ratings Yet) FastDFS是一款类Google ...

  2. Timer 与 TimerTask 示例

    , 1000);// 1秒后执行 然后每隔1秒 执行一次 ); ); timer.cancel();//停止任务(程序停止) } } /** * 启动刷新滚动数据的定时器 */public void ...

  3. ZH奶酪:PHP error_log()将错误信息写入日志文件

    error_log() 是发送错误信息到某个地方的一个函数,在程序编程中比较常见,尤其是在程序调试阶段. bool error_log ( string $message [, int $messag ...

  4. iOS webservice SOAP 请求

    1. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, 无论它们所使用的语 ...

  5. 微信小程序 - 上传图片(组件)

    更新日期: 2019/3/14:首次发布,更新了2018/12/30的UI以及反馈信息获取方式,具体请下载:demo. 2019/3/20:感谢544429676@qq.com指出的现存bug,已修复 ...

  6. vCenter 5.1 U1 Installation: Part 9 (vCenter SSO Configuration)

    http://www.derekseaman.com/2012/09/vmware-vcenter-51-installation-part-9.html In this installment of ...

  7. 010-Go 操作PostgreSQL数据库2

    1:sql脚本 create table post( id serial primary key, content text, author ) ) 2:post.go package post im ...

  8. 007-Go package 说明

    1:在项目src下面存在两个目录,每个目录里面各有一个go文件 2:add.go package test02 func Add(a int, b int) int{ return a + b } 注 ...

  9. Velocity使用总结

    一.载入Velocity依赖包 <dependency> <groupId>org.apache.velocity</groupId> <artifactId ...

  10. Win10 PLSQL 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致

    plsql 登录后提示: Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different.Cha ...