{"id":272,"date":"2004-03-28T11:31:47","date_gmt":"2004-03-28T11:31:47","guid":{"rendered":"http:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2004\/03\/28\/%e5%90%91%e4%be%af%e6%8d%b7%e8%ab%8b%e5%95%8fstl-sort%e7%9a%84%e5%95%8f%e9%a1%8c\/"},"modified":"2004-03-28T11:31:47","modified_gmt":"2004-03-28T11:31:47","slug":"%e5%90%91%e4%be%af%e6%8d%b7%e8%ab%8b%e5%95%8fstl-sort%e7%9a%84%e5%95%8f%e9%a1%8c","status":"publish","type":"post","link":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2004\/03\/%e5%90%91%e4%be%af%e6%8d%b7%e8%ab%8b%e5%95%8fstl-sort%e7%9a%84%e5%95%8f%e9%a1%8c\/","title":{"rendered":"\u5411\u4faf\u6377\u8acb\u554fSTL sort\u7684\u554f\u984c"},"content":{"rendered":"<p>\u9019\u662f\u883b\u4e45\u4ee5\u524d\u7684\u4e8b\u60c5\u4e86,\u90a3\u6642\u5019\u70ba\u4e86\u9019\u500b sort \u7684\u554f\u984c,\u767c mail \u5411\u4faf\u6377\u5148\u751f\u8acb\u6559.\u96d6\u7136\u9694\u4e86\u5f88\u4e45\u624d\u56de\u8986\u6211,\u4e0d\u904e\u5fc3\u88e1\u9084\u662f\u5f88\u611f\u52d5~~\u4e5f\u624d\u77e5\u9053\u81ea\u5df1\u7684\u7a0b\u5f0f\u51fa\u4e86\u4ec0\u9ebc\u932f.<\/p>\n<blockquote><p>\n\u56de\u8986\u65e5\u671f:2003\u5e742\u670820\u65e5 \u4e0a\u5348 03:38<br \/>\n\u4f60\u7684\u7a0b\u5f0f\u5beb\u7684\u908f\u8f2f\u4e0d\u5c0d\u3002<br \/>\n&gt; bool operator==( const myInt&amp; l, const myInt&amp; r) {<br \/>\n&gt; bool operator&gt;( const myInt&amp; l, const myInt&amp; r) {<br \/>\n&gt; bool operator&lt;( const myInt&amp; l, const myInt&amp; r) {<br \/>\n\u9019\u4e09\u500b\u90fd\u6c92\u6709\u5b8c\u6574\u5224\u65b7\u3002sort \u7684\u6642\u5019\uff0c\u8981\u6c42 class \u9700\u5b9a\u7fa9 operator&lt;\uff0c<br \/>\n\u800c operator&lt; \u5728\u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u50b3\u56de 0, \u4ec0\u9ebc\u6642\u5019\u50b3\u56de \u6b63\u503c\uff0c\u4ec0\u9ebc\u6642\u5019\u50b3\u56de\u8ca0\u503c\uff0c\u6709\u4e00\u5b9a\u7684\u898f\u5247\u3002<br \/>\n\u554f\u984c\u4e0d\u662f\u51fa\u5728\u4f60\u6307\u7684\u5730\u65b9\uff0c\u800c\u5728\u9019\u88e1\u3002<br \/>\n&#8212; jjhou<br \/>\n&#8212;&#8211; Original Message &#8212;&#8211;<br \/>\n\u5bc4\u4ef6\u8005: &quot;\u664f\u4ec1&quot;<br \/>\n\u6536\u4ef6\u8005:<br \/>\n\u50b3\u9001\u65e5\u671f: 2002\u5e7410\u67088\u65e5 PM 11:07<br \/>\n\u4e3b\u65e8: \u8acb\u6559 STL sort() \u7684\u554f\u984c<br \/>\n&gt; Dear \u4fafsir:<br \/>\n&gt;<br \/>\n&gt; \u5192\u6627\u8acb\u6559,\u4ee5\u4e0b\u662f\u7a0b\u5f0f,\u800c\u554f\u984c\u5728\u6700\u5f8c\u9762~<br \/>\n&gt;<br \/>\n&gt; #include &lt;iostream&gt;<br \/>\n&gt; #include &lt;algorithm&gt;<br \/>\n&gt; #include &lt;vector&gt;<br \/>\n&gt;<br \/>\n&gt; using namespace std;<br \/>\n&gt;<br \/>\n&gt; class myInt {<br \/>\n&gt; protected:<br \/>\n&gt; &nbsp;&nbsp;int id;<br \/>\n&gt; public:<br \/>\n&gt; &nbsp;&nbsp;myInt():id(0) {};<br \/>\n&gt; &nbsp;&nbsp;explicit myInt( int i ):id(i) {};<br \/>\n&gt; &nbsp;&nbsp;myInt( const myInt&amp; i ):id(i.id) { };<br \/>\n&gt; &nbsp;&nbsp;int getId( void ) const {<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;return id;<br \/>\n&gt; &nbsp;&nbsp;}<br \/>\n&gt; &nbsp;&nbsp;myInt&amp; operator=( myInt&amp; i) {<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;id=i.id;<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;return *this;<br \/>\n&gt; &nbsp;&nbsp;}<br \/>\n&gt; &nbsp;&nbsp;myInt&amp; operator=( int i ) {<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;id=i;<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;return *this;<br \/>\n&gt; &nbsp;&nbsp;}<br \/>\n&gt; &nbsp;&nbsp;myInt&amp; operator*() { return *this; }<br \/>\n&gt; &nbsp;&nbsp;myInt* operator-&gt;() { return this; }<br \/>\n&gt; &nbsp;&nbsp;friend bool operator==( const myInt&amp; l, const myInt&amp; r );<br \/>\n&gt; &nbsp;&nbsp;friend bool operator &nbsp;&nbsp;friend bool operator&gt;( const myInt&amp; l, const myInt&amp; r );<br \/>\n&gt; };<br \/>\n&gt;<br \/>\n&gt; bool operator==( const myInt&amp; l, const myInt&amp; r) {<br \/>\n&gt; &nbsp;&nbsp;if( l.getId() == r.getId() )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;return true;<br \/>\n&gt; }<br \/>\n&gt;<br \/>\n&gt; bool operator&gt;( const myInt&amp; l, const myInt&amp; r) {<br \/>\n&gt; &nbsp;&nbsp;if( l.getId() &gt; r.getId() )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;return true;<br \/>\n&gt; }<br \/>\n&gt; bool operator &nbsp;&nbsp;if( l.getId()  &nbsp;&nbsp;&nbsp;&nbsp;return true;<br \/>\n&gt; }<br \/>\n&gt;<br \/>\n&gt; class myIntCompare {<br \/>\n&gt; public:<br \/>\n&gt; &nbsp;&nbsp;int operator()( myInt aa, myInt bb ) {<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;if( aa.getId() &gt; bb.getId() )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;if( aa.getId() == bb.getId() )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;if( aa.getId()  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<br \/>\n&gt; &nbsp;&nbsp;}<br \/>\n&gt; };<br \/>\n&gt;<br \/>\n&gt; int<br \/>\n&gt; main( int argc, char* argv[] ) {<br \/>\n&gt; &nbsp;&nbsp;const int COUNTS=10000;<br \/>\n&gt; &nbsp;&nbsp;typedef vector intVector_t;<br \/>\n&gt; &nbsp;&nbsp;intVector_t intVector;<br \/>\n&gt; &nbsp;&nbsp;int i;<br \/>\n&gt;<br \/>\n&gt; &nbsp;&nbsp;\/**<br \/>\n&gt; &nbsp;&nbsp;&nbsp;* \u4e0b\u9762\u7684\u5169\u884c code \u6703\u5c0e\u81f4 segmentation fault,\u9019\u5c31\u662f\u6211\u8981\u554f\u7684\u554f\u984c!!<br \/>\n&gt; &nbsp;&nbsp;&nbsp;* for( i=COUNTS;i&gt;=0;&#8211;i )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;*      intVector.push_back( myInt(i) );<br \/>\n&gt; &nbsp;&nbsp;&nbsp;*\/<br \/>\n&gt; &nbsp;&nbsp;intVector.reserve( COUNTS );<br \/>\n&gt; &nbsp;&nbsp;for( i=COUNTS; i&gt;=0; &#8211;i )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;intVector[i]=i;<br \/>\n&gt;<br \/>\n&gt; &nbsp;&nbsp;\/\/ \u4ee5\u4e0b\u5169\u7a2e\u5beb\u6cd5\u90fd\u53ef\u4ee5<br \/>\n&gt; &nbsp;&nbsp;sort( intVector.begin(), intVector.end(), myIntCompare() );<br \/>\n&gt; &nbsp;&nbsp;\/\/sort( intVector.begin(), intVector.end() );<br \/>\n&gt;<br \/>\n&gt; &nbsp;&nbsp;for( i=0; i &nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; intVector[i].getId() &lt; }<br \/>\n&gt;<br \/>\n&gt; \u70ba\u4ec0\u9ebc\u628a<br \/>\n&gt; &nbsp;&nbsp;intVector.reserve( COUNTS );<br \/>\n&gt; &nbsp;&nbsp;for( i=COUNTS; i&gt;=0; &#8211;i )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;intVector[i]=i;<br \/>\n&gt;<br \/>\n&gt; \u6539\u6210<br \/>\n&gt; &nbsp;&nbsp;for( i=COUNTS;i&gt;=0;&#8211;i )<br \/>\n&gt; &nbsp;&nbsp;&nbsp;&nbsp;intVector.push_back( myInt(i) );<br \/>\n&gt;<br \/>\n&gt; \u7de8\u8b6f\u5f8c\u518d\u57f7\u884c<br \/>\n&gt; \u5c31\u6703\u767c\u751f segmentation fault \u7684\u554f\u984c\u5462??<br \/>\n&gt; \u771f\u7684\u662f\u5f88\u5947\u602a<br \/>\n&gt; \u6211\u77e5\u9053 vector \u5728\u914d\u7f6e\u7684\u6642\u5019\u6703\u5148\u9810\u7559\u4e00\u584a\u8a18\u61b6\u9ad4<br \/>\n&gt; \u5728 push_back() \u7684\u6642\u5019,\u5982\u679c\u767c\u73fe\u4e0d\u5920\u5927,\u6703\u81ea\u52d5\u91cd\u65b0\u914d\u7f6e,<br \/>\n&gt; \u4e26\u628a\u539f\u4f86\u7684\u5167\u5bb9\u642c\u5230\u65b0\u7684\u5730\u65b9\u53bb.<br \/>\n&gt; \u6211\u60f3\u53ef\u80fd\u662f\u91cd\u65b0\u914d\u7f6e\u7684\u554f\u984c~~<br \/>\n&gt; \u4f46\u537b\u4e0d\u77e5\u9053\u771f\u6b63\u7684\u539f\u56e0\u662f\u4ec0\u9ebc?<br \/>\n&gt; \u5e0c\u671b\u4faf sir \u80fd\u7d66\u6211\u4e00\u4e9b\u65b9\u5411,\u8b93\u6211\u4e0b\u624b\u53bb\u627e\u51fa\u7b54\u6848~<br \/>\n&gt;<br \/>\n&gt; \u5594,\u5c0d\u4e86,\u6211\u7684\u74b0\u5883\u662f linux g++ 2.96.<br \/>\n&gt;<br \/>\n&gt; With Best Regards.\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u9019\u662f\u883b\u4e45\u4ee5\u524d\u7684\u4e8b\u60c5\u4e86,\u90a3\u6642\u5019\u70ba\u4e86\u9019\u500b sort \u7684\u554f\u984c,\u767c mail \u5411\u4faf\u6377\u5148\u751f\u8acb\u6559.\u96d6\u7136\u9694\u4e86\u5f88\u4e45\u624d\u56de\u8986\u6211,\u4e0d\u904e\u5fc3\u88e1\u9084\u662f\u5f88\u611f\u52d5~~\u4e5f\u624d\u77e5\u9053\u81ea\u5df1\u7684\u7a0b\u5f0f\u51fa\u4e86\u4ec0\u9ebc\u932f. \u56de\u8986\u65e5\u671f:2003\u5e742\u670820\u65e5 \u4e0a\u5348 03:38 \u4f60\u7684\u7a0b\u5f0f\u5beb\u7684\u908f\u8f2f\u4e0d\u5c0d\u3002 &gt; bool operator==( const myInt&amp; l, const myInt&amp; r) { &gt; bool operator&gt;( const myInt&amp; l, const myInt&amp; r) { &gt; bool operator&lt;( const myInt&amp; l, const myInt&amp; r) { \u9019\u4e09\u500b\u90fd\u6c92\u6709\u5b8c\u6574\u5224\u65b7\u3002sort \u7684\u6642\u5019\uff0c\u8981\u6c42 class \u9700\u5b9a\u7fa9 operator&lt;\uff0c \u800c operator&lt; \u5728\u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u50b3\u56de 0, \u4ec0\u9ebc\u6642\u5019\u50b3\u56de \u6b63\u503c\uff0c\u4ec0\u9ebc\u6642\u5019\u50b3\u56de\u8ca0\u503c\uff0c\u6709\u4e00\u5b9a\u7684\u898f\u5247\u3002 \u554f\u984c\u4e0d\u662f\u51fa\u5728\u4f60\u6307\u7684\u5730\u65b9\uff0c\u800c\u5728\u9019\u88e1\u3002 &#8212; jjhou &#8212;&#8211; Original &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2004\/03\/%e5%90%91%e4%be%af%e6%8d%b7%e8%ab%8b%e5%95%8fstl-sort%e7%9a%84%e5%95%8f%e9%a1%8c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\u5411\u4faf\u6377\u8acb\u554fSTL sort\u7684\u554f\u984c&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-cc"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2MOxp-4o","_links":{"self":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":0,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"wp:attachment":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}