??xml version="1.0" encoding="utf-8" standalone="yes"?>精品国产乱码久久久久久浪潮,国产成人无码精品久久久久免费,国产精品永久久久久久久久久http://www.shnenglu.com/woaidongmao/category/6861.html文章均收录自他h博客Q但不喜标题前加-[转脓]Q因其丑陋,见谅Q~zh-cnWed, 02 Sep 2009 21:11:19 GMTWed, 02 Sep 2009 21:11:19 GMT60使用std::vector 的陷?/title><link>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95146.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Wed, 02 Sep 2009 14:36:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95146.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/95146.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95146.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/95146.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/95146.html</trackback:ping><description><![CDATA[<p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span style="color: black; mso-bidi-font-family: arial">在?span lang="EN-US">std</span>的容器的时?span lang="EN-US">,</span>不少人喜Ƣ用<span lang="EN-US">vector, </span>因ؓ比v<span lang="EN-US">list,</span>更省I间<span lang="EN-US">,</span>而且可以Ҏ<span lang="EN-US">index</span>直接d某个?span lang="EN-US">,</span>而不用一个个枚D来取<span lang="EN-US">.<?xml:namespace prefix = o /><o:p></o:p></span></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span style="color: black; mso-bidi-font-family: arial">但是<span lang="EN-US">,std::vector</span>实有一些值得注意的陷?span lang="EN-US">, </span>q里先说其中一?span lang="EN-US">, </span>L以下代码<span lang="EN-US">.<o:p></o:p></span></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">std::vector< int >  values;<o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">values.push_back(1);<o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">values.push_back(2);<o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">values.push_back(3);<o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">values.erase(values.begin() + 1);<o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span style="color: black; mso-bidi-font-family: arial">乍看之下<span lang="EN-US">,</span>q几行简单的代码没什?问题<span lang="EN-US">. </span>实际执行h<span lang="EN-US">, </span>q是没什么问?span lang="EN-US"> , </span>但却有一个陷?span lang="EN-US">. </span>׃例子里面用的?span lang="EN-US">int</span>?span lang="EN-US">vector,</span>所以这样做没有M问题<span lang="EN-US">, </span>?span lang="EN-US">,</span>假如不是一?span lang="EN-US">int, </span>而是一个类<span lang="EN-US">,</span>一个结构体<span lang="EN-US">,</span>cLl构体里面还有指?span lang="EN-US">, </span>那就很可能出问题?span lang="EN-US">. why?<o:p></o:p></span></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span style="color: black; mso-bidi-font-family: arial">因ؓ<span lang="EN-US">vector</span>不象<span lang="EN-US">list,vector</span>始终要保持一个完整的内存l构<span lang="EN-US">(</span>因ؓ是一个数l?span lang="EN-US">),</span>q样才可以让<span lang="EN-US">values[1]</span>q样的方式正运?span lang="EN-US">. </span>但是<span lang="EN-US">,</span>如果要在<span lang="EN-US">vector</span>中间删掉一个成员的?span lang="EN-US">,vector</span>是这样做?span lang="EN-US">, </span>先把该成员后面的一个成?span lang="EN-US">,</span>一直到最后一个成员往前一位置拯<span lang="EN-US">,</span>q样需要删除的成员已经被后面的覆盖?span lang="EN-US">, </span>然后再删除最后一个成?span lang="EN-US">,</span>q样<span lang="EN-US">,vector</span>又能保持一D完整的内存l构?span lang="EN-US">.  </span>注意<span lang="EN-US">,</span>因ؓ最后一个成员会被删?span lang="EN-US">,</span>而如果这个成员里面有一个成员变量是指针<span lang="EN-US">, </span>那析构函数很有可能会把这个指针指向的地方释放?span lang="EN-US">!  </span>q样<span lang="EN-US">,</span>即最后一个成员被复制了一?span lang="EN-US"> </span>到倒数W?span lang="EN-US">2</span>的位|?span lang="EN-US">,</span>也因为在他本w被删除的时?span lang="EN-US">,</span>把倒数W?span lang="EN-US">2</span>?span lang="EN-US">(</span>也就是它的复?span lang="EN-US">) </span>的指针成员所指向的地方给释放?span lang="EN-US">! </span>如图Q?span lang="EN-US"><o:p></o:p></span></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 0pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span lang="EN-US" style="color: black; mso-bidi-font-family: arial">                     <span id="oa2u4si" class="apple-converted-space"> </span><a ><span style="color: #0054b3; text-decoration: none; text-underline: none"><span style="mso-ignore: vglayout"><img class="alignnone size-full wp-image-1468" title="clip_image002" style="border-right: 0px; padding-right: 0px; border-top: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; border-left: 0px; padding-top: 0px; border-bottom: 0px; font-family: arial, helvetica, sans-serif; oldpaddingtop: 0px; oldbordertopwidth: ; oldpaddingbottom: 0px; oldborderbottomwidth: ; oldpaddingright: 0px; oldborderrightwidth: ; oldpaddingleft: 0px; oldborderleftwidth: " height="206" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stdvector_13DCB/clip_image002_0faed496-68b4-4907-a855-209a8dfc2c1e.jpg" width="322" border="0" v:shapes="_x0000_i1025" mergenum="0"></span></span></a><o:p></o:p></span></p> <p style="text-justify: inter-ideograph; background: white; margin: 0cm 0cm 9pt; word-break: break-all; text-indent: 21pt; line-height: 150%; text-align: justify"><span style="color: black; mso-bidi-font-family: arial">解决的办法也很简?span lang="EN-US">, </span>最有<span lang="EN-US">2</span>U?span lang="EN-US">. 1,  </span>增加作ؓ<span lang="EN-US">vector</span>cd的类的拷贝构造函?span lang="EN-US">, </span>因ؓ<span lang="EN-US">vector</span>?span lang="EN-US">erase</span>的时候会发生一ơ拷?span lang="EN-US">,</span>让拷贝构造函C单单是复制指?span lang="EN-US">,</span>q把指针所指向的内容给拯一?span lang="EN-US">,</span>q样׃会导致被最后一个成员释攄时候一起释放掉?span lang="EN-US">. 2, </span>如果有引用记数的?span lang="EN-US">,</span>如智能指?span lang="EN-US">, </span>׃会被释放掉了。不q如果一般编码里面不需要用到引用记数的?span lang="EN-US">,</span>q是Ҏ<span lang="EN-US">1</span>比较?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="line-height: 150%"><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial"><o:p> </o:p></span></p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/95146.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2009-09-02 22:36 <a href="http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95146.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于STL容器存储对象的问?/title><link>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95144.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Wed, 02 Sep 2009 14:23:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95144.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/95144.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95144.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/95144.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/95144.html</trackback:ping><description><![CDATA[<p><font size="4">请问如果我在STL容器存储的是对象Q那么我在调用clear()或eara()的时候,会自动释放这个对象的内存I间吗?会执行该对象的析够函数吗Q?/font></p> <p><font size="4">=======================</font></p> <p><font size="4">会,析构函数也会自动调用?nbsp;  <br>  但如果存储的是对象的指针Q外部对象如果是在堆上分配的Q需要我们自已delete</font> <p><font size="4">=======================</font> <p><font size="4">STL的容器是Z<font color="#ff0000">by   value语意的。当你把一个元素放入容器中Q在容器中存攄实际上是q个元素的一个副本(q就是ؓ什么STL容器要求元素<strong><u>必须可以拯构造和赋?/u></strong>Q,</font>副本所占的内存是STL容器自己分配的,<font color="#ff0000">所以它会自己回收这些内存,同时调用元素的析构函数?/font>   <br>  但是如果你把指针攑ֈ容器中,clear的时候容器只负责回收指针本n所占的内存Q至于指针所指向的东西,它是不管?/font></p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/95144.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2009-09-02 22:23 <a href="http://www.shnenglu.com/woaidongmao/archive/2009/09/02/95144.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL 4Q?STL之容器:选择时机Q删除元素,q代器失?/title><link>http://www.shnenglu.com/woaidongmao/archive/2009/08/31/94925.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Mon, 31 Aug 2009 15:08:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2009/08/31/94925.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/94925.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2009/08/31/94925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/94925.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/94925.html</trackback:ping><description><![CDATA[<p class="MsoNormal" style="background: white; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">  </span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">一<span lang="EN-US">. </span>U类Q?span lang="EN-US"><?xml:namespace prefix = o /><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; margin-left: 32.25pt; layout-grid-mode: char; word-break: break-all; text-indent: -18pt; line-height: 150%; text-align: left; mso-pagination: widow-orphan; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; line-height: 150%; font-family: symbol; mso-bidi-font-family: symbol; mso-font-kerning: 0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: symbol"><span style="mso-list: ignore">·<span style="font: 7pt 'Times New Roman'">            </span></span></span><b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">标准<span lang="EN-US">STL</span>序列容器</span></b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Q?span lang="EN-US">vector</span>?span lang="EN-US">string</span>?span lang="EN-US">deque</span>?span lang="EN-US">list</span>?<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; margin-left: 32.25pt; layout-grid-mode: char; word-break: break-all; text-indent: -18pt; line-height: 150%; text-align: left; mso-pagination: widow-orphan; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; line-height: 150%; font-family: symbol; mso-bidi-font-family: symbol; mso-font-kerning: 0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: symbol"><span style="mso-list: ignore">·<span style="font: 7pt 'Times New Roman'">            </span></span></span><b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">标准<span lang="EN-US">STL</span>兌容器</span></b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Q?span lang="EN-US">set</span>?span lang="EN-US">multiset</span>?span lang="EN-US">map</span>?span lang="EN-US">multimap</span>?<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; margin-left: 32.25pt; layout-grid-mode: char; word-break: break-all; text-indent: -18pt; line-height: 150%; text-align: left; mso-pagination: widow-orphan; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; line-height: 150%; font-family: symbol; mso-bidi-font-family: symbol; mso-font-kerning: 0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: symbol"><span style="mso-list: ignore">·<span style="font: 7pt 'Times New Roman'">            </span></span></span><b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">非标准序列容?/span></b><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">slist</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">?span lang="EN-US">rope</span>?span lang="EN-US">slist</span>是一个单向链表,<span lang="EN-US">rope</span>本质上是一个重型字W串 <span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; margin-left: 32.25pt; layout-grid-mode: char; word-break: break-all; text-indent: -18pt; line-height: 150%; text-align: left; mso-pagination: widow-orphan; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; line-height: 150%; font-family: symbol; mso-bidi-font-family: symbol; mso-font-kerning: 0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: symbol"><span style="mso-list: ignore">·<span style="font: 7pt 'Times New Roman'">            </span></span></span><b><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">非标准关联容?/span></b><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">hash_set</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">?span lang="EN-US">hash_multiset</span>?span lang="EN-US">hash_map</span>?span lang="EN-US">hash_multimap</span>?<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; margin-left: 32.25pt; layout-grid-mode: char; word-break: break-all; text-indent: -18pt; line-height: 150%; text-align: left; mso-pagination: widow-orphan; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt" align="left"><span lang="EN-US" style="font-size: 10pt; color: black; line-height: 150%; font-family: symbol; mso-bidi-font-family: symbol; mso-font-kerning: 0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: symbol"><span style="mso-list: ignore">·<span style="font: 7pt 'Times New Roman'">            </span></span></span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">几种<b>标准?span lang="EN-US">STL</span>容器</b>Q包括数l?span lang="EN-US">bitset</span>?span lang="EN-US">valarray</span>?span lang="EN-US">stack</span>?span lang="EN-US">queue</span>?span lang="EN-US">priority_queue <o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; background: red; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">值得注意的是Q数l可以和<span lang="EN-US">STL</span>法配合Q因为指针可以当作数l的q代器?/span><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">?/span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">  </span><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">?span lang="EN-US">.</span>删除元素 <span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">如果惛_除东西,C<span lang="EN-US">remove</span>法后,要加?span lang="EN-US">erase<o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">所谓删除算法,最l还是要调用成员函数d除某个元素,但是因ؓ<span lang="EN-US">remove</span>q不知道它现在作用于哪个容器Q所?span lang="EN-US">remove</span>法不可能真的删除一个元?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">1.Vector<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">vector<int> v;   <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">v.reserve(10);   <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">for (int i = 1; i <= 10; ++i) {<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> v.push_back(i);<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">}<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">cout << v.size();   // 10<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">v[3] = v[5] = v[9] = 99; <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">remove(v.begin(), v.end(), 99); <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">//v.erase(remove(v.begin(),v.end(),99),v.end());<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">cout << v.size();   // 10</span><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">Q?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">2. list<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">list<int> listTest;<o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">listTest.remove(99);//</span><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">q个成员函数真的删除元素,q且要比<span lang="EN-US">erase+remove</span>高效<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">remove</span><span style="font-size: 12pt; background: white; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">?span lang="EN-US">remove_if</span>之间的十分相伹{但<span lang="EN-US">unique</span>行ؓ也像<span lang="EN-US">remove</span>。它用来从一个区间删除东西(邻近的重复|而不用访问持有区间元素的容器。如果你真的要从容器中删除元素,你也必须成对调用<span lang="EN-US">unique</span>?span lang="EN-US">erase</span>Q?span lang="EN-US">unique</span>?span lang="EN-US">list</span>中也cM?span lang="EN-US">remove</span>。正?span lang="EN-US">list::remove</span>真的删除东西Q而且?span lang="EN-US">erase-remove</span>惯用法高效得多)?span lang="EN-US">list::unique</span>也真的删除邻q的重复|也比<span lang="EN-US">erase-unique</span>高效Q?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">?q代器失效:</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">  <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">一个网友提的问题:<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">void   main() <br>{ <br>vector <string> vcs; <br>vcs.push_back( "this   is   A "); <br>vector <string   > ::iterator   it=vcs.begin(); <br>int   i=9; <br>for(;it!=vcs.end();++it) <br>{ <br>cout < < "caplity   of   vector   is   :   " < <vcs.size() < <endl; <br><br>cout < < "---> " < <*it < <endl;   //</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">L此句会有一个超q?span lang="EN-US">vector <br>                                   //</span>大小的@环,高手能解释一下ؓ什?span lang="EN-US">? <br>if(i==9) <br>{ <br>vcs.push_back( "this   is   BBBBB "); <br>cout < < "vcs.push! " < <endl; <br>} <br>i=8; <br>} <br>}<o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; background: red; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">典型的P代器失效<span lang="EN-US">....</span></span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">  <o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: red; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">vector</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">Q?span lang="EN-US"><br>1.</span>当插入(<span lang="EN-US">push_back</span>Q一个元素后Q?span lang="EN-US">end</span>操作q回的P代器肯定失效?span lang="EN-US"><br>2.</span>当插?span lang="EN-US">(push_back)</span>一个元素后Q?span lang="EN-US">capacity</span>q回g没有插入元素之前相比有改变,则需要重新加载整个容器,此时<span lang="EN-US">first</span>?span lang="EN-US">end</span>操作q回的P代器都会失效?span lang="EN-US"><br>3.</span>当进行删除操作(<span lang="EN-US">erase</span>Q?span lang="EN-US">pop_back</span>Q后Q指向删除点的P代器全部失效Q指向删除点后面的元素的q代器也全部失效?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: red; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">deque</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">q代器的失效情况Q?span lang="EN-US"><br>1.</span>?span lang="EN-US">deque</span>容器首部或者尾部插入元素不会得Q何P代器失效?span lang="EN-US"><br>2.</span>在其首部或尾部删除元素则只会使指向被删除元素的P代器失效?span lang="EN-US"><br>3.</span>?span lang="EN-US">deque</span>容器的Q何其他位|的插入和删除操作将使指向该容器元素的所有P代器失效?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; background: red; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">List/set/map</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">1.</span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">删除Ӟ指向该删除节点的q代器失?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">list<int> intList; <br>list<int>::iterator it = intList.begin(); <br>while(it != intList.end()) <br>{ <br>it = intList.erase(it); <br></span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">…?span lang="EN-US"> <br>}<o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">?span lang="EN-US">.</span>选择时机<span lang="EN-US"><</span>?span lang="EN-US">>--</span>ȝ各种容器特点<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: white; layout-grid-mode: char; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">(1) vector<br></span><span style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: arial; mso-font-kerning: 0pt">内部数据l构Q数l?span lang="EN-US"><br></span>随机讉K每个元素Q所需要的旉为常量?span lang="EN-US"><br></span>在末֢加或删除元素所需旉与元素数目无养I在中间或开头增加或删除元素所需旉随元素数目呈U性变化?span lang="EN-US"><br></span>可动态增加或减少元素Q内存管理自动完成,但程序员可以使用<span lang="EN-US">reserve()</span>成员函数来管理内存?span lang="EN-US"><br>vector</span>的P代器在内存重新分配时失效(它所指向的元素在该操作的前后不再相同Q。当把超q?span lang="EN-US">capacity()-size()</span>个元素插?span lang="EN-US">vector</span>中时Q内存会重新分配Q所有的q代器都失效;否则Q指向当前元素以后的M元素的P代器都将失效。当删除元素Ӟ指向被删除元素以后的M元素的P代器都将失效?span lang="EN-US"><br><br>(2)deque<br></span>内部数据l构Q数l?span lang="EN-US"><br></span>随机讉K每个元素Q所需要的旉为常量?span lang="EN-US"><br></span>在开头和末尾增加元素所需旉与元素数目无养I在中间增加或删除元素所需旉随元素数目呈U性变化?span lang="EN-US"><br></span>可动态增加或减少元素Q内存管理自动完成,不提供用于内存管理的成员函数?span lang="EN-US"><br></span>增加M元素都将?span lang="EN-US">deque</span>的P代器失效。在<span lang="EN-US">deque</span>的中间删除元素将使P代器失效。在<span lang="EN-US">deque</span>的头或尾删除元素Ӟ只有指向该元素的q代器失效?span lang="EN-US"><br><br>(3)list<br></span>内部数据l构Q双向环犉表?span lang="EN-US"><br></span>不能随机讉K一个元素?span lang="EN-US"><br></span>可双向遍历?span lang="EN-US"><br></span>在开头、末֒中间M地方增加或删除元素所需旉都ؓ帔R?span lang="EN-US"><br></span>可动态增加或减少元素Q内存管理自动完成?span lang="EN-US"><br></span>增加M元素都不会ɘq代器失效。删除元素时Q除了指向当前被删除元素的P代器外,其它q代器都不会失效?span lang="EN-US"><br><br>(4)slist<br></span>内部数据l构Q单向链表?span lang="EN-US"><br></span>不可双向遍历Q只能从前到后地遍历?span lang="EN-US"><br></span>其它的特性同<span lang="EN-US">list</span>怼?span lang="EN-US"><br><br>(5)stack<br></span>适配器,它可以将Lcd的序列容器{换ؓ一个堆栈,一般?span lang="EN-US">deque</span>作ؓ支持的序列容器?span lang="EN-US"><br></span>元素只能后进先出Q?span lang="EN-US">LIFO</span>Q?span lang="EN-US"><br></span>不能遍历整个<span lang="EN-US">stack</span>?span lang="EN-US"><br><br>(6)queue<br></span>适配器,它可以将Lcd的序列容器{换ؓ一个队列,一般?span lang="EN-US">deque</span>作ؓ支持的序列容器?span lang="EN-US"><br></span>元素只能先进先出Q?span lang="EN-US">FIFO</span>Q?span lang="EN-US"><br></span>不能遍历整个<span lang="EN-US">queue</span>?span lang="EN-US"><br><br>(7)priority_queue<br></span>适配器,它可以将Lcd的序列容器{换ؓ一个优先队列Q一般?span lang="EN-US">vector</span>作ؓ底层存储方式?span lang="EN-US"><br></span>只能讉KW一个元素,不能遍历整个<span lang="EN-US">priority_queue</span>?span lang="EN-US"><br></span>W一个元素始l是优先U最高的一个元素?span lang="EN-US"><br><br>(8)set<br></span>键和值相{?span lang="EN-US"><br></span>键唯一?span lang="EN-US"><br></span>元素默认按升序排列?span lang="EN-US"><br></span>如果q代器所指向的元素被删除Q则该P代器失效。其它Q何增加、删除元素的操作都不会ɘq代器失效?span lang="EN-US"><br><br>(9)multiset<br></span>键可以不唯一?span lang="EN-US"><br></span>其它特点?span lang="EN-US">set</span>相同?span lang="EN-US"><br><br>(10)hash_set<br></span>?span lang="EN-US">set</span>相比较,它里面的元素不一定是l过排序的,而是按照所用的<span lang="EN-US">hash</span>函数分派的,它能提供更快的搜索速度Q当然跟<span lang="EN-US">hash</span>函数有关Q?span lang="EN-US"><br></span>其它特点?span lang="EN-US">set</span>相同?span lang="EN-US"><br><br>(11)hash_multiset<br></span>键可以不唯一?span lang="EN-US"><br></span>其它特点?span lang="EN-US">hash_set</span>相同?span lang="EN-US"><br><br>(12)map<br></span>键唯一?span lang="EN-US"><br></span>元素默认按键的升序排列?span lang="EN-US"><br></span>如果q代器所指向的元素被删除Q则该P代器失效。其它Q何增加、删除元素的操作都不会ɘq代器失效?span lang="EN-US"><br><br>(13)multimap<br></span>键可以不唯一?span lang="EN-US"><br></span>其它特点?span lang="EN-US">map</span>相同?span lang="EN-US"><br><br>(14)hash_map<br></span>?span lang="EN-US">map</span>相比较,它里面的元素不一定是按键值排序的Q而是按照所用的<span lang="EN-US">hash</span>函数分派的,它能提供更快的搜索速度Q当然也?span lang="EN-US">hash</span>函数有关Q?span lang="EN-US"><br></span>其它特点?span lang="EN-US">map</span>相同?span lang="EN-US"><br><br>(15)hash_multimap<br></span>键可以不唯一?span lang="EN-US"><br></span>其它特点?span lang="EN-US">hash_map</span>相同?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; color: black; font-family: 宋体; mso-bidi-font-family: arial"><o:p> </o:p></span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; color: black; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p> </o:p></span></p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/94925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2009-08-31 23:08 <a href="http://www.shnenglu.com/woaidongmao/archive/2009/08/31/94925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STLPORT的bughttp://www.shnenglu.com/woaidongmao/archive/2009/07/16/90254.html肥仔肥仔Thu, 16 Jul 2009 08:52:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2009/07/16/90254.htmlhttp://www.shnenglu.com/woaidongmao/comments/90254.htmlhttp://www.shnenglu.com/woaidongmao/archive/2009/07/16/90254.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/90254.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/90254.html当采用静态连接,定义?,_STLP_USE_STATIC_LIB

string的下面红色代码出错,采用动态库Q却没有发现q个问题

template <bool __threads, int __inst>
void* _STLP_CALL
__node_alloc<__threads, __inst>::_M_allocate(size_t __n) {
  void*  __r;
  _Obj * _STLP_VOLATILE * __my_free_list = _S_free_list + _S_FREELIST_INDEX(__n);
  // #       ifdef _STLP_THREADS
  /*REFERENCED*/
  _Node_Alloc_Lock<__threads, __inst> __lock_instance;
  // #       endif
  // Acquire the lock here with a constructor call.
  // This ensures that it is released in exit or during stack
  // unwinding.
  if ( (__r  = *__my_free_list) != 0 ) {
    *__my_free_list = ((_Obj*)__r) -> _M_free_list_link;
  } else {
    __r = _S_refill(__n);
  }

 

解决办法Q?/p>

使用动态库的STLPORT



肥仔 2009-07-16 16:52 发表评论
]]>
关于stl q代器失?/title><link>http://www.shnenglu.com/woaidongmao/archive/2009/07/09/89653.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Thu, 09 Jul 2009 06:48:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2009/07/09/89653.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/89653.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2009/07/09/89653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/89653.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/89653.html</trackback:ping><description><![CDATA[<p class="MsoNormal" style="line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">先看两条规制Q?span lang="EN-US"><br><br><span style="color: red">1</span></span><span style="color: red">Q对于节点式容器<span lang="EN-US">(map, list, set)</span>元素的删除,插入操作会导致指向该元素的P代器失效Q其他元素P代器不受影响<span lang="EN-US"><br><br>2</span>Q对于顺序式容器<span lang="EN-US">(vector)</span>元素的删除、插入操作会D指向该元素以及后面的元素的P代器失效</span><span lang="EN-US"><br><br></span>常见的错误代码示例:<span lang="EN-US"><?xml:namespace prefix = o /><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 1</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_74.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_24.gif" width="11" border="0" v:shapes="_x0000_i1025"></a><span style="color: blue">struct</span><span style="color: black"> sMem <br></span><span style="color: teal"> 2</span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_42.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_12.gif" width="19" border="0" v:shapes="_x0000_i1026"></a></span><span style="color: black">{<br></span><span style="color: teal"> 3</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_50.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_14.gif" width="11" border="0" v:shapes="_x0000_i1027"></a>    </span><span style="color: blue">int</span><span style="color: black"> m_i;<br></span><span style="color: teal"> 4</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_52.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_15.gif" width="11" border="0" v:shapes="_x0000_i1028"></a>    sMem(</span><span style="color: blue">int</span><span style="color: black"> i = 10)<br></span><span style="color: teal"> 5</span><span style="color: black">    </span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_44.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_13.gif" width="19" border="0" v:shapes="_x0000_i1029"></a></span><span style="color: black">{<br></span><span style="color: teal"> 6</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_54.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_16.gif" width="11" border="0" v:shapes="_x0000_i1030"></a>        m_i = i;<br></span><span style="color: teal"> 7</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_30.gif"><img title="clip_image004" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image004" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_thumb_11.gif" width="11" border="0" v:shapes="_x0000_i1031"></a>    }<br></span><span style="color: teal"> 8</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_56.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_17.gif" width="11" border="0" v:shapes="_x0000_i1032"></a><br></span><span style="color: teal"> 9</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_58.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_18.gif" width="11" border="0" v:shapes="_x0000_i1033"></a>    </span><span style="color: blue">int</span><span style="color: black"> GetI()<br></span><span style="color: teal">10</span><span style="color: black">    </span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_46.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_14.gif" width="19" border="0" v:shapes="_x0000_i1034"></a></span><span style="color: black">{<br></span><span style="color: teal">11</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_60.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_19.gif" width="11" border="0" v:shapes="_x0000_i1035"></a>        </span><span style="color: blue">return</span><span style="color: black"> m_i;<br></span><span style="color: teal">12</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_32.gif"><img title="clip_image004" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image004" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_thumb_12.gif" width="11" border="0" v:shapes="_x0000_i1036"></a>    }<br></span><span style="color: teal">13</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image005_42.gif"><img title="clip_image005" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image005" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image005_thumb_18.gif" width="11" border="0" v:shapes="_x0000_i1037"></a>};</span><o:p></o:p></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">1</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_76.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_25.gif" width="11" border="0" v:shapes="_x0000_i1038"></a><span style="color: black"><br></span><span style="color: teal">2</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_78.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_26.gif" width="11" border="0" v:shapes="_x0000_i1039"></a>typedef vector<sMem*> sMemList;<br></span><span style="color: teal">3</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_80.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_27.gif" width="11" border="0" v:shapes="_x0000_i1040"></a>typedef vector<sMem*>::iterator sIT;<br></span><span style="color: teal">4</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_82.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_28.gif" width="11" border="0" v:shapes="_x0000_i1041"></a>sMemList MemList;</span><o:p></o:p></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 1</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_84.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_29.gif" width="11" border="0" v:shapes="_x0000_i1042"></a><span style="color: black">    </span><span style="color: green">// </span></span><span style="font-size: 12pt; color: green; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">q代器失?span lang="EN-US"><br></span></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 2</span><span lang="EN-US" style="font-size: 12pt; color: green; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_86.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_30.gif" width="11" border="0" v:shapes="_x0000_i1043"></a></span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">    </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">for</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> (sIT it = MemList.begin(); it != MemList.end(); ++it)<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 3</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">    </span><span lang="EN-US" style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; font-size: 12pt; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; line-height: 150%; padding-top: 0cm; border-bottom: gray 1pt solid; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_48.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_15.gif" width="19" border="0" v:shapes="_x0000_i1044"></a></span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">{<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 4</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_62.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_20.gif" width="11" border="0" v:shapes="_x0000_i1045"></a>        </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">if</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> ((*it)->GetI() == 10)<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 5</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_64.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_21.gif" width="11" border="0" v:shapes="_x0000_i1046"></a>            MemList.erase(it);<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 6</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_66.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_22.gif" width="11" border="0" v:shapes="_x0000_i1047"></a>        </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">else</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 7</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">        </span><span lang="EN-US" style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; font-size: 12pt; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; line-height: 150%; padding-top: 0cm; border-bottom: gray 1pt solid; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_50.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_16.gif" width="19" border="0" v:shapes="_x0000_i1048"></a></span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">{<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 8</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_68.gif"><img title="clip_image003" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image003" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image003_thumb_23.gif" width="11" border="0" v:shapes="_x0000_i1049"></a>            printf("%d:%d\n", i++, (*it)->GetI());<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 9</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_34.gif"><img title="clip_image004" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image004" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image004_thumb_13.gif" width="11" border="0" v:shapes="_x0000_i1050"></a>        }<br></span><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">10</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image005_44.gif"><img title="clip_image005" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image005" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image005_thumb_19.gif" width="11" border="0" v:shapes="_x0000_i1051"></a>    }</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><br></span><span style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">两种解决ҎQ?span lang="EN-US"><br></span>对于序容器Q?<span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_88.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_31.gif" width="11" border="0" v:shapes="_x0000_i1052"></a><span style="color: black"><br><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_90.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_32.gif" width="11" border="0" v:shapes="_x0000_i1053"></a></span><span style="color: blue">bool</span><span style="color: black"> Equal10(sMem* pMem)<br></span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_52.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_17.gif" width="19" border="0" v:shapes="_x0000_i1054"></a></span><span style="color: black">{<o:p></o:p></span></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">    </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">return</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> pMem->GetI() == 10 ? </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">true</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> : </span><span lang="EN-US" style="font-size: 12pt; color: blue; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">false</span><span lang="EN-US" style="font-size: 12pt; color: black; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">;<br>}</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">1</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_92.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_33.gif" width="11" border="0" v:shapes="_x0000_i1055"></a><span style="color: black">MemList.erase(remove_if(MemList.begin(), MemList.end(), Equal10));</span><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><br></span><span style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">对于l点容器Q?span lang="EN-US"><o:p></o:p></span></span></p> <p class="MsoNormal" style="background: #eeeeee; word-break: break-all; line-height: 150%; text-align: left; mso-pagination: widow-orphan" align="left"><span lang="EN-US" style="font-size: 12pt; color: teal; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"> 1</span><span lang="EN-US" style="font-size: 12pt; line-height: 150%; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_94.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_34.gif" width="11" border="0" v:shapes="_x0000_i1056"></a><span style="color: black">    </span><span style="color: blue">for</span><span style="color: black"> (sIT it = MemList.begin();<br></span><span style="color: teal"> 2</span><span style="color: black"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_96.gif"><img title="clip_image001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="16" alt="clip_image001" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image001_thumb_35.gif" width="11" border="0" v:shapes="_x0000_i1057"></a>        it != MemList.end(); )<br></span><span style="color: teal"> 3</span><span style="color: black">    </span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_54.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_18.gif" width="19" border="0" v:shapes="_x0000_i1058"></a></span><span style="color: black">{<br></span><span style="color: teal"> 4</span><span style="color: black">        </span><span style="color: blue">if</span><span style="color: black"> ((*it).second->GetI() == 10)<br></span><span style="color: teal"> 5</span><span style="color: black">            MemList.erase(it++);<br></span><span style="color: teal"> 6</span><span style="color: black">        </span><span style="color: blue">else</span><span style="color: black"><br></span><span style="color: teal"> 7</span><span style="color: black">        </span><span style="border-right: gray 1pt solid; padding-right: 0cm; border-top: gray 1pt solid; display: none; padding-left: 0cm; background: white; padding-bottom: 0cm; border-left: gray 1pt solid; padding-top: 0cm; border-bottom: gray 1pt solid; mso-hide: all; mso-border-alt: solid gray .75pt"><a href="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_56.gif"><img title="clip_image002" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="24" alt="clip_image002" src="http://www.shnenglu.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/stl_D009/clip_image002_thumb_19.gif" width="19" border="0" v:shapes="_x0000_i1059"></a></span><span style="color: black">{<br></span><span style="color: teal"> 8</span><span style="color: black">            printf("%d:%d\n", i++, (*it).second->GetI());<br></span><span style="color: teal"> 9</span><span style="color: black">            it++;<br></span><span style="color: teal">10</span><span style="color: black">        }<br></span><span style="color: teal">11</span><span style="color: black">    }</span><o:p></o:p></span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: arial"><o:p> </o:p></span></p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/89653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2009-07-09 14:48 <a href="http://www.shnenglu.com/woaidongmao/archive/2009/07/09/89653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>stlport的link errorhttp://www.shnenglu.com/woaidongmao/archive/2009/05/18/83275.html肥仔肥仔Mon, 18 May 2009 06:32:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2009/05/18/83275.htmlhttp://www.shnenglu.com/woaidongmao/comments/83275.htmlhttp://www.shnenglu.com/woaidongmao/archive/2009/05/18/83275.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/83275.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/83275.html
一下几个步骤组合会引发q个问题
1、在stdafx.h中定义了“_STLP_USE_STATIC_LIB”Q用静态库q接Q?br>2、但是的有cpp文g使用了STLPortQ但是设|ؓ不用头文gQ也是没有#include"stdafx.h"Q?br>
解决办法Q?br>?#8220;_STLP_USE_STATIC_LIB”的定义放?Project Settings里面?/p>

肥仔 2009-05-18 14:32 发表评论
]]>
STL 仿函数列?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/11/09/66384.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Sat, 08 Nov 2008 17:14:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/11/09/66384.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/66384.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/11/09/66384.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/66384.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/66384.html</trackback:ping><description><![CDATA[<table class="MsoNormalTable" style="mso-cellspacing: 1.5pt" cellpadding="0" border="0"> <tbody> <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p><span lang="EN-US"><?xml:namespace prefix = o /><o:p> </o:p></span></p></td></tr> <tr style="mso-yfti-irow: 1"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #dadada; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal" style="text-align: center" align="center"><b><span lang="EN-US" style="font-size: 12pt; font-family: 宋体"><a title="Sort by this column" ><span lang="EN-US" style="color: black"><span lang="EN-US">名称</span></span><span lang="EN-US" style="color: black"><span lang="EN-US"> </span></span></a></span></b><b><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></b></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #dadada; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal" style="text-align: center" align="center"><b><span lang="EN-US" style="font-size: 12pt; font-family: 宋体"><a title="Sort by this column" ><span lang="EN-US" style="color: black"><span lang="EN-US">功能描述</span></span><span lang="EN-US" style="color: black"><span lang="EN-US"> </span></span></a></span></b><b><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></b></p></td></tr> <tr style="mso-yfti-irow: 2"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">equal_to</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">相等</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr style="mso-yfti-irow: 3"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">not_equal_to</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">不相{?/span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr style="mso-yfti-irow: 4"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">less</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">于</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr style="mso-yfti-irow: 5"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">greater</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">大于</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr style="mso-yfti-irow: 6"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">less_equal</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: white; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">于{于</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr style="mso-yfti-irow: 7; mso-yfti-lastrow: yes"> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt" colspan="2"> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体">greater_equal</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td> <td style="padding-right: 0.75pt; padding-left: 0.75pt; background: #eaeaea; padding-bottom: 0.75pt; padding-top: 0.75pt"> <p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体">大于{于</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: 宋体"><o:p></o:p></span></p></td></tr> <tr height="0"> <td style="border-right: medium none; border-top: medium none; border-left: medium none; border-bottom: medium none" width="67"></td> <td style="border-right: medium none; border-top: medium none; border-left: medium none; border-bottom: medium none" width="42"></td> <td style="border-right: medium none; border-top: medium none; border-left: medium none; border-bottom: medium none" width="73"></td></tr></tbody></table> <p>实例Q?span lang="EN-US"><br>//less<int>()</span>默认排序Q-升序<span lang="EN-US"><br>//greater<int>()<br>vector < <span style="color: brown">int</span> > vect;<span style="color: green"><br>//...</span><br>sort(vect.begin(), vect.end());<span style="color: green"><br>//</span></span><span style="color: green">此时相当于调?/span><span lang="EN-US"><br>sort(vect.begin(), vect.end(), less<<span style="color: brown">int</span>>() );<o:p></o:p></span></p> <p class="MsoNormal"><span lang="EN-US" style="font-size: 12pt; font-family: 宋体"><a >http://www.stlchina.org/twiki/bin/view.pl/Main/STLSortAlgorithms?sortcol=0;table=2;up=0#sorted_table</a></span><span lang="EN-US" style="font-size: 12pt; font-family: 宋体; mso-bidi-font-family: arial"><o:p></o:p></span></p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/66384.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-11-09 01:14 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/11/09/66384.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>std::find用法错误记录http://www.shnenglu.com/woaidongmao/archive/2008/11/06/66130.html肥仔肥仔Thu, 06 Nov 2008 07:33:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/11/06/66130.htmlhttp://www.shnenglu.com/woaidongmao/comments/66130.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/11/06/66130.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/66130.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/66130.html//---- std::findq回的是扑ֈ的P代器位置Q如果没有找刎ͼ会返回last iterQ其他算法类?/p>

int data[32] = {0};

int* pFind = NULL;

if(pFind = std::find_if(data, data + sizeof(data)/sizeof(int), cmp_fun))//---判断扑ֈ?/p>

 

//---- 正确的是

if((data + sizeof(data)/sizeof(int)) != (pFind = std::find_if(data, data + sizeof(data)/sizeof(int), cmp_fun)))//---判断扑ֈ?/p>

肥仔 2008-11-06 15:33 发表评论
]]>
STLPort 的预定义http://www.shnenglu.com/woaidongmao/archive/2008/10/20/64468.html肥仔肥仔Mon, 20 Oct 2008 02:16:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/10/20/64468.htmlhttp://www.shnenglu.com/woaidongmao/comments/64468.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/10/20/64468.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/64468.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/64468.html 

一、用静态连接,否则需要stlport的dll

#define _STLP_USE_STATIC_LIB

攑ֈALT+F7->C++->Preprocessor definitions里面卛_QDEBUG和RELEASE都改Q?/p>

肥仔 2008-10-20 10:16 发表评论
]]>
Boost Serialization 库教E?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/10/19/64345.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Sat, 18 Oct 2008 16:38:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/10/19/64345.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/64345.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/10/19/64345.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/64345.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/64345.html</trackback:ping><description><![CDATA[<p>输出档案(archive)cM于输出数据流(stream)。数据能通过<< ?& 操作W存储到档案(archive)? <pre><code> </code></pre><pre><code>ar << data;</code></pre><pre><code>ar & data;</code></pre> <p>输入档案(archive)cM于输入数据流(stream)。数据能通过>> ?& 操作W从档案(archive)中装载?<pre><code> </code></pre><pre><code>ar >> data;</code></pre><pre><code>ar & data;</code></pre> <p>对于原始数据cdQ当q些操作调用的时候,数据是简单的“被存储/被装载?“到/从?档案(archive)。对于类(class)数据cdQ类的serialize 函数被调用。对上面的操作,每个serialize 函数用来“存?装蝲”其数据成员。这个处理采用递归的方式,直到所有包含在cM的数据“被存储/被装载”? <p>  <h5><a name="simplecase">一个非常简单的情Ş</a></h5> <p>通常用serialize 函数来存储和装蝲cȝ数据成员? <p>q个库包含一个叫 <a >demo.cpp</a> 的程序,用于介绍如何用这个库。下面,我们从这个demo摘录代码Q来介绍q个库应用的最单情形?pre><code> </code></pre><pre><code>#include <fstream></code></pre><pre><code> </code></pre><pre><code>// include headers that implement a archive in simple text format</code></pre><pre><code>#include <boost/archive/text_oarchive.hpp></code></pre><pre><code>#include <boost/archive/text_iarchive.hpp></code></pre><pre><code> </code></pre><pre><code>/////////////////////////////////////////////////////////////</code></pre><pre><code>// gps coordinate</code></pre><pre><code>//</code></pre><pre><code>// illustrates serialization for a simple type</code></pre><pre><code>//</code></pre><pre><code>class gps_position</code></pre><pre><code>{</code></pre><pre><code>private:</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    // When the class Archive corresponds to an output archive, the</code></pre><pre><code>    // & operator is defined similar to <<.  Likewise, when the class Archive</code></pre><pre><code>    // is a type of input archive the & operator is defined similar to >>.</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        ar & degrees;</code></pre><pre><code>        ar & minutes;</code></pre><pre><code>        ar & seconds;</code></pre><pre><code>    }</code></pre><pre><code>    int degrees;</code></pre><pre><code>    int minutes;</code></pre><pre><code>    float seconds;</code></pre><pre><code>public:</code></pre><pre><code>    gps_position(){};</code></pre><pre><code>    gps_position(int d, int m, float s) :</code></pre><pre><code>        degrees(d), minutes(m), seconds(s)</code></pre><pre><code>    {}</code></pre><pre><code>};</code></pre><pre><code> </code></pre><pre><code>int main() {</code></pre><pre><code>    // create and open a character archive for output</code></pre><pre><code>    std::ofstream ofs("filename");</code></pre><pre><code>    boost::archive::text_oarchive oa(ofs);</code></pre><pre><code> </code></pre><pre><code>    // create class instance</code></pre><pre><code>    const gps_position g(35, 59, 24.567f);</code></pre><pre><code>    // write class instance to archive</code></pre><pre><code>    oa << g;</code></pre><pre><code>    // close archive</code></pre><pre><code>    ofs.close();</code></pre><pre><code> </code></pre><pre><code>    // ... some time later restore the class instance to its orginal state</code></pre><pre><code>    // create and open an archive for input</code></pre><pre><code>    std::ifstream ifs("filename", std::ios::binary);</code></pre><pre><code>    boost::archive::text_iarchive ia(ifs);</code></pre><pre><code>    // read class state from archive</code></pre><pre><code>    gps_position newg;</code></pre><pre><code>    ia >> newg;</code></pre><pre><code>    // close archive</code></pre><pre><code>    ifs.close();</code></pre><pre><code>    return 0;</code></pre><pre><code>}</code></pre><pre> </pre> <p>对于每个通过序列化“被存储”的c,必须存在一个函数去实现“存储”其所有状态数据。对于每个通过序列化“被装蝲”的c,必须存在一个函数来实现“装载”其所有状态数据。在上面的例子中Q这些函数是模板成员函数serialize? <p>  <h5><a name="nonintrusiveversion">非R入的版本</a></h5> <p>在上例是侵入的设计。类是需要由其实例来序列化,来改变。这在某些情形是困难的。一个等L可选的设计如下Q?pre><code> </code></pre><pre><code>#include <boost/archive/text_oarchive.hpp></code></pre><pre><code>#include <boost/archive/text_iarchive.hpp></code></pre><pre><code> </code></pre><pre><code>class gps_position</code></pre><pre><code>{</code></pre><pre><code>public:</code></pre><pre><code>    int degrees;</code></pre><pre><code>    int minutes;</code></pre><pre><code>    float seconds;</code></pre><pre><code>    gps_position(){};</code></pre><pre><code>    gps_position(int d, int m, float s) :</code></pre><pre><code>        degrees(d), minutes(m), seconds(s)</code></pre><pre><code>    {}</code></pre><pre><code>};</code></pre><pre><code> </code></pre><pre><code>namespace boost {</code></pre><pre><code>namespace serialization {</code></pre><pre><code> </code></pre><pre><code>template<class Archive></code></pre><pre><code>void serialize(Archive & ar, gps_position & g, const unsigned int version)</code></pre><pre><code>{</code></pre><pre><code>    ar & g.degrees;</code></pre><pre><code>    ar & g.minutes;</code></pre><pre><code>    ar & g.seconds;</code></pre><pre><code>}</code></pre><pre><code> </code></pre><pre><code>} // namespace serialization</code></pre><pre><code>} // namespace boost</code></pre> <p>q种情况生成的serialize 函数不是gps_positioncȝ成员函数。这有异曲同工之妙? <p>非R入序列化主要应用在不改变cd义就可实现类的序列化。ؓ实现q种可能Q类必须提供_的信息来更新cȝ态。在q个例子中,我们假设cLpublic成员。仅当提供够信息来存储和装载的c,才能不改变类自nQ在外部来序列化cȝ态? <p>  <h5><a name="serializablemembers">可序列化的成?/a></h5> <p>一个可序列化的c,可拥有可序列化的成员Q例如: <pre><code> </code></pre><pre><code>class bus_stop</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        ar & latitude;</code></pre><pre><code>        ar & longitude;</code></pre><pre><code>    }</code></pre><pre><code>    gps_position latitude;</code></pre><pre><code>    gps_position longitude;</code></pre><pre><code>protected:</code></pre><pre><code>    bus_stop(const gps_position & lat_, const gps_position & long_) :</code></pre><pre><code>    latitude(lat_), longitude(long_)</code></pre><pre><code>    {}</code></pre><pre><code>public:</code></pre><pre><code>    bus_stop(){}</code></pre><pre><code>    // See item # 14 in Effective C++ by Scott Meyers.</code></pre><pre><code>    // re non-virtual destructors in base classes.</code></pre><pre><code>    virtual ~bus_stop(){}</code></pre><pre><code>};</code></pre> <p>q里Q类cd的成员被序列化,恰如原始cd被序列化一栗? <p>注意Q类bus_stop的实例“存储”时Q其归档(archive)操作W将调用latitude ?longitude的serialize 函数。这依ơ调用定义在gps_position中的serialize 来被“存储”。这U手法中Q通过<code>bus_stop</code>的归?archive)操作W?整个数据l构被存?<code>bus_stop</code>是它的根条目? <p>  <h5><a name="derivedclasses">zc?/a></h5> <p>zcd包含其基cȝ序列化?<pre><code> </code></pre><pre><code>#include <boost/serialization/base_object.hpp></code></pre><pre><code> </code></pre><pre><code>class bus_stop_corner : public bus_stop</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        // serialize base class information</code></pre><pre><code>        ar & boost::serialization::base_object<bus_stop>(*this);</code></pre><pre><code>        ar & street1;</code></pre><pre><code>        ar & street2;</code></pre><pre><code>    }</code></pre><pre><code>    std::string street1;</code></pre><pre><code>    std::string street2;</code></pre><pre><code>    virtual std::string description() const</code></pre><pre><code>    {</code></pre><pre><code>        return street1 + " and " + street2;</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_stop_corner(){}</code></pre><pre><code>    bus_stop_corner(const gps_position & lat_, const gps_position & long_,</code></pre><pre><code>        const std::string & s1_, const std::string & s2_</code></pre><pre><code>    ) :</code></pre><pre><code>        bus_stop(lat_, long_), street1(s1_), street2(s2_)</code></pre><pre><code>    {}</code></pre><pre><code>};</code></pre><pre> </pre> <p>注意在派生类中不要直接调用其基类的序列化函数。这样做看似工作Q实际上l过跟踪实例用于存储来消除冗余的代码。它也绕q写到档案中cȝ版本信息的代码。因此,L声明serialize 作ؓU有函数。声明friend boost::serialization::access 运行序列化库存取私有变量和函数? <p>  <h5><a name="pointers">指针</a></h5> <p>假设我们定义了bus route包含一lbus stops。假定: <ol> <li>我们可以有几Ubus stop的类型(Cbus_stop是一个基c)? <li>一个所l的 bus_stop可以展现多于一个的路线?</li></ol> <p>一个bus route 用一l指向bus_stop的指针来描述是方便的?pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    bus_stop * stops[10];</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        int i;</code></pre><pre><code>        for(i = 0; i < 10; ++i)</code></pre><pre><code>            ar & stops[i];</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre> </pre> <p>数组stops 的每个成员将被序列化。但是,C每个成员是个指针?- 实际含义是什么?序列化整个对象是要求在另一个地方和旉重新构造原始数据结构。用指针Z完成q些Q存储指针的值是不够的,指针指向的对象必d储。当成员最后被装蝲Q一个新的对象被创徏Q新的指针被装蝲到类的成员中? <p>所有这一切是由序列化库自动完成的。通过指针兌的对象,上述代码能完成存储和装蝲? <p>  <h5><a name="arrays">数组</a></h5> <p>事实上上q方案比较复杂。序列化库能出被序列化的对象是一个数l,生上q等L代码。因此上qC码能更短的写为:<pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    bus_stop * stops[10];</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        ar & stops;</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre> </pre> <h5><a name="stl">STL</a>容器</h5> <p>上面的例子用数组成员。更多的如此的一个应用用STL容器为如此的目的。序列化库包含ؓ所有STL容器序列化的代码。因此,下种Ҏ正如我们所预期的样子工作?pre><code> </code></pre><pre><code>#include <boost/serialization/list.hpp></code></pre><pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    std::list<bus_stop *> stops;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        ar & stops;</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre> </pre> <h5><a name="versioning">cȝ版本</a></h5> <p>假设我们对bus_routecL意,在品中使用它。一D|间后Q发觉bus_route c需要包含线路驾驶员的名字。因此新版本如下Q?<pre><code> </code></pre><pre><code>#include <boost/serialization/list.hpp></code></pre><pre><code>#include <boost/serialization/string.hpp></code></pre><pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    std::list<bus_stop *> stops;</code></pre><pre><code>    std::string driver_name;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        ar & driver_name;</code></pre><pre><code>        ar & stops;</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre> </pre> <p>好,完毕Q异?..会发生在d旧版本所生成的数据文件时。如何考虑版本问题Q? <p>通常Q序列化库ؓ每个被序列化的类在档案中存储版本受缺省值是0。当档案装蝲Ӟ存储的版本号可被d。上qC码可修改如下Q?pre><code> </code></pre><pre><code>#include <boost/serialization/list.hpp></code></pre><pre><code>#include <boost/serialization/string.hpp></code></pre><pre><code>#include <boost/serialization/version.hpp></code></pre><pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    std::list<bus_stop *> stops;</code></pre><pre><code>    std::string driver_name;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void serialize(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        // only save/load driver_name for newer archives</code></pre><pre><code>        if(version > 0)</code></pre><pre><code>            ar & driver_name;</code></pre><pre><code>        ar & stops;</code></pre><pre><code>    }</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre><code> </code></pre><pre><code>BOOST_CLASS_VERSION(bus_route, 1)</code></pre><pre> </pre> <p>Ҏ个类通过应用的版本,没有必要l护一个版本文件。一个文件版本是所有它l成的类的版本的联合。系l允许程序和以前版本的程序创建的档案向下兼容? <p>  <h5><a name="splitting">把serialize拆分成save/load</a></h5> <p>serialize函数是简单,z,q且保证cL员按同样的顺序(序列化系l的keyQ被存储/被装载。可是有像这里例子一P装蝲和存储不一致的情Ş。例如,一个类有多个版本的情况发生。上q情形能重写为:<pre><code> </code></pre><pre><code>#include <boost/serialization/list.hpp></code></pre><pre><code>#include <boost/serialization/string.hpp></code></pre><pre><code>#include <boost/serialization/version.hpp></code></pre><pre><code>#include <boost/serialization/split_member.hpp></code></pre><pre><code> </code></pre><pre><code>class bus_route</code></pre><pre><code>{</code></pre><pre><code>    friend class boost::serialization::access;</code></pre><pre><code>    std::list<bus_stop *> stops;</code></pre><pre><code>    std::string driver_name;</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void save(Archive & ar, const unsigned int version) const</code></pre><pre><code>    {</code></pre><pre><code>        // note, version is always the latest when saving</code></pre><pre><code>        ar  & driver_name;</code></pre><pre><code>        ar  & stops;</code></pre><pre><code>    }</code></pre><pre><code>    template<class Archive></code></pre><pre><code>    void load(Archive & ar, const unsigned int version)</code></pre><pre><code>    {</code></pre><pre><code>        if(version > 0)</code></pre><pre><code>            ar & driver_name;</code></pre><pre><code>        ar  & stops;</code></pre><pre><code>    }</code></pre><pre><code>    BOOST_SERIALIZATION_SPLIT_MEMBER()</code></pre><pre><code>public:</code></pre><pre><code>    bus_route(){}</code></pre><pre><code>};</code></pre><pre><code> </code></pre><pre><code>BOOST_CLASS_VERSION(bus_route, 1)</code></pre><pre> </pre> <p>BOOST_SERIALIZATION_SPLIT_MEMBER() 宏生成调?save ?load的代码,依赖于是否档案被用于“存储”或“装载”? <p>  <h5><a name="archives">档案</a></h5> <p>我们q里讨论聚焦到cȝ序列化能力上。被序列化的数据的实际编码实C档案(archive)cM。被序列化的数据是所选档?archive)cȝ序列化的产物??key设计军_q两个组件的独立性。允怓Q何序列化的规范可用于M档案(archive)? <p>在这指南中Q我们用了一个档案类-用于存储的text_oarchive和用于装载的text_iarchivecR在库中其他档案cȝ接口完全一致。一旦类的序列化已经被定义,c能被序列化CQ何档案类型? <p>假如当前的档案集不能提供某个属性,格式Q或行ؓ需要特化的应用。要么创Z个新的要么从已有的里面衍生一个。将在后l文档中描述? <p>注意我们的例子save和loadE序数据在一个程序中Q这是ؓ了讨论方便而已。通常Q被装蝲的档案或许在或许不在同一个程序中? <p>T完整的演C程?- <a >demo.cpp</a> 包括Q? <ol> <li>创徏各种cd?stops, routes ?schedules <li>昄? <li>序列化到一个名?"testfile.txt"的文件中 <li>q原到另一个结构中 <li>昄被存储的l构 </li></ol> <p><a >q个E序的输?/a> 分证实了对序列化pȝ所有的要求Q都在这个系l中体现了。对序列化文件是ASCII文本?a >档案文g的内?/a> 能被昄?/p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/64345.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-10-19 00:38 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/10/19/64345.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用std::vector的一个误?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/10/16/64134.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Thu, 16 Oct 2008 05:28:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/10/16/64134.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/64134.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/10/16/64134.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/64134.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/64134.html</trackback:ping><description><![CDATA[<form id="Form1" name="Form1" action="http://www.shnenglu.com/1010.html" method="post" _initialAction="1010.html"> <div id="ee684gu" class="pagelayout"> <div id="mke2y2q" class="centercolumn"> <div id="6s42iq2" class="singlepost">借助valgrindQ终于找C个隐藏很久的bugQ?br> <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.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><span style="color: #0000ff">class</span><span style="color: #000000"> A<br><img id="Codehighlighter1_8_21_Open_Image" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_8_21_Closed_Image" style="display: none" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_8_21_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.shnenglu.com/Images/dot.gif"></span><span id="Codehighlighter1_8_21_Open_Text"><span style="color: #000000">{<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align="top">    <img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span><span style="color: #000000">;<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">std::vector</span><span style="color: #000000"><</span><span style="color: #000000">A</span><span style="color: #000000">></span><span style="color: #000000">  vecArray;<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">vecArray.push_back( <img src="http://www.shnenglu.com/Images/dot.gif"> );<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">vecArray.push_back( <img src="http://www.shnenglu.com/Images/dot.gif"> );<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">vecArray.push_back( <img src="http://www.shnenglu.com/Images/dot.gif"> );<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">A </span><span style="color: #000000">&</span><span style="color: #000000"> a</span><span style="color: #000000">=</span><span style="color: #000000"> vecArray.back();<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top">vecArray.push_back( <img src="http://www.shnenglu.com/Images/dot.gif"> );<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><img src="http://www.shnenglu.com/Images/dot.gif"><img src="http://www.shnenglu.com/Images/dot.gif"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align="top"></span></div>以上的代码骤眼看上去也没有什么奇怪的地方Q经q多ơ的试也没有发C么问题。但昨天bugl于爆发了,E序L无缘无故down掉?br>其实原因很简单,是?A & a = vecArray.back(); q一步?br>vector是一个会自增长的容器Q自增长的结果就是把原来的内存释放掉Q重新分配一个够大的内存。既然原来的内存已经释放掉,那么a所引用的内存就是一D|效的内存。对无效内存的访问,后果有多严重׃用多说了?br><br>惌x法也很多Q最单就是用std::list或者std::deque替代vector?</div> <div id="wikkyoi" class="singlepost"> </div> <div id="c2eiqiy" class="singlepost">========================================================================================</div> <div id="gycmewo" class="singlepost"> </div> <p>Comments <ul> <li> <p><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#1026">#</a> <a name="1026"></a>re: 使用std::vector的一个误? <p><a>glacjay</a><br>Posted @ 2005-11-09 22:06<br>不可以保存烦引吗Q必要用指针吗Qؓ了效率?  <a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#post">回复</a> <a href="http://www.shnenglu.com/comment?author=glacjay">更多评论</a> <a href="http://www.shnenglu.com/"></a><a></a></p> <li> <p><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#1033">#</a> <a name="1033"></a>re: 使用std::vector的一个误? <p><a href="http://www.shnenglu.com/cyt/">cyt</a><br>Posted @ 2005-11-10 09:23<br>用烦引是可以Q不q个Z大喜Ƣ用索引。其原因是索引q不是stl容器所共有的特性,一旦用了索引Q以后如果需要{换ؓ其他容器的时候,未免需要改动不地斏V另外,我这里所说的只是一U解x法。对于我q种情况Q只需要更改一个typedef定义可以解决问题。如果改成是用烦引的话,需要更改不地方,而且很容易会漏了?<br>E序本n法也比较复杂,相对来说使用索引性能也不会下降多,q个倒不是什么需要考虑的地方了?nbsp; <a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#post">回复</a> <a href="http://www.shnenglu.com/comment?author=cyt">更多评论</a> <a href="http://www.shnenglu.com/"></a><a></a></p> <li> <p><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#1092">#</a> <a name="1092"></a>re: 使用std::vector的一个误? <p><a>nilaozi</a><br>Posted @ 2005-11-11 22:23<br>最后一ơ评论。也够出气了Q?<br>一个在blogjava失去家园的h声讨?<br>除了政治原因可以删了一个hblogQ别的理由都不能成ؓ理由?<br>我ؓ每个都到他们所需的ebookN错了Q?<br>贴到首页Q我q以你们有预xӞCSDN是这P理员认为可以才攑ֈ首页?<br>不信你到他们的首늜看?<br><a >http://csdn.blog.net/ahhoo</a><br>如果你认不错的,请到回脓?<br><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#post">回复</a> <a href="http://www.shnenglu.com/comment?author=nilaozi">更多评论</a> <a href="http://www.shnenglu.com/"></a><a></a></p> <li> <p><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#12011">#</a> <a name="12011"></a>re: 使用std::vector的一个误? <p><a href="http://www.shnenglu.com/pjqblues/">blues</a><br>Posted @ 2006-09-04 14:05<br>vector和deque的差别是操作上的Qlist虽然是基于链表的Q但是不能随问,M的容器都不是完美的,而且使用指针必然要判断,但也是个bugQ?<br>我想解决的话cd用boost的any应该可以了,它本w带有安全检查,我没试仅是徏?nbsp; <a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#post">回复</a> <a href="http://www.shnenglu.com/comment?author=blues">更多评论</a> <a href="http://www.shnenglu.com/"></a><a></a></p> <li> <p><a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#63290">#</a> <a name="63290"></a>re: 使用std::vector的一个误?a name="Post"></a> <p><a>stl</a><br>Posted @ 2008-10-06 01:03<br>STL里的容器大部分都是“值”容器,你用“引用”去讉K容器里的内容Q错了正常。这不是vector的问题,你自׃用的问题。如果基于性能考虑上面的程序一定要使用“引用”语意的话,应该用vector<A*>的写法才比较正确?nbsp; <a href="http://www.shnenglu.com/cyt/archive/2005/11/10/1010.html#post">回复</a> <a href="http://www.shnenglu.com/comment?author=stl">更多评论</a> <a href="http://www.shnenglu.com/"></a></p></li></ul></div></div></form><img src ="http://www.shnenglu.com/woaidongmao/aggbug/64134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-10-16 13:28 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/10/16/64134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>iteratorҎ值加减的分别http://www.shnenglu.com/woaidongmao/archive/2008/10/13/63875.html肥仔肥仔Mon, 13 Oct 2008 03:25:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/10/13/63875.htmlhttp://www.shnenglu.com/woaidongmao/comments/63875.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/10/13/63875.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/63875.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/63875.html//----下面的可?

vector<TickValue>::iterator iter;
iter = iter + 3;   

//----下面的不可以

list<TickValue>::iterator iter;
iter = iter + 3;   



肥仔 2008-10-13 11:25 发表评论
]]>
STL的container的一些特性测?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/09/09/61382.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Tue, 09 Sep 2008 07:53:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/09/09/61382.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/61382.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/09/09/61382.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/61382.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/61382.html</trackback:ping><description><![CDATA[<p>I容器对首元素求地址q算 <p>int main(int argc, char* argv[])<br>{<br>    vector<int> vec_test;<br>    list<int>    lst_test;<br>    set<int>    set_test;<br>    int* p_test = &(*vec_test.begin()); //---- NULL<br>    p_test = &vec_test[0];                //---- NULL<br>    p_test = &(*lst_test.begin());        //---- 不是NULL<br>    p_test = (int*)&(*set_test.begin());//---- 不是NULL<br>    return 0;<br>} <p>============================================================================================ <p>vector的assginI地址试Q红色部分无法理?<p>int main(int argc, char* argv[])<br>{<br>    vector<int> vec_test;<br>    size_t s = 0;<br>    vec_test.assign(NULL, NULL);<br>    s = vec_test.size();    //---- 0<br> <font color="#ff0000">   vec_test.assign(NULL, NULL + 100);<br>    s = vec_test.size();    //---- 0</font><br>    return 0;<br>}</p><img src ="http://www.shnenglu.com/woaidongmao/aggbug/61382.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-09-09 15:53 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/09/09/61382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>容器.size()q回size_tQ的注意事项http://www.shnenglu.com/woaidongmao/archive/2008/09/09/61380.html肥仔肥仔Tue, 09 Sep 2008 07:26:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/09/09/61380.htmlhttp://www.shnenglu.com/woaidongmao/comments/61380.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/09/09/61380.html#Feedback1http://www.shnenglu.com/woaidongmao/comments/commentRss/61380.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/61380.html下面代码的原意是便利vector的前n-1个,但是Q没注意到size_t是unsigned intQ因此会出现bug.

如果m_vecSymbolSummary.size() == 0,那么m_vecSymbolSummary.size() - 1会变成很大敎ͼ而生问?/p>

 

for (size_t t = 0; t < (m_vecSymbolSummary.size() - 1); ++t)



肥仔 2008-09-09 15:26 发表评论
]]>
容器相等Q可q行直接判断http://www.shnenglu.com/woaidongmao/archive/2008/09/06/61131.html肥仔肥仔Sat, 06 Sep 2008 08:37:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/09/06/61131.htmlhttp://www.shnenglu.com/woaidongmao/comments/61131.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/09/06/61131.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/61131.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/61131.htmlvector, list, set, map ,hash_map, hash_set

但是需要容器元素,支持==,或者有<



肥仔 2008-09-06 16:37 发表评论
]]>
std::sort多关键字排序http://www.shnenglu.com/woaidongmao/archive/2008/09/06/61126.html肥仔肥仔Sat, 06 Sep 2008 07:39:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/09/06/61126.htmlhttp://www.shnenglu.com/woaidongmao/comments/61126.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/09/06/61126.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/61126.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/61126.html#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct MultiData
{
    int a;
    int b;
   int c;
};

struct ICmpMuls
{
    bool operator()(const MultiData& first, const MultiData& second) const
    {
        if(first.a< second.a)return true;
        if(first.a== second.a)
           if(first.b< second.b)return true;
              if(first.b== second.b)
                 if(first.c< second.c)return true;
        return false;
    }
};

int main(int argc, char* argv[])
{
    vector<MultiData> vec_muls;
    for (int i = 0; i < 300; ++i)
        vec_muls.push_back(MultiData(rand()%20, rand()));
    sort(vec_muls.begin(), vec_muls.end(), ICmpMuls());
    for (size_t s = 0; s < vec_muls.size(); ++s)
        cout<<s<<":     "<<vec_muls[s].login<<" / "<<vec_muls[s].order<<endl;
    return 0;
}



肥仔 2008-09-06 15:39 发表评论
]]>
条款9Q在删除选项中仔l选择http://www.shnenglu.com/woaidongmao/archive/2008/08/02/57844.html肥仔肥仔Sat, 02 Aug 2008 08:57:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/08/02/57844.htmlhttp://www.shnenglu.com/woaidongmao/comments/57844.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/08/02/57844.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/57844.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/57844.html假定你有一个标准STL容器QcQ容UintQ?pre>Container<int> c;

而你xc中所有gؓ1963的对象都L。o人吃惊的是,完成q项d的方法因不同的容器类型而不同:没有一U方法是通用的?

如果你有一个连l内存容器(vector、deque或string——参?a >条款1Q,最好的Ҏ是erase-remove惯用法(参见条款32Q:

c.erase(remove(c.begin(), c.end(), 1963),	// 当c是vector、string
		c.end());			// 或dequeӞ
						// erase-remove惯用?
						// 是去除特定值的元素
						// 的最x?

q方法也适合于listQ但是,正如条款44解释的,list的成员函数remove更高效:

c.remove(1963);		// 当c是listӞ
			// remove成员函数是去?
			// 特定值的元素的最x?

当c是标准关联容器(卻Iset、multiset、map或multimapQ时Q用Q何叫做remove的东襉K是完全错误的。这L容器没有叫做remove的成员函敎ͼ而且使用remove法可能覆盖容器|参见条款32Q,潜在地破坏容器。(关于q样的破坏的l节Q参?a >条款22Q那个条ƾ也解释了ؓ什么试囑֜map和multimap上用remove肯定不能~译Q而试囑֜set和multiset上用可能不能编译。)

不,对于兌容器Q解决问题的适当Ҏ是调用eraseQ?pre>c.erase(1963); // 当c是标准关联容器时 // erase成员函数是去? // 特定值的元素的最x?

q不仅是正确的,而且很高效,只花费对数时间。(序列容器的基于删除的技术需要线性时间。)q且Q关联容器的erase成员函数有基于等仯不是相{的优势Q?a >条款19解释了这一区别的重要性?

让我们现在稍微修改一下这个问题。不是从c中除L个有特定值的物体Q让我们消除下面判断式(参见条款39Q返回真的每个对象:

bool badValue(int x);	// q回x是否是“bad?/pre>

对于序列容器Qvector、string、deque和listQ,我们要做的只是把每个remove替换为remove_ifQ然后就完成了:

c.erase(remove_if(c.begin(), c.end(), badValue),	// 当c是vector、string
			c.end());			// 或deque时这是去?
						// badValueq回?
						// 的对象的最x?
c.remove_if(badValue);				// 当c是list时这是去?
						// badValueq回?
						// 的对象的最x?

对于标准兌容器Q它不是很直截了当。有两种Ҏ处理该问题,一个更Ҏ~码Q另一个更高效。“更Ҏ但效率较低”的解决Ҏ用remove_copy_if把我们需要的值拷贝到一个新容器中,然后把原容器的内容和新的交换Q?pre>AssocContainer<int> c; // c现在是一U? ... // 标准兌容器 AssocContainer<int> goodValues; // 用于容纳不删? // 的值的临时容器 remove_copy_if(c.begin(), c.end(), // 从c拯不删? inserter(goodValues, // 的值到 goodValues.end()), // goodValues badValue); c.swap(goodValues); // 交换c和goodValues // 的内?

对这U方法的~点是它拯了所有不删除的元素,而这L拯开销可能大于我们感兴支付的?

我们可以通过直接从原容器删除元素来避开那笔帐单。不q,因ؓ兌容器没有提供cMremove_if的成员函敎ͼ所以我们必d一个@环来q代c中的元素Q和原来一样删除元素?

看v来,q个d很简单,而且实际上,代码也很单。不q的是,那些正确工作的代码很是跃出脑v的代码。例如,q是很多E序员首先想到的Q?pre>AssocContainer<int> c; ... for (AssocContainer<int>::iterator i = c.begin(); // 清晰Q直截了? i!= c.end(); // 而漏z百出的用于 ++i) { // 删除c中badValueq回? if (badValue(*i)) c.erase(i); // 的每个元素的代码 } // 不要q么做!

唉,q有未定义的行ؓ。当容器的一个元素被删时Q指向那个元素的所有P代器都失效了。当c.erase(i)q回Ӟi已经失效。那对于q个循环是个坏消息,因ؓ在eraseq回后,i通过for循环?+i部分自增?

Z避免q个问题Q我们必M证在调用erase之前得Cc中下一元素的P代器。最Ҏ的方法是当我们调用时在i上用后|递增Q?pre>AssocContainer<int> c; ... for (AssocContainer<int>::iterator i = c.begin(); // for循环的第三部? i != c.end(); // 是空的;i现在在下? /*nothing*/ ){ // 自增 if (badValue(*i)) c.erase(i++); // 对于坏的|把当前的 else ++i; // i传给eraseQ然? } // 作ؓ副作用增加iQ? // 对于好的| // 只增加i

q种调用erase的解x法可以工作,因ؓ表达式i++的值是i的旧|但作为副作用Qi增加了。因此,我们把i的旧|没增加的Q传leraseQ但在erase开始执行前i已经自增了。那正好是我们想要的。正如我所说的Q代码很单,只不q不是大多数E序员在W一ơ尝试时惛_的?

现在让我们进一步修改该问题。不仅删除badValueq回真的每个元素Q而且每当一个元素被删掉Ӟ我们也想把一条消息写到日志文件中?

对于兌容器Q这说多Ҏ有多容易,因ؓ只需要对我们刚才开发的循环做一个微不道的修改p了:

ofstream logFile;					// 要写入的日志文g
AssocContainer<int> c;
...
for (AssocContainer<int>::iterator i = c.begin();	// 循环条g和前面一?
	i !=c.end();){
	if (badValue(*i)){ 
		logFile << "Erasing " << *i <<'\n';	// 写日志文?
		c.erase(i++);			// 删除元素
	}
	else ++i;
}

现在是vector、string和dequel我们带来麻烦。我们不能再使用erase-remove惯用法,因ؓ没有办法让erase或remove写日志文件。而且Q我们不能用刚刚ؓ兌容器开发的循环Q因为它为vector、string和deque产生未定义的行ؓQ要记得对于那样的容器,调用erase不仅使所有指向被删元素的q代器失效,也被删元素之后的所有P代器失效。在我们的情况里Q那包括所有i之后的P代器。我们写i++Q?+i或你能想L其它M东西都没有用Q因为没有能Dq代器有效的?

我们必须对vector、string和deque采用不同的战略。特别是Q我们必d用erase的返回倹{那个返回值正是我们需要的Q一旦删除完成,它就是指向紧接在被删元素之后的元素的有效q代器。换句话_我们q么写:

for (SeqContainer<int>::iterator i = c.begin(); 
	i != c.end();){
	if (badValue(*i)){
		logFile << "Erasing " << *i << '\n'; 
		i = c.erase(i);			// 通过把erase的返回?
	}					// 赋给i来保持i有效
	else
		++i;
}

q可以很好地工作Q但只用于标准序列容器。由于论证一个可能的问题Q?a >条款5做了Q,标准兌容器的erase的返回类型是void[1]。对于那些容器,你必M用“后|递增你要传给erase的P代器”技术。(Z说说Q在为序列容器编码和为关联容器编码之间的q种差别是ؓ什么写容器无关代码一般缺乏考虑的一个例子——参?a >条款2?

Z避免你奇怪list的适当Ҏ是什么,事实表明对于q代和删除,你可以像vector/string/deque一h像关联容器一样对待listQ两U方法都可以为list工作?

如果我们观察在本条款中提到的所有东西,我们得出下列l论Q?

  • 去除一个容器中有特定值的所有对象:

    如果容器是vector、string或dequeQ用erase-remove惯用法?

    如果容器是listQ用list::remove?

    如果容器是标准关联容器,使用它的erase成员函数?/p>

  • 去除一个容器中满一个特定判定式的所有对象:

    如果容器是vector、string或dequeQ用erase-remove_if惯用法?

    如果容器是listQ用list::remove_if?

    如果容器是标准关联容器,使用remove_copy_if和swapQ或写一个@环来遍历容器元素Q当你把q代器传lerase时记得后|递增它?/p>

  • 在@环内做某些事情(除了删除对象之外Q:

    如果容器是标准序列容器,写一个@环来遍历容器元素Q每当调用erase时记得都用它的返回值更C的P代器?

    如果容器是标准关联容器,写一个@环来遍历容器元素Q当你把q代器传lerase时记得后|递增它?/p>

如你所见,与仅仅调用erase相比Q有效地删除容器元素有更多的东西。解决问题的最好方法取决于你是怎样鉴别出哪个对象是要被L的,储存它们的容器的cdQ和当你删除它们的时候你q想要做什么(如果有的话)。只要你心而且注意了本条款的徏议,你将毫不费力。如果你不小心,你将冒着产生不必要低效的代码或未定义行ؓ的危险?/p>

肥仔 2008-08-02 16:57 发表评论
]]>
STL 查找法http://www.shnenglu.com/woaidongmao/archive/2008/07/04/55335.html肥仔肥仔Fri, 04 Jul 2008 05:36:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/07/04/55335.htmlhttp://www.shnenglu.com/woaidongmao/comments/55335.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/07/04/55335.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/55335.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/55335.html

要给出所有我们在本条ƾ中所考虑到的Q我们的从哪儿着手?下面的表格道Z一切?/p>
你想知道? 使用的算? 使用的成员函?/tr>
在无序区?/td> 在已序区?/td> 在set或map?/td> 在multiset或multimap?/td>
期望值是否存在? find binary_search count find
期望值是否存在?如果有,W一个等于这个值的对象在哪里? find equal_range find find or lower_bound(see article)
W一个不{于期望值的对象在哪里? find_if lower_bound lower_bound lower_bound
W一个等于期望值的对象在哪里? find_if upper_bound upper_bound upper_bound
有多对象等于期望| count equal_range count count
{于期望值的所有对象在哪里Q?/td> findQP代) equal_range equal_range equal_range

===============================================================================
Example
// equal_range example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mygreater (int i,int j) { return (i>j); }

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  vector<int> v(myints,myints+8);                         // 10 20 30 30 20 10 10 20
  pair<vector<int>::iterator,vector<int>::iterator> bounds;

  // using default comparison:
  sort (v.begin(), v.end());                              // 10 10 10 20 20 20 30 30
  bounds=equal_range (v.begin(), v.end(), 20);            //          ^        ^

  // using "mygreater" as comp:
  sort (v.begin(), v.end(), mygreater);                   // 30 30 20 20 20 10 10 10
  bounds=equal_range (v.begin(), v.end(), 20, mygreater); //       ^        ^

  cout << "bounds at positions " << int(bounds.first - v.begin());
  cout << " and " << int(bounds.second - v.begin()) << endl;

  return 0;
}

Output:

bounds ar positions 2 and 5
=================================================================

equal_range()
template< class ForwardIterator, class Type >
pair< ForwardIterator, ForwardIterator >
equal_range( ForwardIterator first,                 ForwardIterator last, const Type &value );
//q回pair Q若容器中存在搜索|W一个返回by lower_bound(); W二?by upper_bound(); 
               若容器中不存在搜索|两个都返回by upper_bound();            template< class ForwardIterator, class Type, class Compare >
pair< ForwardIterator, ForwardIterator >
equal_range( ForwardIterator first,                 ForwardIterator last, const Type &value,                 Compare comp );

#include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
#include<functional>
using namespace std;
/* generates:
      array element sequence after sort:
      12 15 17 19 20 22 23 26 29 35 40 51
      equal_range result of search for value 23:
              *ia_iter.first: 23         *ia_iter.second: 26
      equal_range result of search for absent value 21:
              *ia_iter.first: 22         *ia_iter.second: 22
      vector element sequence after sort:
      51 40 35 29 26 23 22 20 19 17 15 12
      equal_range result of search for value 26:
              *ivec_iter.first: 26       *ivec_iter.second: 23
      equal_range result of search for absent value 21:
              *ivec_iter.first: 20       *ivec_iter.second: 20
      */

int main()
{
      int ia[] = { 29,23,20,22,17,15,26,51,19,12,35,40 };
      vector< int > ivec( ia, ia+12 );
      ostream_iterator< int >     ofile( cout, " " );
      sort( &ia[0], &ia[12] );
      cout << "array element sequence after sort:\n";
      copy( ia, ia+12, ofile ); cout << "\n\n";
      pair< int*,int* > ia_iter;
      ia_iter = equal_range( &ia[0], &ia[12], 23 );
      cout << "equal_range result of search for value 23:\n\t"
           << "*ia_iter.first: "     << *ia_iter.first << "\t"
           << "*ia_iter.second: " << *ia_iter.second << "\n\n";
      ia_iter = equal_range( &ia[0], &ia[12], 21 );
      cout << "equal_range result of search for "
           << "absent value 21:\n\t"
           << "*ia_iter.first: "     << *ia_iter.first << "\t"
           << "*ia_iter.second: " << *ia_iter.second << "\n\n";
      sort( ivec.begin(), ivec.end(), greater<int>() );
      cout << "vector element sequence after sort:\n";
      copy( ivec.begin(), ivec.end(), ofile ); cout << "\n\n";
      typedef vector< int>::iterator iter_ivec;
      pair< iter_ivec, iter_ivec > ivec_iter;
      ivec_iter = equal_range( ivec.begin(), ivec.end(), 26,
                  greater<int>() );
      cout << "equal_range result of search for value 26:\n\t"
           << "*ivec_iter.first: "     << *ivec_iter.first << "\t"
           << "*ivec_iter.second: " << *ivec_iter.second
         ;
      ivec_iter = equal_range( ivec.begin(), ivec.end(), 21,
                  greater<int>() );
      cout << "equal_range result of search for "
           << "absent value 21:\n\t"
           << "*ivec_iter.first: "     << *ivec_iter.first << "\t"
           << "*ivec_iter.second: " << *ivec_iter.second
           ;
}



肥仔 2008-07-04 13:36 发表评论
]]>
EffectiveSTLQItem16Q如何将vector和string的数据传l传l的API函数http://www.shnenglu.com/woaidongmao/archive/2008/06/19/54027.html肥仔肥仔Thu, 19 Jun 2008 08:13:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/06/19/54027.htmlhttp://www.shnenglu.com/woaidongmao/comments/54027.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/06/19/54027.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/54027.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/54027.htmlItem 16Q如何将vector和string的数据传l传l的API函数

       因ؓ C++语言已经?998q被标准化,C++的中坚分子在试图推动E序员从数组转到vector时就没什么顾虑了。同L情况也发生于从char *指针转到string对象的过E中。有很好的理由来做这些{变,比如可以消除常见的编E错误(Item 13Q,和有Z获得STL泛型法的全部强大能?(参见Q比如,Item 31)?

       但是Q麻烦还是有的,而最常见的一个就是已l存在的传统C风格API函数接受的是数组和char *指针Q而不是vector和string对象。这LAPI函数q将会存在很长时_如果我们要高效用STL的话Q就必须和它们和q_处?

       q运的是Q这很容易。如果你有一个vector对象vQ而你需要得C个指向v中数据的指针Q以使得它可以被当作一个数l,只要使用&v[0]可以了。对于string对象sQ相应的语法是很单的s.c_str()。但只能从上面读。如q告时常指明的,有几个限制。?

l定一?

vector<int> v;

       表达式v[0]生一个指向vector中首元素的引用,所以,&v[0]是指向那个首元素的指针。vector中的元素被C++标准限定为存储在q箋内存中,p是一个数l,所以,如果我们传递vl如此Ş式的C风格API函数

void doSomething(const int* pInts, size_t numInts);

       我们可以q么做:

doSomething(&v[0],v.size());

       也许吧。可能吧。唯一的问题就是,如果v是空的。如果这L话,v.size()?Q?amp;v[0]试图产生一个指向根本就不存在的东西的指针。这不是件好事。其l果未定义。一个较安全的方法是q样Q?

if (!v.empty()) {

doSomething(&v[0], v.size());

}

       如果走得不对路,你可能会到些半瓶子的h物,他们会告诉你说可以用v.begin()代替&v[0]Q因?q些讨厌的家伙将会告诉你)begin()q回指向vector内部的iteratorQ而对于vectorQ其iterators实际上是指针。那l常是正的Q但如Item 50所_q不L如此Q你不该依赖于此。begin()的返回类型是iteratorQ而不是一个指针,当你需要一个指向vector内部数据的指针时l不该用begin()。如果你Z某些原因军_键入v.begin()Q就键入&*v.begin()Q?因ؓq将会生和&v[0]相同的指针,虽然它让你有更多的击键工作且让代码读h更晦涩。坦白地_如果你正被告诉你使用v.begin()代替&v[0]的h围绕的话Q你该重新考虑一下你的社交圈了?

       cM的从vector上获取指向内部数据的指针的方法,对string是不可靠的,因ؓ

       (1) string中的数据q没有承存储在连l内存中Q?

       (2)string的内部表CŞ式ƈ没承Z一个空字符l束?

       q解释了string的成员函数c_str()存在的原因,它返回一个按C风格设计指针Q指向string的|which returns a pointer to the value of the string in a form designed for CQ?我们可以如此传递一个string对象sl这个函敎ͼ

void doSomething(const char *pString);

       pq样Q?

doSomething(s.c_str());

       即是字W串的长度ؓ0Q它都能工作。在那种情况下,c_str()返回一个指向结束符的指针。即使字W串内部自己存在l束W时Q它同样能工作。然而,如果真的q样QdoSomething很可能将W一个结束符解释为字W串l束。string对象不在意是否容U了l束W,但基于char *的C风格API函数在意?

       再看一下doSomething()的申明:

void doSomething(const int* pInts, size_t numInts);

void doSomething(const char *pString);

       在两UŞ式下Q指针都被传递ؓ指向const的指针。vector和string的数据被传给只读取而不修改它们的API函数。到目前为止都做的是最安全的事情。对于stringQ这也是唯一可做的,因ؓ没有承诺说c_str()产生的指针指在string数据的内部表CŞ式上Q它可以q回一个指针指向数据的一个不可修改的拯Q这个拷贝满C风格API函数Ҏ式的要求。(如果q个恐吓令你寒毛都立h的话Q还请宽心,因ؓ它也怸成立。我没听说目前哪个运行库的实现是使用了这个自由权的。)

       对于vectorQ有更多一点点灉|性。如果你v传给一个修改其元素的C风格API函数的话Q典型情况都是没问题Q但被调用的函数l不能试图改变vector中元素的个数。比如,它绝不能试图在vectorq未使用的容量(capacityQ上“创建”新的元素。如果这么干了,v会变得内部状态不一_因ؓ它再也不知道自己的正大(sizeQ了。v.size()会得到一个不正确的结果。ƈ且,如果被调用的函数试图在一个大和定wQ见Item 14Q相{的vector上追加数据的话,真的会发生灾难性事件。我甚至Ҏ׃愿去惌它。实在太可怕了?

       你注意到我在前面用的是“典型地”一词吗Q你当然注意C。有些vector对其数据有些额外的限Ӟ你一定要保q些额外限制l箋被满뀂D个例子,Item 23解释了排序的vector常用来实现关联容器,但对q些vector而言Q保持排序非帔R要。如果你一个排序的vector传给一个可能修改其数据的API函数Q你需要重视vector在调用返回后不再保持排序的情c?

       如果你想用C风格API函数q回的元素初始化一个vectorQ你可以利用vector和数l内在的相容性,通过存储vecotr的元素的I间传给API函数Q?

// C API: this function takes a pointer to an array of at most arraySize

// doubles and writes data to it. It returns the number of doubles written,

// which is never more than maxNumDoubles.

size_t fillArray(double *pArray, size_t arraySize);

vector<double> vd(maxNumDoubles); // create a vector whose

// size is maxNumDoubles

vd.resize(fillArray(&vd[0], vd.size())); // have fillArray write data

// into vd, then resize vd

// to the number of

// elements fillArray wrote

       q个技巧只能工作于vectorQ因为只有vector承诺了与数组h相同的内在内存分布。但是,如果你想用来自C风格API函数的数据初始化string对象Q你可以做得_单。只要让API函数数据放入一个vector<char>Q然后从vector中将数据拷到string:

// C API: this function takes a pointer to an array of at most arraySize

// chars and writes data to it. It returns the number of chars written,

// which is never more than maxNumChars.

size_t fillString(char *pArray, size_t arraySize);

vector<char> vc(maxNumChars); // create a vector whose

// size is maxNumChars

size_t charsWritten = fillString(&vc[0], vc.size()); // have fillString write

// into vc

string s(vc.begin(), vc.begin()+charsWritten); // copy data from vc to s

// via range constructor

// ( see Item 5)

       事实上,q个LL有效的:让C风格API函数数据放入一个vectorQ然后拷C实际惌的STL容器中?

size_t fillArray(double *pArray, size_t arraySize); // as above

vector<double> vd(maxNumDoubles); // also as above

vd.resize(fillArray(&vd[0], vd.size());

deque<double> d(vd.begin(), vd.end()); // copy data into

// deque

list<double> l(vd.begin(), vd.end()); // copy data into list

set<double> s(vd.begin(), vd.end()); // copy data into set

       此外Q这也提CZvector和string以外的STL容器如何它们的数据传给C风格API函数。只要将容器的每个数据拷到vectorQ然后将它们传给API函数Q?

void doSomething(const int* pInts, size_t numInts); // C API (from above)

set<int> intSet; // set that will hold

... // data to pass to API

vector<int> v(intSet.begin(), intSet.end()); // copy set data into

// a vector

if (!v.empty()) doSomething(&v[0], v.size()); // pass the data to

// the API

       你也可以数据拷q一个数l,然后数l传lC风格的APIQ但你ؓ什么想q样做?除非你在~译期就知道容器的大,否则你不得不分配动态数l,而Item 13解释了ؓ什么你应该L使用vector来取代动态分配的数组?

肥仔 2008-06-19 16:13 发表评论
]]>
使用boost.spirit制作一个简单的四则计算?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/05/17/50116.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Fri, 16 May 2008 16:16:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/05/17/50116.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/50116.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/05/17/50116.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/50116.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/50116.html</trackback:ping><description><![CDATA[<p></p> <div class="keoygyq" id="page"> <div id="km6cmmm" class="narrowcolumn" id="main"> <div class="6s62cws" id="contents" style="margin-right: 260px"> <div id="cumcuw6" class="llist1"> <div id="wg4oo2c" class="padding6x"> <div id="gkeo424" class="post" id="post-4942"> <div id="6m02ak8" class="content"> <div id="kum2mc2" class="intro"> <div id="4ys2wew" class="excerpt"> 从传l意义上来说Qboost.spirit库是一个类gyacc的库Q主要业务是做词法解析,然后提供各种d数据的接口,但是׃q是一个用C++实现、ƈ且大量运Expression Templates技巧的?.. </div></div> <div id="6ue2kkc" class="tbody">点击下蝲此文Ӟ<img style="margin: 0px 2px -4px 0px" alt="下蝲文g" src="http://www.kuqin.com/upimg/allimg/070913/0001320.gif"> <a target="_blank">单的四则计算器源码和可执行程?/a><br><br>从传l意义上来说Qboost.spirit库是一个类gyacc的库Q主要业务是做词法解析,然后提供各种d数据的接口,但是׃q是一个用C++实现、ƈ且大量运<a target="_blank">Expression Templates</a>技巧的库,所以各U功能可以用非常快捷的方式实玎ͼ非常的好用,几乎把C++的各U优良特性都充分发挥出来?br><br>在此Q我׃boost.spirit库中间的一个例子出发,l过单的修改变成一个极为健壮的四则计算器。我所参考的例子是boost 1.34.0的libs/spirit/example/fundamental/calc_plain.cppQ我因ؓ是在q上面直接修改而来Q所以源码中带有原作者的版权声明?br><br>关于boost.spirit的用法,在这里我先不_以后有时间我来慢慢的把它用中国话讲解一遍。这个程序的核心实际上是一个EBNF的表辑ּQ也是如何用EBNF语法来表C四则运?br>在这里,我就直接l出{案QEBNF的知识请暂时自行看编译原理的教材Q:<br> <div id="iumw88o" class="UBBPanel"> <div id="seyi42w" class="UBBTitle"><img style="margin: 0px 2px -3px 0px" alt="E序代码" src="http://www.kuqin.com/upimg/allimg/070913/0001321.gif"> E序代码</div> <div id="wasck4c" class="UBBContent"><br>expression ::= term ( ('+' term) | ('-' term) )*<br>term ::= factor ( ('*' factor) | ('/' factor) )*<br>factor ::= REAL | '(' ex ')' | ('-' factor) | ('+' factor)<br></div></div><br>其中Qexpression是我们需要的表达式pattern。注意这里,正是׃expression首先d配termQ而term首先d配factorQ最后factor是以“纯数字”、“括号”、“正负符号”的序匚wQterm则是以factor、“乘法”、“除法”的序匚wQ而expression是以term、“加法”、“减法”的序匚wQ所以就保证了整个表辑ּ匚wq程是按照四则运的先后序q行的。在匚w的过E中只要安插各种“监视”的函数Qboost.spirit里面的术语叫做“actor”)Q就可以L实现四则q算?br><br>把EBNF对应到boost.spirit里,具体的grammercd现如下:<br> <div id="kqoggkc" class="UBBPanel"> <div id="i6a6s6o" class="UBBTitle"><img style="margin: 0px 2px -3px 0px" alt="E序代码" src="http://www.kuqin.com/upimg/allimg/070913/0001321.gif"> E序代码</div> <div id="qamemo6" class="UBBContent">struct calculator : public grammar<calculator><br>{<br>    template <typename ScannerT><br>    struct definition<br>    {<br>        definition(calculator const& /*self*/)<br>        {<br>            expression<br>                =   term<br>                >> *(   ('+' >> term[do_calc<do_add>()])<br>            |   ('-' >> term[do_calc<do_substract>()])<br>            )<br>                ;<br><br>            term<br>                =   factor<br>                >> *(   ('*' >> factor[do_calc<do_multiply>()])<br>            |   ('/' >> factor[do_calc<do_divide>()])<br>            )<br>                ;<br><br>            factor<br>                =   real_p[&push_real]<br>            |   '(' >> expression >> ')'<br>                |   ('-' >> factor[&do_neg])<br>            |   ('+' >> factor)<br>                ;<br>        }<br><br>        rule<ScannerT> expression, term, factor;<br><br>        rule<ScannerT> const&<br>            start() const { return expression; }<br>    };<br>};<br></div></div><br>h意,calculator从grammerzQ但是除了在内部定义了一个嵌套class以外Qƈ没有做更多的事情?br><br>至于q里面用到的do_calc<>、push_real{functor和函敎ͼ则是一些极为简单的东西Q实现如下:<br> <div id="8gqyyqg" class="UBBPanel"> <div id="4iaai2e" class="UBBTitle"><img style="margin: 0px 2px -3px 0px" alt="E序代码" src="http://www.kuqin.com/upimg/allimg/070913/0001321.gif"> E序代码</div> <div id="ceeoo44" class="UBBContent">namespace<br>{<br>    stack<double> calc_stack;<br><br>    struct do_add<br>    {<br>        double operator () (double lhs, double rhs) const<br>        {<br>            return lhs + rhs;<br>        }<br>    };<br><br>    struct do_substract<br>    {<br>        double operator () (double lhs, double rhs) const<br>        {<br>            return lhs - rhs;<br>        }<br>    };<br><br>    struct do_multiply<br>    {<br>        double operator () (double lhs, double rhs) const<br>        {<br>            return lhs * rhs;<br>        }<br>    };<br><br>    struct do_divide<br>    {<br>        double operator () (double lhs, double rhs) const<br>        {<br>            return lhs / rhs;<br>        }<br>    };<br><br>    template <typename op><br>    struct do_calc<br>    {<br>        void operator () (const char *, const char *) const<br>        {<br>            double result = calc_stack.top();<br>            calc_stack.pop();<br>            result = op()(calc_stack.top(), result);<br>            calc_stack.pop();<br>            calc_stack.push(result);<br>        }<br>    };<br><br>    void push_real(double d)<br>    {<br>        calc_stack.push(d);<br>    }<br><br>    void do_neg(char const*, char const*)  <br>    {<br>        cout << "NEGATE\n";<br>        double result = calc_stack.top();<br>        calc_stack.pop();<br>        calc_stack.push(-result);<br>    }<br><br>    double show_result()     <br>    {<br>        return calc_stack.top();<br>    }<br>}<br></div></div> <p><br>可以从源码中清晰看到Q这些函数就是简单的做了些出?入栈以及q算的工作,每一个函数功能极为单U,可认为就是一些状态及处理函数而已Q而状态机逻辑则由grammer搞定了?</p> <p>原文链接Q?a >http://www.realdodo.com/blog/article.asp?id=216</a></p></div></div></div></div></div></div></div></div><img src ="http://www.shnenglu.com/woaidongmao/aggbug/50116.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-05-17 00:16 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/05/17/50116.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一些常用的正则表达?/title><link>http://www.shnenglu.com/woaidongmao/archive/2008/05/10/49475.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Sat, 10 May 2008 14:25:00 GMT</pubDate><guid>http://www.shnenglu.com/woaidongmao/archive/2008/05/10/49475.html</guid><wfw:comment>http://www.shnenglu.com/woaidongmao/comments/49475.html</wfw:comment><comments>http://www.shnenglu.com/woaidongmao/archive/2008/05/10/49475.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/woaidongmao/comments/commentRss/49475.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/woaidongmao/services/trackbacks/49475.html</trackback:ping><description><![CDATA[<div class="sue22o4" id="csdnblog_allwrap"> <form language="javascript" id="Form1" name="Form1" action="http://blog.csdn.net/544214.aspx" method="post"> <div class="ugkssu8" id="csdnblog_midwrap"> <div class="6igyyk6" id="csdnblog_content"> <div id="ygyy8ac" class="gutter"> <div id="4oiiiac" class="default_contents"> <div id="cksskem" class="user_article"> <div id="um6qq24" class="blogstory"> <p><span style="color: #000000">"</span><span style="color: #000000">^\d+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">非负整数Q正整数 + 0Q?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[0-9]*[1-9][0-9]*$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">正整?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^((-\d+)|(0+))$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">非正整数Q负整数 + 0Q?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^-[0-9]*[1-9][0-9]*$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">负整?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^-?\d+$</span><span style="color: #000000">"</span><span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">整数 </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^\d+(\.\d+)?$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">非负点敎ͼ正QҎ + 0Q?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">正QҎ </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^((-\d+(\.\d+)?)|(0+(\.0+)?))$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">非正点敎ͼ负QҎ + 0Q?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">负QҎ </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^(-?\d+)(\.\d+)?$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">点?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[A-Za-z]+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">?6个英文字母组成的字符?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[A-Z]+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">?6个英文字母的大写l成的字W串 </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[a-z]+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">?6个英文字母的写l成的字W串 </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[A-Za-z0-9]+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">由数字和26个英文字母组成的字符?</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^\w+$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">由数字?6个英文字母或者下划线l成的字W串 </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$</span><span style="color: #000000">"</span><span style="color: #000000">    </span><span style="color: #008000">//</span><span style="color: #008000">email地址 </span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$</span><span style="color: #000000">"</span><span style="color: #000000">  </span><span style="color: #008000">//</span><span style="color: #008000">url</span><span style="color: #008000"><br><a target="_blank"></a><a target="_blank"></a></span><span style="color: #000000">/^</span><span style="color: #000000">(d</span><span id="Codehighlighter1_768_770_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"><a target="_blank"></a></span><span id="Codehighlighter1_768_770_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">2</span><span style="color: #000000">}</span></span><span style="color: #000000">|</span><span style="color: #000000">d</span><span id="Codehighlighter1_774_776_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"><a target="_blank"></a></span><span id="Codehighlighter1_774_776_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">4</span><span style="color: #000000">}</span></span><span style="color: #000000">)</span><span style="color: #000000">-</span><span style="color: #000000">((</span><span style="color: #000000">0</span><span style="color: #000000">([</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">9</span><span style="color: #000000">]</span><span id="Codehighlighter1_788_790_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"><a target="_blank"></a></span><span id="Codehighlighter1_788_790_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">}</span></span><span style="color: #000000">))</span><span style="color: #000000">|</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">[</span><span style="color: #000000">1</span><span style="color: #000000">|</span><span style="color: #000000">2</span><span style="color: #000000">]))</span><span style="color: #000000">-</span><span style="color: #000000">(([</span><span style="color: #000000">0</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">]([</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">9</span><span style="color: #000000">]</span><span id="Codehighlighter1_817_819_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"><a target="_blank"></a></span><span id="Codehighlighter1_817_819_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">}</span></span><span style="color: #000000">))</span><span style="color: #000000">|</span><span style="color: #000000">(</span><span style="color: #000000">3</span><span style="color: #000000">[</span><span style="color: #000000">0</span><span style="color: #000000">|</span><span style="color: #000000">1</span><span style="color: #000000">]))$</span><span style="color: #000000">/</span><span style="color: #000000">   </span><span style="color: #008000">//</span><span style="color: #008000">  q???/span><span style="color: #008000"><br><a target="_blank"></a><a target="_blank"></a></span><span style="color: #000000">/^</span><span style="color: #000000">((</span><span style="color: #000000">0</span><span style="color: #000000">([</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">9</span><span style="color: #000000">]</span><span id="Codehighlighter1_858_860_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"><a target="_blank"></a></span><span id="Codehighlighter1_858_860_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">}</span></span><span style="color: #000000">))</span><span style="color: #000000">|</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">[</span><span style="color: #000000">1</span><span style="color: #000000">|</span><span style="color: #000000">2</span><span style="color: #000000">]))</span><span style="color: #000000">/</span><span style="color: #000000">(([</span><span style="color: #000000">0</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">]([</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">9</span><span style="color: #000000">]</span><span id="Codehighlighter1_888_890_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"><a target="_blank"></a></span><span id="Codehighlighter1_888_890_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">}</span></span><span style="color: #000000">))</span><span style="color: #000000">|</span><span style="color: #000000">(</span><span style="color: #000000">3</span><span style="color: #000000">[</span><span style="color: #000000">0</span><span style="color: #000000">|</span><span style="color: #000000">1</span><span style="color: #000000">]))</span><span style="color: #000000">/</span><span style="color: #000000">(d</span><span id="Codehighlighter1_908_910_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"><a target="_blank"></a></span><span id="Codehighlighter1_908_910_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">2</span><span style="color: #000000">}</span></span><span style="color: #000000">|</span><span style="color: #000000">d</span><span id="Codehighlighter1_914_916_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"><a target="_blank"></a></span><span id="Codehighlighter1_914_916_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">4</span><span style="color: #000000">}</span></span><span style="color: #000000">)$</span><span style="color: #000000">/</span><span style="color: #000000">   </span><span style="color: #008000">//</span><span style="color: #008000"> ??q?/span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$</span><span style="color: #000000">"</span><span style="color: #000000">   </span><span style="color: #008000">//</span><span style="color: #008000">Emil</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?</span><span style="color: #000000">"</span><span style="color: #000000">     </span><span style="color: #008000">//</span><span style="color: #008000">电话L</span><span style="color: #008000"><br><a target="_blank"></a></span><span style="color: #000000">"</span><span style="color: #000000">^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$</span><span style="color: #000000">"</span><span style="color: #000000">   </span><span style="color: #008000">//</span><span style="color: #008000">IP地址</span><br></p> <p></p> <p>匚w中文字符的正则表辑ּQ?[\u4e00-\u9fa5]<br>匚w双字节字W?包括汉字在内)Q[^\x00-\xff]<br>匚wI的正则表辑ּQ\n[\s| ]*\r<br>匚wHTML标记的正则表辑ּQ?<(.*)>.*<\/\1>|<(.*) \/>/<br>匚w首尾I格的正则表辑ּQ?^\s*)|(\s*$)<br>匚wEmail地址的正则表辑ּQ\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*<br>匚w|址URL的正则表辑ּQ^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$<br>匚w帐号是否合法(字母开_允许5-16字节Q允许字母数字下划线)Q^[a-zA-Z][a-zA-Z0-9_]{4,15}$<br>匚w国内电话LQ?\d{3}-|\d{4}-)?(\d{8}|\d{7})?<br>匚w腾讯QQP^[1-9]*[1-9][0-9]*$<br></p> <p><u>下表是元字符及其在正则表辑ּ上下文中的行为的一个完整列表: <br></u><br><strong>\</strong> 下一个字W标Cؓ一个特D字W、或一个原义字W、或一个后向引用、或一个八q制转义W?br><br><strong>^</strong> 匚w输入字符串的开始位|。如果设|了 RegExp 对象的Multiline 属性,^ 也匹?’\n??’\r?之后的位|?<br><br><strong>$</strong> 匚w输入字符串的l束位置。如果设|了 RegExp 对象的Multiline 属性,$ 也匹?’\n??’\r?之前的位|?<br><br><strong>*</strong> 匚w前面的子表达式零ơ或多次?<br><br><strong>+</strong> 匚w前面的子表达式一ơ或多次? {h?{1,}?<br><br><strong>?</strong> 匚w前面的子表达式零ơ或一ơ? {h?{0,1}?<br><br><strong>{n}</strong> n 是一个非负整敎ͼ匚w定的n ơ?br><br><strong>{n,}</strong> n 是一个非负整敎ͼ臛_匚wn ơ?<br><br><strong>{n,m}</strong> m ?n 均ؓ非负整数Q其中n <= m。最匹?n ơ且最多匹?m ơ。在逗号和两个数之间不能有空根{?br><br><strong>?</strong> 当该字符紧跟在Q何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面Ӟ匚w模式是非贪婪的。非贪婪模式可能少的匹配所搜烦的字W串Q而默认的贪婪模式则尽可能多的匚w所搜烦的字W串?<br><br><strong>.</strong> 匚w?"\n" 之外的Q何单个字W。要匚w包括 ’\n?在内的Q何字W,请用象 ’[.\n]?的模式?<br><strong>(pattern)</strong> 匚wpattern q获取这一匚w?<br><br><strong>(?:pattern)</strong> 匚wpattern 但不获取匚wl果Q也是说这是一个非获取匚wQ不q行存储供以后用?<br><br><strong>(?=pattern)</strong> 正向预查Q在M匚w pattern 的字W串开始处匚w查找字符丌Ӏ这是一个非获取匚wQ也是_该匹配不需要获取供以后使用?<br><br><strong>(?!pattern)</strong> 负向预查Q与<strong>(?=pattern)</strong>作用相反 <br><br><strong>x|y</strong> 匚w x ?y?<br><br><strong>[xyz]</strong> 字符集合?<br><br><strong>[^xyz]</strong> 负值字W集合?<br><br><strong>[a-z]</strong> 字符范围Q匹配指定范围内的Q意字W?<br><br><strong>[^a-z]</strong> 负值字W范_匚wM不在指定范围内的L字符?<br><br><strong>\b</strong> 匚w一个单词边界,也就是指单词和空格间的位|?br><br><strong>\B</strong> 匚w非单词边界?<br><br><strong>\cx</strong> 匚w由x指明的控制字W?<br><br><strong>\d</strong> 匚w一个数字字W。等价于 [0-9]?<br><br><strong>\D</strong> 匚w一个非数字字符。等价于 [^0-9]?<br><br><strong>\f</strong> 匚w一个换늬。等价于 \x0c ?\cL?<br><br><strong>\n</strong> 匚w一个换行符。等价于 \x0a ?\cJ?<br><br><strong>\r</strong> 匚w一个回车符。等价于 \x0d ?\cM?<br><br><strong>\s</strong> 匚wMI白字符Q包括空根{制表符、换늬{等。等价于[ \f\n\r\t\v]?<br><br><strong>\S</strong> 匚wM非空白字W。等价于 [^ \f\n\r\t\v]?<br><br><strong>\t</strong> 匚w一个制表符。等价于 \x09 ?\cI?<br><br><strong>\v</strong> 匚w一个垂直制表符。等价于 \x0b ?\cK?<br><br><strong>\w</strong> 匚w包括下划U的M单词字符。等价于’[A-Za-z0-9_]’?<br><br><strong>\W</strong> 匚wM非单词字W。等价于 ’[^A-Za-z0-9_]’?<br><br><strong>\xn</strong> 匚w nQ其?n 为十六进制{义倹{十六进制{义值必Mؓ定的两个数字长?br><br><strong>\num</strong> 匚w numQ其中num是一个正整数。对所获取的匹配的引用?<br><br><strong>\n</strong> 标识一个八q制转义值或一个后向引用。如?\n 之前臛_ n 个获取的子表辑ּQ则 n 为后向引用。否则,如果 n 为八q制数字 (0-7)Q则 n Z个八q制转义倹{?<br><br><strong>\nm</strong> 标识一个八q制转义值或一个后向引用。如?\nm 之前臛_有is preceded by at least nm 个获取得子表辑ּQ则 nm 为后向引用。如?\nm 之前臛_?n 个获取,?n Z个后跟文?m 的后向引用。如果前面的条g都不满Q若 n ?m 均ؓ八进制数?(0-7)Q则 \nm 匹配八q制转义?nm?<br><br><strong>\nml</strong> 如果 n 为八q制数字 (0-3)Q且 m ?l 均ؓ八进制数?(0-7)Q则匚w八进制{义?nml?<br><br><strong>\un</strong> 匚w nQ其?n 是一个用四个十六q制数字表示的Unicode字符?<br></p> <p>匚w中文字符的正则表辑ּQ?[u4e00-u9fa5]</p> <p>匚w双字节字W?包括汉字在内)Q[^x00-xff]</p> <p>应用Q计字W串的长度(一个双字节字符长度?QASCII字符?Q?/p> <p>String.prototype.len=function(){return this.replace([^x00-xff]/g,"aa").length;}</p> <p>匚wI的正则表辑ּQn[s| ]*r</p> <p>匚wHTML标记的正则表辑ּQ?<(.*)>.*</1>|<(.*) />/ </p> <p>匚w首尾I格的正则表辑ּQ?^s*)|(s*$)</p> <p>应用Qjavascript中没有像vbscript那样的trim函数Q我们就可以利用q个表达式来实现Q如下:</p> <p>String.prototype.trim = function()<br>{<br>return this.replace(/(^s*)|(s*$)/g, "");<br>}</p> <p>利用正则表达式分解和转换IP地址Q?/p> <p>下面是利用正则表辑ּ匚wIP地址QƈIP地址转换成对应数值的JavascriptE序Q?/p> <p>function IP2V(ip)<br>{<br>re=/(d+).(d+).(d+).(d+)/g //匚wIP地址的正则表辑ּ<br>if(re.test(ip))<br>{<br>return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1<br>}<br>else<br>{<br>throw new Error("Not a valid IP address!")<br>}<br>}</p> <p>不过上面的程序如果不用正则表辑ּQ而直接用split函数来分解可能更单,E序如下Q?/p> <p>var ip="10.100.20.168"<br>ip=ip.split(".")<br>alert("IP值是Q?+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))</p> <p>匚wEmail地址的正则表辑ּQw+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*</p> <p>匚w|址URL的正则表辑ּQhttp://([w-]+.)+[w-]+(/[w- ./?%&=]*)?</p> <p>利用正则表达式去除字串中重复的字W的法E序Q?/p> <p>var s="abacabefgeeii"<br>var s1=s.replace(/(.).*1/g,"$1")<br>var re=new RegExp("["+s1+"]","g")<br>var s2=s.replace(re,"") <br>alert(s1+s2) //l果为:abcefgi</p> <p>我原来在CSDN上发贴寻求一个表辑ּ来实现去除重复字W的ҎQ最l没有找刎ͼq是我能惛_的最单的实现Ҏ。思\是用后向引用取出包括重复的字符Q再以重复的字符建立W二个表辑ּQ取C重复的字W,两者串q。这个方法对于字W顺序有要求的字W串可能不适用?/p> <p>得用正则表达式从URL地址中提取文件名的javascriptE序Q如下结果ؓpage1</p> <p>s="http://www.9499.net/page1.htm"<br>s=s.replace(/(.*/){0,}([^.]+).*/ig,"$2")<br>alert(s)</p> <p>利用正则表达式限制网表单里的文本框输入内容Q?/p> <p>用正则表辑ּ限制只能输入中文Qonkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"</p> <p>用正则表辑ּ限制只能输入全角字符Q?onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"</p> <p>用正则表辑ּ限制只能输入数字Qonkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"</p> <p>用正则表辑ּ限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"</p></div></div></div></div></div></div></form></div><img src ="http://www.shnenglu.com/woaidongmao/aggbug/49475.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2008-05-10 22:25 <a href="http://www.shnenglu.com/woaidongmao/archive/2008/05/10/49475.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC6中用STLPort4.6.2http://www.shnenglu.com/woaidongmao/archive/2008/05/07/49105.html肥仔肥仔Wed, 07 May 2008 02:53:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/05/07/49105.htmlhttp://www.shnenglu.com/woaidongmao/comments/49105.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/05/07/49105.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/49105.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/49105.html~译׃说了, |上q方面的文章很多

********************************************
如果和PlatformSDK 一起用的?br>要记的在STLport目录?\stlport\stl_user_config.h 文g?br>把这一句打开

     # define _STLP_NEW_PLATFORM_SDK 1

否则在编译时会有如下错误
second C linkage of overloaded function 'InterlockedIncrement' not allowed

********************************************

如果想静态链?STLPort 请在VC6?C/C++ \ General \ Preprocessor definitions
中添加宏 _STLP_USE_STATIC_LIB

********************************************

Ҏ使用IOSTREAM? 如果有问?br>在STLport目录?\stlport\stl_user_config.h 文g?br>把这一句打开?

    # define _STLP_NO_IOSTREAMS 1

********************************************

- When you erase an element from a hash_map only iterators to the erased element are invalidated
  so you can write something like:

while (it != myHashMap.end()) {
if (condition)
myHashMap.erase(it++); //q里Zq样不出?而把it++攑֤面就不行?br>    //在外面是对删除后的无效指针加, 而里面是Ҏ效指针加
else
++it;
}

  To finish STLport has a special debug mode to check such bad construction. Check the
  stl_user_config.h file in the stlport folder for that, the macro is _STLP_DEBUG.

肥仔 2008-05-07 10:53 发表评论
]]>
Boost.Pythonhttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48144.html肥仔肥仔Fri, 25 Apr 2008 09:54:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48144.htmlhttp://www.shnenglu.com/woaidongmao/comments/48144.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48144.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/48144.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/48144.htmlBoost.Python ?Boost 中的一个组Ӟ 使用它能够大大简化用 C++ ?Python 写扩展库的步骤,提高开发效率, 虽然目前它对 Python 嵌入 C++ 的支持还不是很多Q?但也能提供很大方ѝ另外, 华宇?/a>也编写了一份关?Boost.Python 明教E?/a>?

1 Boost 安装?/h3>

在正式开始?Boost.Python 之前Q?我们必须先编?Boost?首先?Boost 的官方站?/a> 下蝲 Boost 的源码包Q?把它们解压到你喜Ƣ的目录Qؓ~译做好准备?另外Q?在正式安?Boost.Python 之前Q?我们必须先正安?Python?

1.1 Linux 下的~译

首先切换?Boost 源码所在的路径Q?执行 ./configure 脚本Qؓ配置脚本提供 Python q行环境相应的参敎ͼ

./configure --with-python=/usr/bin/python \
            --with-python-version=2.4 \
            --with-python-root=/usr

然后Q?和绝大部?Linux E序一行, 执行 make 可以开始编译了。编译完毕后Q?切换?root 权限后再执行 make installQ把 Boost 相应的头文g和库文g复制到相应的地方Q?可以用了?

1.2 使用 MinGW + MSys ?Windows 下的~译

首先需要编译的?Boost 的编译工?bjamQ?直接?bjam 所在目录下Q??Boost 源码包所在目录下?\tools\build\jam_srcQ?执行 build.bat mingwQ稍{片刻, bjam.exe q译好了?把编译好?bjam.exe 复制C?%PATH% 路径能够直接扑ֈ的地方, 为后l的~译工作做好准备?

接下来, 切换?Boost 源码所在\径, 执行 bjam q行~译?我们需要提供关?Python 的一些参敎ͼ 变量 PYTHON_ROOT 指向 Python q行环境所在的目录Q变?PYTHON_VERSION 的gؓ Python 的版本号Q?如果你的 Python 安装路径与滇狐不同,请将相应的变量修改ؓ你机器上相应的\径, ~译命o行如下:

bjam.exe "-sTOOLS=mingw" "-sPYTHON_ROOT=E:\Python" "-sPYTHON_VERSION=2.4"

~译完毕后, 你将会在你的 C:\Boost 下找到编译得到的 Boost 相应头文件与库文Ӟ 你可以根据你的需要将它移动到别的地方备用?

2 使用 Boost.Python 嵌入 Python 模块?C++

Boost.Python 目前q没有提供完整的?Python 模块嵌入?C++ 的包装库Q因此许多工作我们还必须通过 Python C API 来进行?但是Q?利用 Boost.Python 中提供的一些模块, 能够l我们的工作带来极大便利?

2.1 修改模块加蝲路径Q装?Python 模块

与Q何一个其?Python 嵌入 C/C++ 的程序一P 我们需要在W一?#include 语句处含?Python.hQ?q在E序开始时调用 Py_Initialize()Q在E序l束时调?Py_Finalize()?

接下来, 我们便可以开始准备装?Python 模块了?Z?Python 解释器能够正地扑ֈ Python 模块所在的位置Q?我们需要将 Python 模块所在的路径d到模块搜索\径中Q添加搜索\径的 Python 语句如下Q?pre>import sys if not '/module/path' in sys.path: sys.path.append('/module/path')

我们使用 Python C API 执行cM的语句, p模块的搜烦路径d?Python 解释器中?d了搜索\径后Q?可以通过 PyImport_ImportModule 函数加蝲 Python 模块了?PyImport_ImportModule q回值是 PyObject *Q?Z避免手工处理J琐的引用计数等问题Q我们求助于 Boost.Python 提供?handle 模块Q??PyObject * 装hQ?以方便用, 代码如下Q?pre>#include <boost/python.hpp> ... boost::python::handle<>* _module; // Module handle. std::string path; // Path of the Python module. std::string module; // Module name. ... try { PyRun_SimpleString("import sys"); PyRun_SimpleString((std::string("if not '") + path + "' in sys.path: sys.path.append('" + path + "')").c_str()); _module = new boost::python::handle<>( PyImport_ImportModule((char *) module)); ... } catch (...) { PyErr_Print(); PyErr_Clear(); delete _module; _module = NULL; return false; } ...

需要注意的是, 通过 Python C API 加蝲?Python 解释器ƈ没有把当前\径列入默认的搜烦路径中。因此, 即你的 Python 模块存攑֜当前路径Q?你也必须使用上面的代码将当前路径d到搜索\径中之后Q才能通过 PyImport_ImportModule 加蝲到模块?

?Python 模块使用完毕或程序结束时Q?请?delete ?_module 指针释放Q?handle 被释攄时候会自动释放相应?Python 模块q回收相应资源?

2.2 调用 Python 函数

导入?Python 模块之后Q?调用 Python 函数非常容易了?Boost.Python 里封装了一个非常好用的模板函数 boost::python::call_methodQ它可以替你处理调用函数旉要处理的U种l节Q?你?Python C API 中繁琐的“将参数打包?PyObject *”?“构?Tuple”?“传?Tuple”?“解包返回值”等工作中彻底解攑և来, 你只需要这P

    boost::python::call_method<q回值类?gt;(模块指针, "Python 函数?,
        参数 1, 参数 2, ...);

模块指针可以通过我们前面得到?_module ?get Ҏ获得Q?例如Q?pre>... bool result; std::string config_file; ... try { return boost::python::call_method<bool>(_module->get(), "initialize", config_file); } catch (...) { PyErr_Print(); PyErr_Clear(); ... } ...

2.3 使用 Python cd?/h4>

使用 Python C API 调用 Python 函数和调?Python cd象是没有太大区别的,我们只需要调用类的构造方法, 得到一个类对象Q?然后把该cȝ指针看做模块指针Q按照前面调用普通函数的Ҏ调用cL员方法就可以了?例如Q?下列代码?_module 中创Z一?YukiSession 对象Q?然后调用了其中的 on_welcome Ҏ?除了展示调用cL员方法外Q?q段代码q展CZ构?Python list 对象??Python list 对象中获取元素的方式?pre> ... boost::python::handle<> _yukisession; ... // Retrieve the module handle and namespace handle. boost::python::object main_module(*_module); boost::python::object main_namespace = main_module.attr("__dict__"); // Call the method and get the object handle. _yukisession = boost::python::handle<>((PyRun_String( "YukiSession()", Py_eval_input, main_namespace.ptr(), main_namespace.ptr()))); ... // Compose a list. boost::python::list param; param.append(boost::python::str(_addr.get_host_addr())); param.append(boost::python::str()); // Call the method and retrieve the result. // Method is equivalent to: // "bool __thiscall YukiSession::on_welcome(list param);" result = boost::python::call_method<bool> (_yukisession.get(), "on_welcome", param); // Extract an item from a list. str = boost::python::call_method<std::string> (param.ptr(), "__getitem__", 1); ...

3 在嵌入的 Python 模块中调?C++ E序

通过动态链接库的方式?Boost.Python 导出 C++ 模块?Python E序与在 C++ 可执行程序中导出模块l嵌入的 Python 解释器, ~写E序的方式几乎是完全相同的。因此这里只单介l导出普通函数的ҎQ?惌l了解更多高U功能, 如导?C++ cR?导出可被 Python 重蝲的类{, 可以参看华宇煜的 Boost.Python 明教E?/a>或官?Boost.Python 文档?

3.1 导出 C++ 函数

首先使用 BOOST_PYTHON_MODULE 宏定义需要导出给 Python 的模块,然后?boost::python::def 语句定义导出的函数?参数列表以及 Doc StringQ?例如在下面的例子中, 我们导出了一?C++ 函数 yukigettextQƈ重命名ؓ gettextQ?pre>const char *yukigettext(const char *id); BOOST_PYTHON_MODULE(yuki) { boost::python::def("gettext", yukigettext, boost::python::args("id"), "Translate message."); }

3.2 ?Python 初始?C++ 模块

使用 BOOST_PYTHON_MODULE(name) 定义?Python 模块后,该宏会自动生成一个函?initnameQ?我们需要在 Py_Initialize() 之后调用q个自动生成的函敎ͼ 初始化导出到 Python 的模块?例如我们刚才导出模块用的?BOOST_PYTHON_MODULE(yuki)Q?因此初始化的时候就应该调用 inityuki()Q?pre>... Py_Initialize(); inityuki(); ...

3.3 ?Python 模块中调?C++ 模块

此时我们?Python 模块中只需要像普通的 Python 模块那样Q?导入的 C++ 模块?import 语句加蝲q来Q?可以调用了Q?pre>import yuki ... print yuki.gettext("This is a test!")

肥仔 2008-04-25 17:54 发表评论
]]>
对字W串cd和数值类型间q行转换问题的处理之Boostlglexical_casthttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48140.html肥仔肥仔Fri, 25 Apr 2008 09:08:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48140.htmlhttp://www.shnenglu.com/woaidongmao/comments/48140.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48140.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/48140.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/48140.html在CSDN论坛上经常看到询问如何在字符串类型和数值类型间q行转换的问题,也看C许多不同的答案。下面先讨论一下从字符串类型到数值类型的转换?
  1. 如何字W串"123"转换为intcd整数123Q答案是Q用标准C的库函数atoiQ?
  2. 如果要{换ؓlongcd呢?标准C的库函数atolQ?
  3. 如何?123.12"转换为doublecd呢?标准C的库函数atodQ?
  4. 如果要{换ؓlong doublecd呢?标准C的库函数atoldQ?
  5. …?

后来有朋友开始用标准库中的stringc,问这个如何{换ؓ数|有朋友答曎ͼ请先转换为const char*。我很佩服作{者有数学家的思维Q把陌生的问题{化成熟悉的问题。(曄有一则笑话,好事者问数学Ӟ知道如何烧水吗?{:知道。把水壶加满_点火烧。又问:如果水壶里已l有水了呢?{:先倒掉Q就转化为我熟悉的问题了……)

不,不,q样是C的做法,不是C++。那么,C++该怎么做呢Q用Boost Conversion Library所提供的函数lexical_castQ需要引入头文gboost/lexical_cast.hppQ无疑是最单方便的。如Q?

#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
        using boost::lexical_cast;
        int a = lexical_cast<int>("123");
        double b = lexical_cast<double>("123.12");
        std::cout<<a<<std::endl
        std::cout<<b<<std::endl;
        return 0;
}

一个函数就z地解决了所有的问题?

不知道诸位对C里的scanf/printfpd印象如何QM阿炯我肯定记不住那些E奇古怪的参数Q而且如果写错了参敎ͼ׃得到莫名其妙的输出结果,调试h可就要命了(我更讨厌的是字符数组Q空间开100呢,又怕太装不下Q开100000呢,总觉得太费Q心里憋气,好在C++标准为我们提供了stringq样的字W串c)。这时候,lexical_cast出来帮忙啦?

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
int main()
{
        using std::string;
        const double d = 123.12;
        string s = boost::lexical_cast<string>(d);
        std::cout<<s<<std::endl;
        return 0;
}

跟前面一L单?

3.3 异常

如果转换p|Q则会有异常bad_lexical_cast抛出。该异常cL标准异常cbad_cast的子cR?

#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
        using std::cout;
        using std::endl;
        int i;
        try{
                i = boost::lexical_cast<int>("abcd");
        }
        catch(boost::bad_lexical_cast& e)
        {
                cout<<e.what()<<endl;
                return 1;
        }
        cout<<i<<endl;
        return 0;
}

昄“abcd”ƈ不能转换Z个intcd的数|于是抛出异常Q捕捉后输出“bad lexical cast: source type value could not be interpreted as target”这L信息?

3.4 注意事项

lexical_cast依赖于字W流std::stringstreamQ会自动引入头文?a >4Q,其原理相当简单:把源cdd到字W流中,再写到目标类型中Q就大功告成。例?

int d = boost::lexical_cast<int>("123");

q当于

int d;
std::stringstream s;
s<<"123";
s>>d;

既然是用了字符,当然有些随之而来的问题,需要特别指?a >5?

  • ׃Visual C++ 6的本地化QlocaleQ部分实现有问题Q因此如果用了非默认的localeQ可能会莫名其妙地抛出异常。当Ӟ一般情况下我们q不需要去改变默认的localeQ所以问题不是很大?
  • 输入数据必须“完整”地转换Q否则抛出bad_lexical_cast异常。例?
int i = boost::lexical_cast<int>("123.123"); // this will throw 

便会抛出异常。因为?23.123”只能“部分”地转换?23Q不能“完整”地转换?23.123?

  • 点数的_ֺ问题?
std::string s = boost::lexical_cast<std::string>(123.1234567);

以上语句预想的结果是得到?23.1234567”,但是实际上我们只会得到?23.123”,因ؓ默认情况下std::stringstream的精度是6Q这是C语言E序库中的“前辈”printf留下的传l)。这可以说是boost::lexical_cast的一个bug。怎么办呢Q权宜之计,可以q么做:打开头文?lt;boost/lexical_cast.hpp>Q注意对照修?a >6Q?

#include <boost/limits.hpp>
//...
template<typename Target, typename Source>
Target lexical_cast(Source arg) {
        //...
        Target result; 
        interpreter.precision(std::numeric_limits<Source>::digits10);
        if( !(interpreter << arg) ||
        !(interpreter >> result) ||
        !(interpreter >> std::ws).eof())
        //...
}

卛_得到正确l果。当Ӟ理论上效率会有一点点损失Q不q几乎可以忽略不计?

4 结

我们已经体验了boost::lexcial_cast。当Ӟlexical_cast不仅仅局限于字符串类型与数值类型之间的转换Q可在Q意可输出到stringstream的类型和L可从stringstream输入的类型间转换。这ơ的了解管很粗略,不过毕竟我们已经“走qBoost”,而不仅仅是“走q”。以后,我们可以自行领略Boost的动Z处啦?

肥仔 2008-04-25 17:08 发表评论
]]>
STLPort 5.1.5 ~译http://www.shnenglu.com/woaidongmao/archive/2008/04/25/48093.html肥仔肥仔Thu, 24 Apr 2008 16:56:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48093.htmlhttp://www.shnenglu.com/woaidongmao/comments/48093.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/04/25/48093.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/48093.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/48093.html

STLPort 5.1.5 下蝲

http://sourceforge.net/project/showfiles.php?group_id=146814

Bat文g

 

cd c:\Program Files\Microsoft Visual Studio\VC98\bin
pause

call VCVARS32.BAT
pause

rem cd C:\STLport-5.1.5\STLport-5.1.5\build\Makefiles\nmake
cd C:\STLport-5.1.5\STLport-5.1.5\build\lib
pause
call configure --help
pause
call configure -c msvc6
pause
nmake /f msvc.mak
nmake /f msvc.mak install

rem nmake -f vc6.mak clean all
rem nmake -f vc6.mak install

pause
exit



肥仔 2008-04-25 00:56 发表评论
]]>
boost bathttp://www.shnenglu.com/woaidongmao/archive/2008/04/24/48049.html肥仔肥仔Thu, 24 Apr 2008 10:53:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/04/24/48049.htmlhttp://www.shnenglu.com/woaidongmao/comments/48049.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/04/24/48049.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/48049.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/48049.htmlSET INCLUDE=\"D:\\STLport-4.6.2\\stlport\";%INCLUDE%
SET LIB=\"D:\\STLport-4.6.2\\lib\";%LIB%
SET MSVC_ROOT="C:\Program Files\Microsoft Visual Studio\VC98"
SET VISUALC="C:\Program Files\Microsoft Visual Studio\VC98"
SET JAM_TOOLSET=VISUALC
SET PYTHON_ROOT=C:\Python22
SET PYTHON_VERSION=2.2
SET STLPORT_PATH=C:\STLport-4.6.2\lib
SET STLPORT_VERSION=4.6.2
rem pause
cd c:\Program Files\Microsoft Visual Studio\VC98\bin
pause
call VCVARS32.BAT
pause
cd C:\boost_1_33_1\libs\regex\build
pause
rem call nmake -f vc6-stlport.mak
pause
cd C:\boost_1_33_1
pause
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc --with-thread stage
rem msvc-stlport-tools
bjam -sBOOST_ROOT=. -sTOOLS=msvc-stlport --with-thread stage
rem quit
exit

rem 版本Qboost 1.33.1 STLport 5.0 / VS.net 2003 没有Ҏ讄Q除了程序的debug build应加_STLP_DEBUG宏定义?br>rem boost build procedure:
SET INCLUDE=\"D:\\STLport_50\\stlport\";%INCLUDE%
SET LIB=\"D:\\STLport_50\\lib\";%LIB%

SET PYTHON_ROOT=\"D:\\Python24\"
SET PYTHON_VERSION=2.4.2
SET STLPORT_PATH=\"D:\\\"
SET STLPORT_50_PATH=\"D:\\STLport_50\"
SET STLPORT_VERSION=5.0
SET STLPORT_VERSIONS=5.0
SET VC71_ROOT=\"d:\\Program Files\\Microsoft Visual Studio .NET 2003\\Vc7\"

bjam.exe -sBOOST_ROOT=. -sTOOLS=vc-7_1 stage
bjam.exe -sBOOST_ROOT=. -sTOOLS=vc-7_1-stlport stage

如果不编译python库,最后两行改?

bjam.exe -sBOOST_ROOT=. -sTOOLS=vc-7_1 --without-python stage
bjam.exe -sBOOST_ROOT=. -sTOOLS=vc-7_1-stlport --without-python stage

pause
cd c:\boost_1_33_1
pause
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc "-sBUILD=debug release static/dynamic
pause
cd C:\Program Files\Tencent\TM
pause
start TMShell.exe
pause



肥仔 2008-04-24 18:53 发表评论
]]>
boost ~译http://www.shnenglu.com/woaidongmao/archive/2008/04/19/47559.html肥仔肥仔Fri, 18 Apr 2008 17:55:00 GMThttp://www.shnenglu.com/woaidongmao/archive/2008/04/19/47559.htmlhttp://www.shnenglu.com/woaidongmao/comments/47559.htmlhttp://www.shnenglu.com/woaidongmao/archive/2008/04/19/47559.html#Feedback0http://www.shnenglu.com/woaidongmao/comments/commentRss/47559.htmlhttp://www.shnenglu.com/woaidongmao/services/trackbacks/47559.htmlBoost历史版本下蝲地址

http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041

 

bat文gQboost 1.33.1 Python 2.2.1,可能需要重新安装stl port, stl port版本有问?nbsp;

::如果不要~译bjam.exe的话
rem GOTO install

cd C:\boost_1_33_1\tools\build\jam_src
pause
call build.bat
pause
copy C:\boost_1_33_1\tools\build\jam_src\bin.ntx86\bjam.exe C:\boost_1_33_1
pause

:install

rem SET INCLUDE=\"C:\\STLport-4.6.2\\stlport\";%INCLUDE%
rem SET LIB=\"C:\\STLport-4.6.2\\lib\";%LIB%
SET MSVC_ROOT="C:\Program Files\Microsoft Visual Studio\VC98"
SET VISUALC="C:\Program Files\Microsoft Visual Studio\VC98"
SET JAM_TOOLSET=VISUALC
SET PYTHON_ROOT=C:\Python22
SET PYTHON_VERSION=2.2
SET STLPORT_PATH=C:\STLport-4.6.2\lib
SET STLPORT_VERSION=4.6.2
cd c:\Program Files\Microsoft Visual Studio\VC98\bin
pause

call VCVARS32.BAT
pause

cd C:\boost_1_33_1
pause

::stage代表 lib
::msvc-stlport是msvc-stlport-tools.jam文gQ可以搜索得?br>::--with-thread是只~译thread库,--without-thread则相?br>::比如~译thread 的lib库如下行.
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc --with-thread stage

::~译所?br>bjam -sBOOST_ROOT=. -sTOOLS=msvc-stlport install
bjam -sBOOST_ROOT=. -sTOOLS=msvc install

::~译regex
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc-stlport --with-regex install
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc --with-regex install

::~译thread
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc-stlport --with-thread install
rem bjam -sBOOST_ROOT=. -sTOOLS=msvc --with-thread install

pause
exit

 

 

 

1、下载python-2.5.2.msi安装到C:\Python25

http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi

2、下载boost_1_35_0Q解压到C:\boost_1_35_0

http://sourceforge.net/project/downloading.php?group_id=7586&use_mirror=nchc&filename=boost_1_35_0.zip&48604828

3、执行C:\boost_1_35_0\tools\jam\build_dist.bat

4、生成了bjam.exe在:C:\boost_1_35_0\tools\jam\src\bin.ntx86\bjam.exe

5、拷贝bjam.exe到C:\boost_1_35_0

6、打开cmdQ执行:cd C:\boost_1_35_0

7、执行bjam --toolset=msvc -sPYTHON_ROOT="{C:\Python25}" install

bjam --toolset=msvc-6.0 stdlib=stlport -sPYTHON_ROOT="{C:\Python25}" install

{待半个时左右

8、将C:\boost_1_35_0\libs文g夹(里面是源代码Qlib_sourceQ,拯到C:\BoostQƈ更改名ؓboost_src,q样成了C:\Boost\boost_src

9、把C:\Boost\include\boost-1_35_0\boostQ加入到VC6的Include路径,hpp也是C++的头文g啊!Boost库就是用的这个?

10、将C:\Boost\include\boost-1_35_0\boost和C:\Boost\boost_srcQ加入到VC6的source路径

11、把C:\Boost\libQ加入到VC6的LIB路径

12、C:\boost_1_35_0q个文g夹可以删除了Q如果不惛_除,那么C:\boost_1_35_0\bin.v2q个是编译时候的obj{文Ӟ可以删除掉?

q行出现的错?

1、预定义头要加入Q?D_STLP_DEBUG,__STL_DEBUG

2、ZM limit 错误Q工E?>讄->C/C++选项卡下面有“工E选项”的一个命令区在命令行的末֊?Zm1000Q改?Zm2000也行Q是整百的都可以

3、“compiler   is   out   of   heap   space”,\ZMҎ2000(最大了)

4、“Fatal   Error   C1063”,q个错误是编译器栈溢出,可能是你的程序单个源文g太大了,拆分成小一些的文g试试  
      MSDN上:  
      Fatal   Error   C1063  
     compiler   limit   :   compiler   stack   overflow  
      The   program   was   too   complex,   possibly   due   to   recursive   include   files.   Split   the   code   into   smaller   source   files   and   recompile.

手动~译boost::regex

1、cmd;

2、cd C:\boost_1_35_0\libs\regex\build

3、vcvars32.bat

4、SET STLPORT_PATH=C:\STLport\lib(如果需要以STLPort~译的话)

5、nmake -f vc6-stlport.makQSTLPort~译Q或?nmake -f vc6.makQ普通编译)

用来试的代码,建立一个Win32 Console ApplicationQ?

#include "stdafx.h"
#include <iostream>
#include <boost/regex.hpp>
#include <boost/thread.hpp>
int main()

    // 3 digits, a word, any character, 2 digits or "N/A", 
    // a space, then the first word again
    boost::regex reg("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");   
    std::string correct="123Hello N/A Hello"; 
    std::string incorrect="123Hello 12 hello";   
    assert(boost::regex_match(correct,reg)==true); 
    assert(boost::regex_match(incorrect,reg)==false);
    boost::regex reg1("(new)|(delete)");
    boost::smatch m;std::string s=  "Calls to new must be followed by delete. \  Calling simply new results in a leak!";
    if (boost::regex_search(s,m,reg1))
    {  // Did new match? 
        if (m[1].matched)   
            std::cout << "The expression (new) matched!\n"; 
        if (m[2].matched)   
            std::cout << "The expression (delete) matched!\n";
    }
    return 0;
}

如果一切顺利通过Q恭喜你Qboost安装成功?

vc6 上安装stlport和boost?/a>

用户头像

1
vc 6   sp5补丁  预处理补?br>python2.2.3
stlport4.5.3
boost1.3.0.2
2
使用stlport的iostreamQboost.python~译p|
如果用stlportt的iostreamQboost.regex~译p|
选择boost1.3.0.2的原?
?br>http://engineering.meta-comm.com/boost.aspx
得知Boost-wide regression reports
vc6基本支持boost1.3.0.2 的全部库
而boost以后的版本,vc6 的表现可以说是惨不忍睏V?br>boost1.3.0.2支持 python2.2 stlport 4.5.3
基本是q个样子了?/p>

肥仔 2008-04-19 01:55 发表评论
]]>
޹һɾþþƷۺ| þѹƷ| ɫþþþþþС˵| 99þþþ| ݺɫþ| þþþþþþþþþĻ| Ʒþþþ| ŷaƬѿþ| ھƷþþþþþɬ| þ99Ʒ99þ| 97þóƷ2021| þþƷ| 츾þþ | պƷþþվ| Ҫþðѹۿ| ޹徫Ʒ߾þ | þþƷAVɫ| þһѵ| þˬˬƬAV| ձvaҹĻþ | 츾þþ| һɫþHEZYO| þþþavר | þþƷݲݲ| ƷVIDEOSSEXþ÷| 91ƷۺϾþ㽶 | 99þùƷһ | þþƷһ| Ʒþˬ | þþþþҹӰԺ | պݺݾþ͵͵ɫۺ96| þþþþþ| ۺϾþĻӰ| Ʒʾþþþ999Ұ| ƷŷƬþùŷ| ձɫվWWWþ| ۺɫ77777þ| ۺϾþۺϼþ| ŷһþþþþþôƬ| AVþþƷݺݰ˳| þþƷ91þ鶹|