C++的字符串格式化库
2018-03-04 08:36
204 查看
这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入。看上去很不错,只不过其是基于boost库的。
下面是一个例子:
12345678 | // 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!
是不是很方便?让我们看一个更复杂的例子:
123456789101112131415 | // 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++的字符串格式化库
- C#字符串格式化的C++实现
- 格式化字符串——以C++的名义 (zt)
- C++字符串格式化(FormatString)
- C++字符串格式化文章
- [转]C++ 字符串格式化 sprintf
- [转]C++字符串格式化 sprintf
- 使用C++ ostringstream来格式化字符串输出
- C++字符串格式化 sprintf、printf
- C++的字符串格式化库
- C++中关于文字编码的问题(格式化字符串)
- 转载收藏一篇C++字符串格式化文章
- C/C++格式化字符串说明
- C++字符串格式化文章
- C++字符串格式化 sprintf、printf
- 格式化字符串——以C++的名义 - [软件开发]
- C++字符串格式化 sprintf
- C++的字符串格式化库 | 酷壳 - CoolShell.cn
- C++字符串格式化
- C++字符串格式化 sprintf详解