<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ctemplate | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/tag/ctemplate/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Tue, 02 Nov 2010 02:29:37 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>C++的字符串格式化库</title>
		<link>https://coolshell.cn/articles/3258.html</link>
					<comments>https://coolshell.cn/articles/3258.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Tue, 02 Nov 2010 00:59:06 +0000</pubDate>
				<category><![CDATA[C/C++语言]]></category>
		<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[编程工具]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpptempl]]></category>
		<category><![CDATA[ctemplate]]></category>
		<category><![CDATA[Hapax]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=3258</guid>

					<description><![CDATA[<p>这里向大家介绍一个C++的字符串格式化库，叫cpptempl，这个库支持对字符串格式的条件，循环，变量插入。看上去很不错，只不过其是基于boost库的。 下面是...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/3258.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/3258.html">C++的字符串格式化库</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>这里向大家介绍一个C++的字符串格式化库，叫cpptempl，这个库支持对字符串格式的条件，循环，变量插入。看上去很不错，只不过其是基于boost库的。</p>
<p>下面是一个例子：</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">// The text template
wstring text = L&quot;I heart {$place}!&quot; ;
// Data to feed the template engine
cpptempl::data_map data ;
// {$place} =&gt; Okinawa
data[L&quot;place&quot;] = cpptempl::make_data(L&quot;Okinawa&quot;);
// parse the template with the supplied data dictionary
wstring result = cpptempl::parse(text, data) ;</pre>
<p>输出结果是：</p>
<blockquote><p>I heart Okinawa!</p></blockquote>
<p>是不是很方便？让我们看一个更复杂的例子：</p>
<p><span id="more-3258"></span></p>
<pre data-enlighter-language="cpp" class="EnlighterJSRAW">// You&#039;d probably load this template from a file in real life.
wstring text = L&quot;&lt;h3&gt;Locations&lt;/h3&gt;\n&lt;ul&gt;\n&quot;
    L&quot;{% for place in places %}&quot;
    L&quot;&lt;li&gt;{$place}&lt;/li&gt;\n&quot;
    L&quot;{% endfor %}&quot;
    L&quot;&lt;/ul&gt;&quot; ;
// Create the list of items
cpptempl::data_list places;
places.push_back(cpptempl::make_data(L&quot;Okinawa&quot;));
places.push_back(cpptempl::make_data(L&quot;San Francisco&quot;));
// Now set this in the data map
cpptempl::data_map data ;
data[L&quot;places&quot;] = cpptempl::make_data(places);
// parse the template with the supplied data dictionary
wstring result = cpptempl::parse(text, data) ;</pre>
<p>输出结果是：</p>
<blockquote><p>&lt;h3&gt;Locations&lt;/h3&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;Okinawa&lt;/li&gt;<br />
&lt;li&gt;San Francisco&lt;/li&gt;<br />
&lt;/ul&gt;</p></blockquote>
<p>更为详细的说明请到这里：<a href="http://bitbucket.org/ginstrom/cpptemplate/wiki/Home" target="_blank">http://bitbucket.org/ginstrom/cpptemplate/wiki/Home</a>。</p>
<p>Google也有一个类似的库叫ctemplate：<a href="http://code.google.com/p/google-ctemplate/" target="_blank">http://code.google.com/p/google-ctemplate/</a> 提供相似的方法，你也可以试试看。与Google相对应的Java库叫Hapax：<a href="http://code.google.com/p/hapax/" target="_blank">http://code.google.com/p/hapax/</a>。<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2020/03/rust-social-wide-150x150.jpg" alt="Rust语言的编程范式" width="150" height="150" /></a><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_title">Rust语言的编程范式</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="对技术的态度" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8088.html" class="wp_rp_title">对技术的态度</a></li><li ><a href="https://coolshell.cn/articles/7992.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/08/cpp_small-150x150.jpg" alt="C++的坑真的多吗？" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7992.html" class="wp_rp_title">C++的坑真的多吗？</a></li><li ><a href="https://coolshell.cn/articles/5576.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/10/00.QuickBasic_PDS_IDE-150x150.png" alt="那些曾伴我走过编程之路的软件" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5576.html" class="wp_rp_title">那些曾伴我走过编程之路的软件</a></li><li ><a href="https://coolshell.cn/articles/4990.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/07/programmer-150x150.png" alt="程序员技术练级攻略" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4990.html" class="wp_rp_title">程序员技术练级攻略</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/3258.html">C++的字符串格式化库</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/3258.html/feed</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
