﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Carlward blog-文章分类-STL</title><link>http://www.cppblog.com/carlward/category/12435.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 24 Dec 2009 22:17:27 GMT</lastBuildDate><pubDate>Thu, 24 Dec 2009 22:17:27 GMT</pubDate><ttl>60</ttl><item><title>（原创）STL 使用注意事项</title><link>http://www.cppblog.com/carlward/articles/103855.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Wed, 23 Dec 2009 14:31:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/103855.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/103855.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/103855.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/103855.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/103855.html</trackback:ping><description><![CDATA[结合自身遇到的问题与网络上查找到的信息，将一些注意事项记录下来。一来避免将来犯同样的错误，二来也给大家提个醒。<br><br>1、在 DLL 中不可以将 std::string 作为函数入口参数<br>&nbsp;&nbsp; 原因：DLL 与 EXE 的内存分配方式不一样<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXE 调用 DLL 中使用 std::string 作为参数的函数会导致内存分配泄漏
<img src ="http://www.cppblog.com/carlward/aggbug/103855.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-12-23 22:31 <a href="http://www.cppblog.com/carlward/articles/103855.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转帖）如何使用Function Object? (C/C++) (STL) </title><link>http://www.cppblog.com/carlward/articles/103661.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Mon, 21 Dec 2009 15:59:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/103661.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/103661.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/103661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/103661.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/103661.html</trackback:ping><description><![CDATA[<a href="http://www.cnblogs.com/oomusou/archive/2007/01/18/623299.html">http://www.cnblogs.com/oomusou/archive/2007/01/18/623299.html</a><br><br>
<p>很多STL algorithm都是_if結尾的，讓我們可以帶function進去，若配合function object，可讓function更有彈性!!</p>
<p>以下的範例想利用count_if() algorithm得知vector大於n的有幾個?<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img id=Codehighlighter1_0_207_Open_Image onclick="this.style.display='none'; Codehighlighter1_0_207_Open_Text.style.display='none'; Codehighlighter1_0_207_Closed_Image.style.display='inline'; Codehighlighter1_0_207_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_0_207_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_0_207_Closed_Text.style.display='none'; Codehighlighter1_0_207_Open_Image.style.display='inline'; Codehighlighter1_0_207_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><span id=Codehighlighter1_0_207_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id=Codehighlighter1_0_207_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>(C)&nbsp;OOMusou&nbsp;2007&nbsp;</span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://oomusou.cnblogs.com</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Filename&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;FunctionObjectSimple.cpp<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Compiler&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Visual&nbsp;C++&nbsp;8.0&nbsp;/&nbsp;ISO&nbsp;C++<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Description&nbsp;:&nbsp;Demo&nbsp;how&nbsp;to&nbsp;use&nbsp;use&nbsp;Function&nbsp;Object<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Release&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;01/18/2007&nbsp;1.0<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;biggerThan3(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Function&nbsp;Object</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img id=Codehighlighter1_352_456_Open_Image onclick="this.style.display='none'; Codehighlighter1_352_456_Open_Text.style.display='none'; Codehighlighter1_352_456_Closed_Image.style.display='inline'; Codehighlighter1_352_456_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_352_456_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_352_456_Closed_Text.style.display='none'; Codehighlighter1_352_456_Open_Image.style.display='inline'; Codehighlighter1_352_456_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;biggerThan&nbsp;</span><span id=Codehighlighter1_352_456_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_352_456_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img id=Codehighlighter1_390_391_Open_Image onclick="this.style.display='none'; Codehighlighter1_390_391_Open_Text.style.display='none'; Codehighlighter1_390_391_Closed_Image.style.display='inline'; Codehighlighter1_390_391_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_390_391_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_390_391_Closed_Text.style.display='none'; Codehighlighter1_390_391_Open_Image.style.display='inline'; Codehighlighter1_390_391_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;biggerThan(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)&nbsp;:&nbsp;n(n)&nbsp;</span><span id=Codehighlighter1_390_391_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_390_391_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Constructor</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #008000"><img id=Codehighlighter1_436_454_Open_Image onclick="this.style.display='none'; Codehighlighter1_436_454_Open_Text.style.display='none'; Codehighlighter1_436_454_Closed_Image.style.display='inline'; Codehighlighter1_436_454_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_436_454_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_436_454_Closed_Text.style.display='none'; Codehighlighter1_436_454_Open_Image.style.display='inline'; Codehighlighter1_436_454_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">()&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;val)&nbsp;</span><span id=Codehighlighter1_436_454_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_436_454_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;val&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;n;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_471_720_Open_Image onclick="this.style.display='none'; Codehighlighter1_471_720_Open_Text.style.display='none'; Codehighlighter1_471_720_Closed_Image.style.display='inline'; Codehighlighter1_471_720_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_471_720_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_471_720_Closed_Text.style.display='none'; Codehighlighter1_471_720_Open_Image.style.display='inline'; Codehighlighter1_471_720_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;</span><span id=Codehighlighter1_471_720_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_471_720_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_486_500_Open_Image onclick="this.style.display='none'; Codehighlighter1_486_500_Open_Text.style.display='none'; Codehighlighter1_486_500_Closed_Image.style.display='inline'; Codehighlighter1_486_500_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_486_500_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_486_500_Closed_Text.style.display='none'; Codehighlighter1_486_500_Open_Image.style.display='inline'; Codehighlighter1_486_500_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;ia[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span id=Codehighlighter1_486_500_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_486_500_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;ivec(ia,&nbsp;ia&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(ia)&nbsp;</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;count_if(ivec.begin(),&nbsp;ivec.end(),&nbsp;biggerThan3);<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;count_if(ivec.begin(),&nbsp;ivec.end(),&nbsp;biggerThan(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;j&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img id=Codehighlighter1_749_769_Open_Image onclick="this.style.display='none'; Codehighlighter1_749_769_Open_Text.style.display='none'; Codehighlighter1_749_769_Closed_Image.style.display='inline'; Codehighlighter1_749_769_Closed_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_749_769_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_749_769_Closed_Text.style.display='none'; Codehighlighter1_749_769_Open_Image.style.display='inline'; Codehighlighter1_749_769_Open_Text.style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;biggerThan3(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;val)&nbsp;</span><span id=Codehighlighter1_749_769_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_749_769_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;val&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br>執行結果<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">2</span><span style="COLOR: #000000"><br><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">2</span></div>
<br>若沒有function object，我們就只能帶一個function name進去，由於其signature是固定的，所以只能帶進如33行那樣固定n的function，但STL algorithm還允許我們帶function object進去，若能用function object，就很有彈性了，18~22行將function包成function object，當然用class也行，但若用struct可以省去public:字眼，首先用constructor接下參數，然後對() operator做overload，這樣29行就可以帶參數進去，無論n帶多少都可以，當然更有彈性了。 <br>
<img src ="http://www.cppblog.com/carlward/aggbug/103661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-12-21 23:59 <a href="http://www.cppblog.com/carlward/articles/103661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转帖）如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template) </title><link>http://www.cppblog.com/carlward/articles/103660.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Mon, 21 Dec 2009 15:55:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/103660.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/103660.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/103660.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/103660.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/103660.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: http://www.cnblogs.com/oomusou/archive/2007/05/12/744000.htmlAbstract之前在(原創) 如何使用for_each() algorithm? (C/C++) (STL)&nbsp;曾經討論過for_each()，不過當時功力尚淺，只談到了皮毛而已，這次看了effective STL的item 41、43後，對for_each()又...&nbsp;&nbsp;<a href='http://www.cppblog.com/carlward/articles/103660.html'>阅读全文</a><img src ="http://www.cppblog.com/carlward/aggbug/103660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-12-21 23:55 <a href="http://www.cppblog.com/carlward/articles/103660.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转帖）如何使for_each()傳回值? (C/C++) (STL) </title><link>http://www.cppblog.com/carlward/articles/103657.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Mon, 21 Dec 2009 15:46:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/103657.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/103657.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/103657.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/103657.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/103657.html</trackback:ping><description><![CDATA[<a href="http://www.cnblogs.com/oomusou/archive/2007/05/13/744906.html">http://www.cnblogs.com/oomusou/archive/2007/05/13/744906.html</a><br><br>
<p><strong>Abstract</strong><br>for_each()是STL中少數可以回傳值的algorithm，此範例在展示for_each()此特殊功能與function object可以留住state的特性。<br><br><strong>Introduction<br></strong>function object與global function的差別除了function object可以傳入參數外，還可以不使用static就可以留住state。<br><br>一個簡單的需求，想要每印n個數字就換行，並且統計出所有iterator的和，所以function object必須能留住state才知道目前印了幾個數字，且統計sum為多少。<br><br><strong>Sample Code<br></strong></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img id=Codehighlighter1_0_299_Open_Image onclick="this.style.display='none'; Codehighlighter1_0_299_Open_Text.style.display='none'; Codehighlighter1_0_299_Closed_Image.style.display='inline'; Codehighlighter1_0_299_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_0_299_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_0_299_Closed_Text.style.display='none'; Codehighlighter1_0_299_Open_Image.style.display='inline'; Codehighlighter1_0_299_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><span id=Codehighlighter1_0_299_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id=Codehighlighter1_0_299_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>(C)&nbsp;OOMusou&nbsp;2007&nbsp;</span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://oomusou.cnblogs.com</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Filename&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;GenericAlgo_for_each_state_returnValue.cpp<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Compiler&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;Visual&nbsp;C++&nbsp;8.0&nbsp;/&nbsp;BCB&nbsp;6.0&nbsp;/&nbsp;gcc&nbsp;3.4.2&nbsp;/&nbsp;ISO&nbsp;C++<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Description&nbsp;:&nbsp;Demo&nbsp;how&nbsp;to&nbsp;use&nbsp;for_each&nbsp;to&nbsp;return&nbsp;value&nbsp;and&nbsp;remain&nbsp;state.<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>Release&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;05/13/2007&nbsp;1.0<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;05/15/2007&nbsp;2.0<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #008000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img id=Codehighlighter1_400_691_Open_Image onclick="this.style.display='none'; Codehighlighter1_400_691_Open_Text.style.display='none'; Codehighlighter1_400_691_Closed_Image.style.display='inline'; Codehighlighter1_400_691_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_400_691_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_400_691_Closed_Text.style.display='none'; Codehighlighter1_400_691_Open_Image.style.display='inline'; Codehighlighter1_400_691_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;printElem&nbsp;</span><span id=Codehighlighter1_400_691_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_400_691_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_cnt;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_sum;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_505_506_Open_Image onclick="this.style.display='none'; Codehighlighter1_505_506_Open_Text.style.display='none'; Codehighlighter1_505_506_Closed_Image.style.display='inline'; Codehighlighter1_505_506_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_505_506_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_505_506_Closed_Text.style.display='none'; Codehighlighter1_505_506_Open_Image.style.display='inline'; Codehighlighter1_505_506_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;printElem(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">)&nbsp;:&nbsp;_n(n),&nbsp;_cnt(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">),&nbsp;_sum(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span id=Codehighlighter1_505_506_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_505_506_Open_Text><span style="COLOR: #000000">{}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_538_646_Open_Image onclick="this.style.display='none'; Codehighlighter1_538_646_Open_Text.style.display='none'; Codehighlighter1_538_646_Closed_Image.style.display='inline'; Codehighlighter1_538_646_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_538_646_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_538_646_Closed_Text.style.display='none'; Codehighlighter1_538_646_Open_Image.style.display='inline'; Codehighlighter1_538_646_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">()&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;elem)&nbsp;</span><span id=Codehighlighter1_538_646_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_538_646_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">_cnt;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;_sum&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;elem;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;elem;<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;(_cnt&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;_n)&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;:&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img id=Codehighlighter1_668_689_Open_Image onclick="this.style.display='none'; Codehighlighter1_668_689_Open_Text.style.display='none'; Codehighlighter1_668_689_Closed_Image.style.display='inline'; Codehighlighter1_668_689_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_668_689_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_668_689_Closed_Text.style.display='none'; Codehighlighter1_668_689_Open_Image.style.display='inline'; Codehighlighter1_668_689_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">()&nbsp;</span><span id=Codehighlighter1_668_689_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_668_689_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;_sum;<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img id=Codehighlighter1_706_892_Open_Image onclick="this.style.display='none'; Codehighlighter1_706_892_Open_Text.style.display='none'; Codehighlighter1_706_892_Closed_Image.style.display='inline'; Codehighlighter1_706_892_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_706_892_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_706_892_Closed_Text.style.display='none'; Codehighlighter1_706_892_Open_Image.style.display='inline'; Codehighlighter1_706_892_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;</span><span id=Codehighlighter1_706_892_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_706_892_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;ivec;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;ivec.push_back(i);<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;for_each(ivec.begin(),&nbsp;ivec.end(),&nbsp;printElem(</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">sum&nbsp;is&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;sum&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br></p>
<p><br><strong>執行結果<br></strong></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000"><br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000"><br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">11</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">13</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">14</span><span style="COLOR: #000000"><br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">15</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">17</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">18</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">19</span><span style="COLOR: #000000"><br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>sum&nbsp;is&nbsp;</span><span style="COLOR: #000000">190</span></div>
<p><br>17行<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_cnt;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_sum;</span></div>
<p><br>_n為設定幾個字跳行<br>_cnt統計目前已經印了幾個字<br>_sum統計目前加總結果<br><br>29行<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(_cnt&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;_n)&nbsp;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;elem&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;elem&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;endl;</span></div>
<p><br>若每印n個字，就加印換行<br><br>35行<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id=Codehighlighter1_15_32_Open_Image onclick="this.style.display='none'; Codehighlighter1_15_32_Open_Text.style.display='none'; Codehighlighter1_15_32_Closed_Image.style.display='inline'; Codehighlighter1_15_32_Closed_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_15_32_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_15_32_Closed_Text.style.display='none'; Codehighlighter1_15_32_Open_Image.style.display='inline'; Codehighlighter1_15_32_Open_Text.style.display='inline';" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">()&nbsp;</span><span id=Codehighlighter1_15_32_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cnblogs.com/Images/dot.gif"></span><span id=Codehighlighter1_15_32_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;_sum;<br><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br>為了讓for_each()能傳回值，特別改寫operator int()，讓function object能夠傳回值。<br><br>46行<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;for_each(ivec.begin(),&nbsp;ivec.end(),&nbsp;printElem(</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">));</span></div>
<p><br>這樣for_each()就能風風光光的每n個字就換行，還可以順便加總結果。<br><br><strong>Conclusion</strong><br>STL真的很神奇，以上的程式想一行一行翻成C#還真的做不到呢!!<br></p>
<img src ="http://www.cppblog.com/carlward/aggbug/103657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-12-21 23:46 <a href="http://www.cppblog.com/carlward/articles/103657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转帖）如何将字符串前后的空白去除? (C/C++) (STL) </title><link>http://www.cppblog.com/carlward/articles/103656.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Mon, 21 Dec 2009 15:45:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/103656.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/103656.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/103656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/103656.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/103656.html</trackback:ping><description><![CDATA[<a href="http://www.cnblogs.com/oomusou/archive/2008/07/15/564786.html">http://www.cnblogs.com/oomusou/archive/2008/07/15/564786.html</a><br><br>
<p><strong>Abstract</strong><br>在<a title="(原創) 如何将字符串前后的空白去除? (中级) (C++) (使用string.find_first_not_of, string.find_last_not_of) " href="http://www.cnblogs.com/oomusou/archive/2006/11/17/564197.html" target=_blank><u><font color=#0000ff>(原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++) (STL) </font></u></a>中已经可顺利将字符串前后的空白去除，且程序相当的精简，在此用另外一种方式达到此要求，且可同时将whitespace去除，并且使用template写法。</p>
<p><strong>Introduction<br></strong>原來版本的程式在VC8可執行，但無法在Dev-C++執行，目前已經修正。</p>
<p><strong>stringTrim1.cpp / C++</strong><br></p>
<div class=cnblogs_code><span style="COLOR: #008080">1</span>&nbsp;<span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;<br></span><span style="COLOR: #008080">2</span>&nbsp;<span style="COLOR: #008000">(C) OOMusou 2006 </span><span style="COLOR: #008000; TEXT-DECORATION: underline">http://oomusou.cnblogs.com</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">3</span>&nbsp;<span style="COLOR: #008000"><br></span><span style="COLOR: #008080">4</span>&nbsp;<span style="COLOR: #008000">Filename&nbsp;&nbsp;&nbsp; : StringTrim1.cpp<br></span><span style="COLOR: #008080">5</span>&nbsp;<span style="COLOR: #008000">Compiler&nbsp;&nbsp;&nbsp; : Visual C++ 8.0 / Dev-C++ 4.9.9.2<br></span><span style="COLOR: #008080">6</span>&nbsp;<span style="COLOR: #008000">Description : Demo how to trim string by find_first_not_of &amp; find_last_not_of<br></span><span style="COLOR: #008080">7</span>&nbsp;<span style="COLOR: #008000">Release&nbsp;&nbsp;&nbsp;&nbsp; : 07/15/2008<br></span><span style="COLOR: #008080">8</span>&nbsp;<span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">9</span>&nbsp;<span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000">#include </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cwctype</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000"> std;<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> trim(</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> s) {<br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (s.empty()) {<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> s;<br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp; }<br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000">&nbsp; <br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">::iterator c;<br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Erase whitespace before the string</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> s.begin(); c </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> s.end() </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> iswspace(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">););<br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; s.erase(s.begin(), </span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">c);<br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Erase whitespace after the string</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> s.end(); c </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> s.begin() </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> iswspace(</span><span style="COLOR: #000000">*--</span><span style="COLOR: #000000">c););<br></span><span style="COLOR: #008080">27</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp; s.erase(</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">c, s.end());<br></span><span style="COLOR: #008080">28</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> s;<br></span><span style="COLOR: #008080">30</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">31</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span>&nbsp;<span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main( ) {<br></span><span style="COLOR: #008080">33</span>&nbsp;<span style="COLOR: #000000">&nbsp; </span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000"> s </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">&nbsp;&nbsp; Hello World!!&nbsp;&nbsp; </span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">34</span>&nbsp;<span style="COLOR: #000000">&nbsp; cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> s </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000"> size:</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> s.size() </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br></span><span style="COLOR: #008080">35</span>&nbsp;<span style="COLOR: #000000">&nbsp; cout </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> trim(s) </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000"> size:</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> trim(s).size() </span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000"> endl;<br></span><span style="COLOR: #008080">36</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<p><br>22和23行<br></p>
<div class=cnblogs_code><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Erase whitespace before the string</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> s.begin(); c </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> s.end() </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> iswspace(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">););<br>&nbsp; s.erase(s.begin(), </span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">c);</span></div>
<p><br>是将字符串前的whitespace删除。<br><br>26和27行<br></p>
<div class=cnblogs_code><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Erase whitespace after the string</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> s.end(); c </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> s.begin() </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> iswspace(</span><span style="COLOR: #000000">*--</span><span style="COLOR: #000000">c););<br>&nbsp; s.erase(</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">c, s.end());</span></div>
<p><br>是将字符串后的whitespace删除。<br><br>22行是一种变形的for写法，for的expr3区没写，并将increment写在expr2区，for从s.begin()开始，若未到s.end()尾端且是whitespace的话就继续，并且判断完whitespace就+1，其实整个for loop就相当于string.find_first_not_of()，要找到第一个不是whitespace的位置。<br><br>23行从s.begin()开始删除whitespace，但为什么要删到--c呢?因为32行的for loop，在跳离for loop之前，已经先+1，所以在此要-1才会正确。<br><br>26和27行同理，只是它是从字符串尾巴考虑。<br><br>我承认这段程序不是很好懂，是充满了C style的写法，不过++,--这种写法本来就是C的光荣、C的特色，既然C++强调多典，又是继承C语言，所以C++程序员还是得熟析这种写法。<br></p>
<img src ="http://www.cppblog.com/carlward/aggbug/103656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-12-21 23:45 <a href="http://www.cppblog.com/carlward/articles/103656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>几种STL容器的基本用法[资料]</title><link>http://www.cppblog.com/carlward/articles/102208.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Sat, 28 Nov 2009 17:20:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/102208.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/102208.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/102208.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/102208.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/102208.html</trackback:ping><description><![CDATA[<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>一、原型与构造函数</font></span></span></strong></div>
<div>Vector的原型可定义为</div>
<div>vector&lt;T, allocator &lt;T&gt; &gt; </div>
<div>其构造函数为</div>
<div>vector()<span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span>空的</div>
<div>vector(al)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span>指定一种allocator</div>
<div>vector(n)&nbsp;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span>用默认T()初始化n个元素</div>
<div>vector(n, val)<span>&nbsp;&nbsp; //</span>用Val初始化n个元素</div>
<div>vector(n,val,al)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span>用val初始化n个元素，用al做分配器</div>
<div>vector(first,last)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span>从己有的first到last复制生成</div>
<div>vector(first,last,al)&nbsp;//从己有的first到last复制生成，用al做分配器</div>
<div>&nbsp;</div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>二、操作</font></span></span></strong></div>
<div><strong>1.</strong><strong>开辟N</strong><strong>个空间</strong></div>
<div>vecobj.reserve(N);</div>
<div><strong>2.</strong><strong>当前（重新分配内存前）得到最大容量</strong></div>
<div>capacity();</div>
<div><strong>3.</strong><strong>重新分配内存为N</strong></div>
<div>resize(N)</div>
<div>如果变小，则删除多余。如果变大，则用T()添充</div>
<div><strong>4.</strong><strong>清空</strong></div>
<div>clear();</div>
<div>注意，clear()和resize()都不一定使得vector变小，若欲释放内存，请使用vecobj.swap(vector&lt;T, A&gt;())</div>
<div><strong>5.</strong><strong>存取首尾元素</strong></div>
<div>front()与back()操作,取后一个和最前一个元素，注意其返回是引用,其而是左值(l_value),因此可以赋值. 做类似于vecobj.front() = 3;的操作,但要保证front空间有效，否则形为无法预测。</div>
<div><strong>6.</strong><strong>取值</strong></div>
<div>[]与at可以做此操作，at会检查，如果越界有会out_of_range的异常被throw</div>
<div><strong>7.push_back, pop_back</strong></div>
<div>要保证不为空</div>
<div><strong>8.</strong><strong>使用assign</strong></div>
<div>assign可以改变大小和初值，大小是随意的，不受开始时大小的限制，如果设置为0,则清空。</div>
<div>assign(5,0)把vector改为5个大小，并用0添充</div>
<div>assign(iax+3,iax+5); 从数组第4到5个填充，注意左闭右开，即可取到iax[3]与iax[4]</div>
<div><strong>9.</strong><strong>使用insert</strong></div>
<div>insert(it, x),在it前插入一个元素x</div>
<div>insert(it,first,last),在it前插入一个序列[first,last)左闭右开</div>
<div><strong>10.</strong><strong>使用erase</strong></div>
<div>erase(it)删除在it处的元素，返回值为下一元素。如果intVec.erase(intVec.end());并不会报错，如果删除一个序列[first,last)，使用erase(first,last)</div>
<div><strong>11.BVector</strong><strong>是vector&lt;bool&gt;</strong><strong>的特化版，具体的用途有待查证</strong></div>
<div><strong>12.flip()</strong><strong>把某一元素，求反。如vecObj[i].flip();</strong></div>
<div><strong>13.swap. vecObj.swap(vecObj[i],vecObj[j]);</strong></div>
<div>若要在容器中装一个对象并且能并检索，需要重载operator == ,如下：</div>
<div>#include &lt;vector&gt;</div>
<div>#include &lt;iostream&gt;</div>
<div>#include &lt;stdlib.h&gt;</div>
<div>#include &lt;time.h&gt;</div>
<div>//#include &lt;getopt.h&gt;</div>
<div>using namespace std;</div>
<div>&nbsp;</div>
<div>class Obj</div>
<div>{</div>
<div>public:</div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Obj(int x, int y, int z)</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this-&gt;x = x;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this-&gt;y = y;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this-&gt;z = z;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool operator == (const Obj &amp; obj)</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(obj.x == x &amp;&amp; obj.y == y &amp;&amp; obj.z == z)</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int getX()</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this -&gt; x;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div>
<div>private:</div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int x;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int y;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int z;</span></div>
<div>};</div>
<div>&nbsp;</div>
<div>int main(int argc, char * argv[])</div>
<div>{</div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vector&lt;Obj&gt; vecObj;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Obj obj1(2,3,4);</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Obj obj2(4,5,6);</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vecObj.push_back(obj1);</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vecObj.push_back(obj2);</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vector&lt;Obj&gt;::iterator it =find(vecObj.begin(),vecObj.end(),Obj(2,3,4));</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(it != vecObj.end())</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; (*it).getX() &lt;&lt; endl;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</span></div>
<div>}</div>
<div style="MARGIN: 17pt 0cm 16.5pt"><strong><span style="FONT-SIZE: x-large"><span style="COLOR: #000000"><span style="FONT-SIZE: x-small"><font size=1>list的基本用法</font></span></span></span></strong></div>
<div>与vector的用法基本相同，其中需要强调一点的是splice()函数，是指把指定段的另一个List插入到指定位置的前面。</div>
<div>splice(iterator it , list &amp;x)</div>
<div>splice(iterator it, list &amp;x, iterator first)</div>
<div>splice(iterator it,list &amp;x, iterator first, iterator last)</div>
<div>&nbsp;</div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>一、原型与构造函数</font></span></span></strong></div>
<div>typdef list&lt;T, allocator&lt;T&gt; &gt; &nbsp;listObj;</div>
<div>构造函数</div>
<div>list()<span> //</span>空</div>
<div>list(al) //指定allocator的空表</div>
<div>list(n)//n个元素，所有元素都是T()出来的</div>
<div>list(n,val)//n个元素，所有元素都是T(val)出来的</div>
<div>list(n,val,al)//同上，并指定allocator为al</div>
<div>list(first, last) //复制构造 </div>
<div>list(first,last,al) //指定allocator构造</div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>二、操作</font></span></span></strong></div>
<div><strong>1.resize &amp; clear</strong></div>
<div>使用resize(n)改变大小，使用resize(n, val)如果需要用T(val) 来填满空闲值。</div>
<div><strong>2.front ()&amp; back() </strong></div>
<div>如果listObj非常量对象,返回是一个左值函数</div>
<div><strong>3.</strong><strong>插入操作</strong></div>
<div>insert(iterator it , val)</div>
<div>insert(iterator it, first, last)</div>
<div>insert(iteratot it, n, x)//插入n个x</div>
<div><strong>4.</strong><strong>移除</strong></div>
<div>remove(x); //vector.erase(integrator it)</div>
<div><strong>按值删</strong></div>
<div style="TEXT-INDENT: 21pt">int iax[] ={3,4,5,6,6,7,8};</div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list&lt;int&gt; lObj;</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lObj.insert(lObj.begin(),iax, iax + 7);</span></div>
<div><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lObj.remove(6); //</span></div>
<div><strong>按函数条件删 </strong></div>
<div>&nbsp;</div>
<div>#include &lt;iostream&gt;</div>
<div>#include &lt;list&gt;</div>
<div>using namespace std;</div>
<div>// a predicate implemented as a function:</div>
<div>bool single_digit (const int&amp; value) { return (value&lt;10); }</div>
<div>// a predicate implemented as a class:</div>
<div>class is_odd</div>
<div>{</div>
<div>public:</div>
<div>&nbsp;bool operator() (const int&amp; value) {return (value%2)==1; }</div>
<div>};</div>
<div>int main ()</div>
<div>{</div>
<div>&nbsp;int myints[]= {15,36,7,17,20,39,4,1};</div>
<div>&nbsp;list&lt;int&gt; mylist (myints,myints+8);<span>&nbsp;&nbsp; // 15 36 7 17 20 39 4 1</span></div>
<div>&nbsp;mylist.remove_if (single_digit);<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 15 36 17 20 39</span></div>
<div>&nbsp;mylist.remove_if (is_odd());<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 36 20</span></div>
<div>&nbsp;cout &lt;&lt; "mylist contains:";</div>
<div>&nbsp;for (list&lt;int&gt;::iterator it=mylist.begin(); it!=mylist.end(); ++it)</div>
<div><span>&nbsp;&nbsp;&nbsp; cout &lt;&lt; " " &lt;&lt; *it;</span></div>
<div>&nbsp;cout &lt;&lt; endl;</div>
<div>&nbsp;return 0;</div>
<div>}</div>
<div>当然，对于class is_odd，也可以写成 </div>
<div>template &lt;class T&gt;</div>
<div>class is_odd</div>
<div>{</div>
<div>};</div>
<div>调用时，则要改成</div>
<div>mylist.remove_if(is_odd&lt;int&gt;());</div>
<div><strong>5.unique</strong><strong>操作</strong></div>
<div>// list::unique</div>
<pre><span style="FONT-SIZE: 10.5pt">#include &lt;iostream&gt;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">#include &lt;cmath&gt;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">#include &lt;list&gt;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">using namespace std;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">// a binary predicate implemented as a function:</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">bool same_integral_part (double first, double second)</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">{ return ( int(first)==int(second) ); }</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">// a binary predicate implemented as a class:</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">class is_near</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">{</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">public:</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;bool operator() (double first, double second)</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;{ return (fabs(first-second)&lt;5.0); }</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">};</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">int main ()</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">{</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;double mydoubles[]={ 12.15,&nbsp;2.72, 73.0,&nbsp;12.77,&nbsp;3.14,</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12.77, 73.35, 72.25, 15.3,&nbsp;72.25 };</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;list&lt;double&gt; mylist (mydoubles,mydoubles+10);</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;//<strong>UNIQUE</strong></span><strong><span style="FONT-SIZE: 10.5pt">以前必须要</span></strong><strong><span style="FONT-SIZE: 10.5pt">Sort,</span></strong><strong><span style="FONT-SIZE: 10.5pt">切记</span></strong><strong><span style="FONT-SIZE: 10.5pt">,</span></strong><strong><span style="FONT-SIZE: 10.5pt">它的内部实现是</span></strong><strong><span style="FONT-SIZE: 10.5pt">I,i+1</span></strong><strong><span style="FONT-SIZE: 10.5pt">的方式</span></strong><span style="FONT-SIZE: 10.5pt">。</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;mylist.sort();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;2.72,&nbsp;3.14, 12.15, 12.77, 12.77,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 15.3,&nbsp;72.25, 72.25, 73.0,&nbsp;73.35</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt"> &nbsp;mylist.unique();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;2.72,&nbsp;3.14, 12.15, 12.77</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 15.3,&nbsp;72.25, 73.0,&nbsp;73.35</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;mylist.unique (same_integral_part);&nbsp;//&nbsp;2.72,&nbsp;3.14, 12.15</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 15.3,&nbsp;72.25, 73.0</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;mylist.unique (is_near());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;2.72, 12.15, 72.25</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;cout &lt;&lt; "mylist contains:";</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;for (list&lt;double&gt;::iterator it=mylist.begin(); it!=mylist.end(); ++it)</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;&nbsp;&nbsp; cout &lt;&lt; " " &lt;&lt; *it;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;cout &lt;&lt; endl;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">&nbsp;return 0;</span></pre>
<pre><span style="FONT-SIZE: 10.5pt">}</span></pre>
<div>6.排序操作</div>
<div>sort(); //默认按operator &lt;排序，从小到大</div>
<div>sort(pr); //pr为Functional函数</div>
<div><strong>7.Merge</strong><strong>操作</strong></div>
<div>在merge操作前，需要对两个序列都用operator &lt;排序，当然，也可以指定pr排序函数</div>
<div>merge(s2)</div>
<div>merge(s2,pr);</div>
<div><strong>8.reverse()</strong></div>
<div>翻转操作,把整个list翻转</div>
<div style="MARGIN: 17pt 0cm 16.5pt"><strong><span style="FONT-SIZE: x-large"><span style="COLOR: #000000"><span style="FONT-SIZE: x-small"><font size=1>deque的基本操作</font></span></span></span></strong></div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>一、原型与构造函数</font></span></span></strong></div>
<div>typedef deque&lt;T, allocator&lt;T&gt; &gt; deqObj;</div>
<div>构造函数</div>
<div>deque();</div>
<div>deque(al);</div>
<div>deque(n);</div>
<div>deque(n,x);</div>
<div>deque(n,x,al);</div>
<div>deque(first,last);</div>
<div>deque(first,last,al);</div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>二、操作</font></span></span></strong></div>
<div><strong>1.resize &amp; clear</strong></div>
<div>使用resize(n)改变大小，使用resize(n, val)如果需要用T(val) 来填满空闲值。</div>
<div>2.clear操作</div>
<div>在clear后调用deqObj.swap(deque&lt;T,A&gt;())是好习惯，而且也一定要这么做。</div>
<div>3.font(),back(),operator [],(如出边界，形为未定)at()(如出边界，抛异常),push_back(),push_front(),pop_back(),pop_front(),insert(iterator it,x),insert(iterator it,n,x),insert(iterator first,iterator last),(插入后指向刚插入的值)，erase(it),删除在it指定位置的值，erase(iterator first,iterator last)删除指定区间的值(左闭右开)。这些操作与上面的操作雷同。</div>
<div>&nbsp;</div>
<div style="MARGIN: 17pt 0cm 16.5pt"><strong><span style="FONT-SIZE: x-large"><span style="COLOR: #000000"><span style="FONT-SIZE: x-small"><font size=1>Set与multiset的基本操作</font></span></span></span></strong></div>
<div style="MARGIN: 13pt 0cm"><strong><span style="FONT-SIZE: large"><span style="FONT-SIZE: large; COLOR: #000000"><font size=4>一、原型与构造函数</font></span></span></strong></div>
<div>typedef set&lt;Key, less&lt;Key&gt;, allocator&lt;key&gt; &gt; setObj;</div>
<div>构造函数</div>
<div>set(); //空set,按pred()排序</div>
<div>set(pr); //声明一个空的按pr排序的set</div>
<div>set(pr,al); //声明一个按pr排序的集合用al分配</div>
<div>set(first,last)</div>
<div>set(first,last,pr)</div>
<div>set(first,last,pr,al)</div>
<div>操作</div>
<div>1.clear()</div>
<div>2.erase(it); erase(first, last)</div>
<div>3.insert(key),返回值为pair&lt;iterator, bool&gt; 类型，没有与插入元素相同的元素时，second为true,此时first指向新插入的元素。否则为False，first仍指向原来的元素</div>
<div>4.find(key)</div>
<div>5.lower_bound(key)</div>
<div>6.upper_bound(key)</div>
<div>7.equal_range(key),返回一个pair&lt;iterator , iterator &gt;(lower_bound(key), upper_bound(key))</div>
<div>8.count, equal_range的长度</div>
<div>9.key_comp,如果k1排在k2的前面，那么key_comp()(key1,key2)就为true</div>
<div>10.value_comp,对于set&lt;key&gt;对象，它与key_comp一样。</div>
<div>multiset</div>
<div>1.insert,由于insert总能成功，那么它返回的就是新元素的迭代器，而并非pair&lt;iteraor, bool&gt;对象.</div>
<div>2.find返回第一个与key相等的迭代器。</div>
<div>3.equal_range将返回&nbsp;[0,setObj.size())的任意长度.</div>
<div>4.count()将返回[0,setObj.size())的任意值。</div>
<img src ="http://www.cppblog.com/carlward/aggbug/102208.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-11-29 01:20 <a href="http://www.cppblog.com/carlward/articles/102208.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>玩玩vector</title><link>http://www.cppblog.com/carlward/articles/102207.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Sat, 28 Nov 2009 17:19:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/102207.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/102207.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/102207.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/102207.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/102207.html</trackback:ping><description><![CDATA[<p>转自：<a href="http://blog.csdn.net/norains/archive/2008/05/10/2431642.aspx"><font color=#336699>http://blog.csdn.net/norains/archive/2008/05/10/2431642.aspx</font></a></p>
<p>//========================================================================<br>//TITLE:<br>//&nbsp;&nbsp;&nbsp; 玩玩vector<br>//AUTHOR:<br>//&nbsp;&nbsp;&nbsp; norains<br>//DATE:<br>//&nbsp;&nbsp;&nbsp; Saturday&nbsp; 10-May-2008<br>//Environment:<br>//&nbsp;&nbsp;&nbsp; VS2005 + MIPS-SDK<br>//========================================================================</p>
<p>&nbsp;&nbsp;&nbsp; 本文不是讨论为什么要用vector替代数组，或是为什么一定要使用vector，而是在如何用vector在一些场合起到有意思的运用。本文所讨论的方式，并不是建立于推荐的原则，而仅仅是展示vector的有趣用法。在实际中是否使用，可各自掌握。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 1.内存动态分配<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 最常常用的场合，vector其实也是为此而诞生的。举个很简单的例子：<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 普通代码：<br>&nbsp;&nbsp;&nbsp; #define NEW_SIZE&nbsp; 10<br>&nbsp;&nbsp;&nbsp; int *pArr = new int[NEW_SIZE];<br>&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp; delete []pArr;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; vector替代代码：<br>&nbsp;&nbsp;&nbsp; std::vector&lt;int&gt; vct;<br>&nbsp;&nbsp;&nbsp; vct.resize(NEW_SIZE);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 好处不言而喻，采用new分配内存的话，还需要调用delete来负责释放；而如果采用vector，那么，只要分配就好。释放？不管，就让编译器操心吧！<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 2.存储字符串<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 既然数组能存储字符串，那么vector自然也不甘落后咯。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 普通代码：<br>&nbsp;&nbsp;&nbsp; WCHAR szPath[MAX_PATH];<br>&nbsp;&nbsp;&nbsp; wcscpy(szPath,L"path");<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; vector替代代码：<br>&nbsp;&nbsp;&nbsp; std::vector&lt;WCHAR&gt; vctPath(MAX_PATH);<br>&nbsp;&nbsp;&nbsp; wcscpy(&amp;vctPath[0],L"path");<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 在这里我们需要注意一点，标准C++中规定，vector的数据保存是必须在一段连续的内存中，所以我们在函数中可以使用&amp;vctPath[0]的方式。注意，这里还有一个[0]下标，因为如果没有该下标，而仅仅是&amp;vctPath，那么该地址只是对象的起始地址，而不是数据存储的起始地址。对于wcscpy函数来说，它的目标地址必须为一块连续的数据存储内存，而&amp;vctPath[0]刚好符合。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 由此我们可以得出一个结论，凡是需要传递数组起始地址的场合，如果使用vector，那么都可以采用&amp;vector[0]的方式替代。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 3.存储内存数据<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 其实这部份内容和第2点差不多，vector既然能存储字符串，那么铁定也能存储内存的数据。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 普通代码：<br>&nbsp;&nbsp;&nbsp; #define MEM_SIZE&nbsp; 20<br>&nbsp;&nbsp;&nbsp; BYTE * pMem = malloc(MEM_SIZE);<br>&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp; free(pMem);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; vector替代代码：<br>&nbsp;&nbsp;&nbsp; std::vector&lt;BYTE&gt; vct;<br>&nbsp;&nbsp;&nbsp; vct.resize(MEM_SIZE);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 在使用内存处理函数时只要采用和第二点相同的方式即可：<br>&nbsp;&nbsp;&nbsp; memcpy(&amp;vct[0],pBuf,20);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 不过本节的重点不在于如何存储内存数据，因为这可以通过第1、2点可以推断出来，而是想说明另外一个问题，采用vector可能可以起到简化函数行参的作用。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 举个例子，我们实际想写一个函数，需要转换pSource指向的内存数据，那么我们的这个函数至少需要两个参数，分别是指向分配的内存，以及该内存区域的大小：<br>&nbsp;&nbsp;&nbsp; void Convert(BYTE * pBuf, ULONG ulSize)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i = 0; i &lt; ulSize; i ++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 但如果是用vector作为形参，由于vector可以调用size()函数获取长度，所以可以减少一个形参个数：<br>&nbsp;&nbsp;&nbsp; void Convert(std::vector&lt;BYTE&gt; &amp;vect)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;for(std::vector&lt;BYTE&gt;::size_type i = 0; i &lt; vect.size(); i ++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 4.vector的一个应用示例<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 在文章的最后，举一个vector可以简化设计的例子。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 如果我们需要设计这么一个函数，输入班级的序号，然后返回班里每个人的成绩。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 因为需要存储每个人的成绩，所以采用数组来存储应该是一个很合理的选择。又因为每个班级的人数不一致，所以我们必须采用动态分配数组的方式：<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; int * GetScore(int iIndexClass,int *pLen)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int *pNew = new int[iAmount_cls_1];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *pLen = iAmount_cls_1；<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pNew;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 这会引发一个问题，首先我们需要增加一个形参，用来指示该数组的长度。当然，这还是其次，最重要的是，因为内存是动态分配的，需要手工释放。问题就出来了，内存的分配是函数内部分配，而释放需要调用者手工释放！万一调用者忘记释放了呢？最好的结果无非是内存慢慢被侵蚀，然后出现异常。而调用者如果记得释放呢？是不是就没问题了呢？错！在例子中，调用者应该是需要调用delete来释放内存；而如果万一某一天，GetScore函数的编写者不采用new方式，而是malloc，那么调用者调用delete释放内存会出现什么结果？喔，我也不知道，因为C++中未定义。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 似乎使用数组让我们走入了个两难的境地。那么，如果使用vector呢？<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 函数如下：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; std::vector&lt;int&gt; GetScore(int iIndexClass)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;vector&lt;int&gt; vct(iAmount_cls_1);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return vct;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 使用就非常简单咯：<br>&nbsp;&nbsp;&nbsp; vector&lt;int&gt; vctScore;<br>&nbsp;&nbsp;&nbsp; vctScore = GetScore(1);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 不用手工释放内存，不用增加多余的指示长度参数（因为可以调用vctScore.size()来获取），一切都那么简单，一切都那么美好，难道不是么？</p>
<img src ="http://www.cppblog.com/carlward/aggbug/102207.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-11-29 01:19 <a href="http://www.cppblog.com/carlward/articles/102207.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>std:string 与其它基本数据类型的转换</title><link>http://www.cppblog.com/carlward/articles/102206.html</link><dc:creator>Carlward</dc:creator><author>Carlward</author><pubDate>Sat, 28 Nov 2009 17:16:00 GMT</pubDate><guid>http://www.cppblog.com/carlward/articles/102206.html</guid><wfw:comment>http://www.cppblog.com/carlward/comments/102206.html</wfw:comment><comments>http://www.cppblog.com/carlward/articles/102206.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/carlward/comments/commentRss/102206.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/carlward/services/trackbacks/102206.html</trackback:ping><description><![CDATA[<p><strong><span style="FONT-SIZE: large"><font size=4>string -&gt; char*</font></span></strong></p>
<p>std::string str("12345");</p>
<p>char *pChar = NULL;</p>
<p>pChar = std::string.c_str();</p>
<p><strong><span style="FONT-SIZE: large"><font size=4>char* -&gt; string</font></span></strong></p>
<p>char* pChar = new char[10];</p>
<p>std::string str;</p>
<p>str = pChar;</p>
<p><strong><span style="FONT-SIZE: large"><font size=4>string -&gt; double</font></span></strong></p>
<p>std::string str("12345");</p>
<p>double n;</p>
<p>n = atof(str.c_str());</p>
<p><strong><span style="FONT-SIZE: large"><font size=4>int -&gt; string</font></span></strong></p>
<p>int i = 0;</p>
<p>char TempChar[100];</p>
<p>sprintf(TempChar, "int %d to string", i);</p>
<p>std::string Str(TempChar);</p>
<img src ="http://www.cppblog.com/carlward/aggbug/102206.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/carlward/" target="_blank">Carlward</a> 2009-11-29 01:16 <a href="http://www.cppblog.com/carlward/articles/102206.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>