C++的字符串格式化库
这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入。看上去很不错,只不过其是基于boost库的。
下面是一个例子:
// 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!
是不是很方便?让我们看一个更复杂的例子:
// You'd probably load this template from a file in real life.
wstring text = L"
Locations
\n- \n"
- {$place} \n"
L"{% for place in places %}"
L"
L"{% endfor %}"
L"
// 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) ;
输出结果是:
Locations
- Okinawa
- San Francisco
更为详细的说明请到这里: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/。
转载于酷壳CoolShell 无删改 仅以此纪念陈皓(左耳朵耗子)
对正则表达式很头疼,是不是?每次看到都觉得像看天书似的,别说让人自己整一个出来了。下面这个网站可以帮你生成正则表达式,而且还可以根据不同的语言生成不同的代码示例,很强大。 …
开个年会饭也不好吃,奖也没抽到,还要受这个气!!! 一句话:不去! 缺席、迟到扣分,都是基本礼仪吧? 其他的不参与又不扣分,有什么问题吗? 年会还要正装?穿个西…
偶然间看到了阿里中间件Dubbo的性能测试报告,我觉得这份性能测试报告让人觉得做这性能测试的人根本不懂性能测试,我觉得这份报告会把大众带沟里去,所以,想写下这篇文章,做一点科普…