这里向大家介绍一个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. asp.net 常用于客户端注册的机器信息

    项目需要:根据客户端信息去获取用户登录信息 1.根据客户端信息,并查询数据库是否有匹配.如果没有则重新插入客户端信息: 2.根据客户端的设置提交用户登录信息,用户登录成功后,查询以前是否有过配置信息, ...

  2. asp.net正则表达式类的定义

    using System; using System.Collections; using System.Reflection; using System.Reflection.Emit; using ...

  3. 共享权限ACL列表出现SID现象

    http://www.minasi.com/forum/topic.asp?TOPIC_ID=16842 Basically here's what happens, and why it doesn ...

  4. Semaphore的使用

    Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数,并提供了同步机制.使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数. Semapho ...

  5. Eclipse 写 Python的一些小问题

  6. Python实现支付宝当面付之——扫码支付

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7680348.html 一:配置信息准备 登录蚂蚁金服开放平台:https://open.alipay.com/ ...

  7. ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)

    页面跳转时,报  Undefined is not an Object(evaluating this2.props.navigation.navigate) 出错原因:在一个页面组件中调用了另一个组 ...

  8. soa---java 多线程的---锁

    如今soa 与分布式计算已经成为互联网公司技术的标配 那他包括的知识点应该熟悉了解.并以此为基础,去应用,调优各种soa的框架. 包括例如以下的四点.是分布式的基础.         a java 多 ...

  9. BigDecimal去除末尾多余的0

    Java有自带的 stripTrailingZeros() 方法用于去除末尾多余的0 BigDecimal num = new BigDecimal("100.000"); Big ...

  10. Ubuntu 12.04安装VMware Workstation8.0.3

    2012-06-18 12:52   Ubuntu安装VMware Workstation8.0.3   由于使用Ubuntu的人比较少,网上关于Ubuntu的资料也很少,笔者在安装VMware Wo ...