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 无删改 仅以此纪念陈皓(左耳朵耗子)
小姐姐太多,分不清了,我看刮削方案都是基于 nas 的,有没有基于 pc 的? pc 端也有 jellyfin 虚拟机 肥牛? 您是否是在寻找:JAVSP git…
感觉国产安卓机性价比高,而且用户基数大,可玩项目更多吧? 没办法解锁 bootloader ,怎么刷 不是有关注海外的厂家吗? 海外市场有需要刷机的吧? vivo …
上个月服务器到期,刚好看到天翼云低价活动,于是就脑抽买了并把服务迁移到天翼云上,于是噩梦开始了... 只要你的域名有备案在天翼云,它的机器人就会不停地扫描你的所有网站,我有几个…
合速度