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 无删改 仅以此纪念陈皓(左耳朵耗子)
chrome 今天更新后白屏,网页打开了但是看不到内容,看得到鼠标有手指样式,也可以点击,但是看不到内容; 控制台打开也是白屏的,但是也可以点击; 完全退出后默认打开一个百度首…
如题,准备用 cursor 试试实际项目,但是项目是里面有配置文件(包含相关密钥啥的),大伙平时开发是直接开放读取然后开隐私模式呢,还是用排除规则去掉,如果用排除规则的话,我现…
今天刚新装一台,开机后到出现主板 logo 之间间隔了漫长时间,看了任务处理器启动那里显示启动 bios 花了 40 秒,不应该都是 12 秒左右吗,系统是 win11 ,主板…
合速度