锘??xml version="1.0" encoding="utf-8" standalone="yes"?>精品午夜久久福利大片,久久精品国产久精国产一老狼,久久久国产亚洲精品http://www.shnenglu.com/colorful/category/19318.htmlzc qq:1337220912zh-cnFri, 16 Oct 2015 03:01:59 GMTFri, 16 Oct 2015 03:01:59 GMT60STL鐨剅emove鍑芥暟鍜宭ist鐨剅emove鎴愬憳鍑芥暟http://www.shnenglu.com/colorful/archive/2015/10/14/212010.html澶氬僵浜虹敓澶氬僵浜虹敓Wed, 14 Oct 2015 07:50:00 GMThttp://www.shnenglu.com/colorful/archive/2015/10/14/212010.htmlhttp://www.shnenglu.com/colorful/comments/212010.htmlhttp://www.shnenglu.com/colorful/archive/2015/10/14/212010.html#Feedback1http://www.shnenglu.com/colorful/comments/commentRss/212010.htmlhttp://www.shnenglu.com/colorful/services/trackbacks/212010.htmlhttp://www.cnblogs.com/kinuxroot/archive/2013/01/25/stl_remove_problem.html

浠婂ぉ鐪嬩功鍒氬垰鐪嬬殑錛屽氨璁板綍涓嬫潵鍚с傝繖鍙兘鏄佺敓甯歌皥浜嗭紝鏉冧笖浣滀負涓涓閱掔殑渚嬪瓙鍚с?/p>

澶у閮界煡閬揝TL鏈変袱涓潪甯擱噸瑕佺殑緇勬垚閮ㄥ垎錛屽鍣ㄥ拰綆楁硶銆?/p>

綆楁硶灝辨槸涓涓釜鐨勫嚱鏁幫紝閫氳繃榪唬鍣ㄥ拰瀹瑰櫒鍏寵仈鍦ㄤ竴璧鳳紝瀹屾垚涓浜涘伐浣溿?/p>

綆楁硶鍜屽鍣ㄧ殑鍒嗙涓虹▼搴忚璁℃彁渚涗簡寰堝ぇ鐨勭伒媧繪э紝浣嗘槸涔熷甫鏉ヤ簡涓浜涜礋闈㈡晥鏋滐紝涓嬮潰鎴戣鐨勮繖涓棶棰樺氨鏄竴涓緥瀛愩?/p>

STL鐨勭畻娉曢噷鏈変竴涓猺emove鍑芥暟錛岃宭ist鑷韓涔熸湁涓涓猺emove鍑芥暟錛屽姛鑳介兘鏄竴鏍風殑錛岀Щ闄ゆ煇涓涓厓绱狅紝閭f垜浠簲璇ヤ嬌鐢ㄥ摢涓涓憿錛?/p>

鐪嬩竴涓嬩笅闈㈣繖孌電▼搴?/p>

澶嶅埗浠g爜
 1     list<int> numbers;  2   3     for ( int number = 0; number <= 6; number ++ ) {  4         numbers.push_front(number);  5         numbers.push_back(number);  6     }  7   8     copy(numbers.begin(), numbers.end(),  9             ostream_iterator<int>(cout, " ")); 10     cout << endl; 11  12     // remove algorithm will remove element but not erase the element from container 13     // it will return the logical desination of container 14     list<int>::iterator endOfNumbers = remove(numbers.begin(), numbers.end(), 3); 15  16     copy(numbers.begin(), numbers.end(), 17             ostream_iterator<int>(cout, " ")); 18     cout << endl;
澶嶅埗浠g爜

杈撳嚭鏄粈涔堝憿錛?/p>

絎竴琛岃偗瀹氭槸6 5 4 3 2 1 0 0 1 2 3 4 5 6錛岄偅涔堢浜岃浼氳緭鍑轟粈涔堬紵

濡傛灉鏄病鏈変粩緇嗙湅榪嘢TL鐨勪漢鑲畾浼氳涓簉emove(number.begin(), numbers.end(), 3)浼氱Щ闄ゆ墍鏈夊間負3鐨勫厓绱犮傛墍浠ヨ緭鍑烘槸錛? 5 4 2 1 0 0 1 2 4 5 6銆?/p>

浣嗘槸錛屾垜浠湅涓涓嬪畠鐪熸鐨勮緭鍑猴細

6 5 4 2 1 0 0 1 2 4 5 6 5 6

浣犲彲鑳戒細闈炲父鎯婅錛屼負浠涔堟渶鍚庝細澶氬嚭5鍜?涓や釜鏁板憿錛?/p>

鎴戜滑鏉ヨ涓涓媟emove綆楁硶鐨勫師鐞嗐?/p>

remove綆楁硶宸ヤ綔鏃?strong>騫朵笉鏄洿鎺ユ妸鍏冪礌鍒犻櫎錛岃屾槸鐢ㄥ悗闈㈢殑鍏冪礌鏇夸唬鍓嶉潰鐨勫厓绱?/strong>錛屼篃鍗蟲槸璇村鏋滄垜瀵?234榪欎釜搴忓垪remove 2錛岃繑鍥炵殑搴忓垪鏄?1344錛?琚鍒跺埌2鐨勪綅緗紝4琚鍒跺埌3鐨勪綅緗級銆?/p>

榪欐牱涓婇潰鐨勪緥瀛愬氨濂借В閲婁簡錛岄偅涓や釜3鐨勫厓绱犲茍娌℃湁琚Щ闄わ紝鑰屾槸鐢ㄥ悗闈㈢殑鍏冪礌瑕嗙洊浜嗗墠闈㈢殑鍏冪礌銆傚鍑虹殑閭d袱涓暟娌℃湁琚Щ闄ゆ帀鑰屽凡銆?/p>

閭d箞鎴戜滑搴旇濡備綍鐪熸瀹屾垚縐婚櫎鍛紵remove鍑芥暟浼氳繑鍥炰竴涓凱浠e櫒錛岄偅涓凱浠e櫒鏄繖涓簭鍒楃殑閫昏緫緇堢偣錛屼篃鍗蟲槸鎴戜唬鐮侀噷鐨別ndOfNumbers錛屽畠鎸囧悜鍊掓暟絎簩涓?涓娿?/p>

浜庢槸鎴戜滑瑕佸埄鐢╨ist鐨別rase鍑芥暟瀹屾垚鍏冪礌縐婚櫎

numbers.erase(endOfNumbers, numbers.end());

榪欐牱鎴戜滑灝卞畬鎴愪簡鎴戜滑鐨勫伐浣滐紝紼嶇◢鏈夌偣鏇叉姌……

鍏跺疄鎴戜滑鍙互鎶婅繖涓ゆ鏀懼湪涓璧鳳紝姣斿濡傛灉鎴戞兂鎺ョ潃縐婚櫎鎵鏈夊間負2鐨勫厓绱?/p>

numbers.erase(remove(numbers.begin(), numbers.end(), 2), numbers.end());

榪欐牱鎴戜滑灝卞彲浠ヤ竴姝ュ埌浣嶄簡銆?/p>

浣嗘槸榪欐牱濂戒箞錛?/p>

涓嶅ソ銆?/p>

澶у浼氬彂鐜幫紝remove鍑芥暟鐨勫師鐞嗘槸澶嶅埗鑰屼笉鏄寚閽堢殑縐誨姩錛堝洜涓哄嚱鏁版搷綰電殑鏄凱浠e櫒錛岃孋++鐨勮凱浠e櫒娌℃湁瀹氫箟鍒犻櫎鎿嶄綔錛夛紝榪欐牱浼氬甫鏉ヤ竴涓棶棰橈細鎴戜滑浣跨敤list鏄洜涓哄畠鐨勪慨鏀圭殑鏁堢巼闈炲父楂橈紝鏀瑰彉涓涓嬫寚閽堝氨鍙互浜嗐傝岃繖閲屾垜浠鍒朵簡鍏冪礌錛屽鏋滃湪vector涓紝鍙兘榪樻槸楂樻晥鐨勶紝鍥犱負vector鏃犺濡備綍閮借澶嶅埗錛岃屽浜巐ist灝變笉鏄姝や簡錛?strong>浼氭瀬搴﹂檷浣庢垜浠殑鏁堢巼銆?/strong>

閭f垜浠庝箞鍔炲憿錛?/p>

絳旀鏄嬌鐢╨ist鑷繁鐨剅emove鍑芥暟

numbers.remove(1);

鎴戜滑鍙互榪欐牱鍒犻櫎鎵鏈夊間負1鐨勫厓绱犮?/p>

涔熷嵆鏄錛屽鏋滆鍒犻櫎list涓殑鍏冪礌錛?strong>鎴戜滑搴旇浣跨敤list鐨剅emove鎴愬憳鍑芥暟錛岃屼笉鏄痳emove綆楁硶錛?/p>

灝忕粨

鎴戜滑閮界煡閬擄紝STL鏄竴涓晥鐜囥佸鐢ㄦс佺伒媧繪ф姌琛風殑浜х墿錛屽叾涓晥鐜囪嚦鍏抽噸瑕侊紝鎵浠TL宸茬粡紱佹浜嗕竴浜涙晥鐜囦綆鐨勬搷浣滐紙姣斿list鐨勯殢鏈鴻闂級錛岃岄紦鍔變綘鍘諱嬌鐢ㄥ叾瀹冪殑瀹瑰櫒銆?/p>

浣嗘槸錛?strong>鍦ㄧ畻娉曚腑錛屼負浜嗙伒媧繪э紝STL榪樻槸浼氱壓鐗蹭竴浜涗笢瑗?/strong>錛屾瘮濡傛垜浠繖涓緥瀛愩?/p>

涓漢瑙夊緱錛孲TL浣滀負C++鏍囧噯搴撶殑涓涓粍鎴愰儴鍒嗭紝鐗圭偣鍜孋++鏈韓涓妯′竴鏍鳳紝寮哄ぇ鑰屽鏉傦紝鏈変簺鍦版柟闅句互鐞嗚В錛屽緢澶氱粏鑺傞渶瑕佸涔犳敞鎰忥紝鎴戜滑瑕佸浼氶伩鍏嶉櫡鍏ユ煇浜涢櫡闃變箣涓紝姣斿榪欎釜渚嬪瓙灝辨槸涓涓晥鐜囬櫡闃便?/p>

鍏跺畠鏇村鐨勯櫡闃辨槸閿欒澶勭悊鏂歸潰鐨勶紝STL鏈韓騫舵病鏈夎瀹氳繃澶氱殑閿欒澶勭悊錛屽ぇ閮ㄥ垎鐨勯敊璇鐞嗛兘浜ょ粰浜嗘垜浠紝鐞嗙敱寰堢畝鍗曪細鎬ц兘鑷充笂錛屽鏋滀竴涓笢瑗胯嚜韜病鏈夐敊璇鏌ワ紝鎴戜滑鍙互鍖呰涓涓甫閿欒媯鏌ョ殑綾伙紱浣嗘槸濡傛灉榪欎釜涓滆タ鑷韓灝卞甫浜嗛敊璇鏌ワ紝閭d箞鎴戜滑灝辨病鏈変換浣曟柟娉曟彁鍗囧畠鐨勬晥鐜囦簡銆傝繖涔熸槸寰堝C鍜孋++搴撶殑璁捐鍘熷垯銆?/p>

鎵浠ワ紝寰堝鏃跺欙紝闇瑕佹垜浠繁鍏ョ粏鑺?/strong>錛岀劧鍚庡啀鍐沖畾鍒板簳鎬庝箞鍋氥傚洜涓篊++灝辨槸濡傛錛氭湁寰堝璺彲浠ヨ蛋錛岄渶瑕佹垜浠嚜宸遍夋嫨鏈濂界殑涓鏉¤礬銆?/p>

鍒嗙被: C++


]]>
shared_from_this 鍑犱釜鍊煎緱娉ㄦ剰鐨勫湴鏂?/title><link>http://www.shnenglu.com/colorful/archive/2012/06/22/179817.html</link><dc:creator>澶氬僵浜虹敓</dc:creator><author>澶氬僵浜虹敓</author><pubDate>Fri, 22 Jun 2012 14:42:00 GMT</pubDate><guid>http://www.shnenglu.com/colorful/archive/2012/06/22/179817.html</guid><wfw:comment>http://www.shnenglu.com/colorful/comments/179817.html</wfw:comment><comments>http://www.shnenglu.com/colorful/archive/2012/06/22/179817.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/colorful/comments/commentRss/179817.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/colorful/services/trackbacks/179817.html</trackback:ping><description><![CDATA[<div><span style="color: #ff0000;" color="#ff0000">shared_from_this()鏄?enable_shared_from_this<T>鐨勬垚鍛? 鍑芥暟錛岃繑鍥瀞hared_ptr<T>銆傞鍏堥渶瑕佹敞鎰忕殑鏄紝榪欎釜鍑芥暟浠呭湪shared_ptr<T>鐨勬瀯閫犲嚱鏁拌璋冪敤涔嬪悗鎵嶈兘浣? 鐢ㄣ傚師鍥犳槸enable_shared_from_this::weak_ptr騫朵笉鍦ㄦ瀯閫犲嚱鏁頒腑璁劇疆錛岃屾槸鍦╯hared_ptr<T>鐨? 鏋勯犲嚱鏁頒腑璁劇疆銆?/span> <br /><br />濡備笅浠g爜鏄敊璇殑錛?<div> <div><ol><li>class D:public <span >boost</span>::enable_shared_from_this<D></li><li>{</li><li>public:</li><li>    D()</li><li>    {</li><li>        boost::shared_ptr<D> p=shared_from_this();</li><li>    }</li><li>};</li></ol></div> 澶嶅埗浠g爜</div> 鍘?鍥犲緢綆鍗曪紝鍦―鐨勬瀯閫犲嚱鏁頒腑铏界劧鍙互淇濊瘉enable_shared_from_this<D>鐨勬瀯閫犲嚱鏁板凡緇忚璋冪敤錛屼絾姝e鍓嶉潰鎵 璇達紝weak_ptr榪樻病鏈夎緗?<br /><br />濡備笅浠g爜涔熸槸閿欒鐨勶細 <div> <div><ol><li>class D:public boost::enable_shared_from_this<D></li><li>{</li><li>public:</li><li>    void func()</li><li>    {</li><li>        boost::shared_ptr<D> p=shared_from_this();</li><li>    }</li><li>};</li><li>void main()</li><li>{</li><li>    D d;</li><li>    d.func();</li><li>}</li></ol></div> 澶嶅埗浠g爜</div> 閿?璇師鍥犲悓涓娿?<br /><br />濡備笅浠g爜鏄紜殑錛?<div> <div><ol><li>void main()</li><li>{</li><li>    boost::shared_ptr<D> d(new D);</li><li>    d->func();</li><li>}</li></ol></div> 澶嶅埗浠g爜</div> 榪?閲宐oost::shared_ptr<D> d(new D)瀹為檯涓婃墽琛屼簡3涓姩浣滐細棣栧厛璋冪敤enable_shared_from_this<D>鐨勬瀯閫犲嚱鏁幫紱鍏舵璋冪敤D鐨勬瀯閫犲嚱鏁幫紱鏈鍚庤皟鐢? shared_ptr<D>鐨勬瀯閫犲嚱鏁般傛槸絎?涓姩浣滆緗簡enable_shared_from_this<D>鐨? weak_ptr錛岃屼笉鏄1涓姩浣溿傝繖涓湴鏂規槸寰堣繚鑳宑++甯哥悊鍜岄昏緫鐨勶紝蹇呴』灝忓績銆?<br /><br /><span style="color: #0000ff;" color="#0000ff">緇撹鏄紝涓嶈鍦ㄦ瀯閫犲嚱鏁頒腑浣跨敤shared_from_this錛涘叾嬈★紝濡傛灉瑕佷嬌鐢╯hared_ptr錛屽垯搴旇 鍦ㄦ墍鏈夊湴鏂瑰潎浣跨敤錛屼笉鑳戒嬌鐢― d榪欑鏂瑰紡錛屼篃鍐充笉瑕佷紶閫掕8鎸囬拡銆?</span>  <br /><br />鍙︿竴涓煎緱娉ㄦ剰鐨勫湴鏂規槸鍦ㄧ被鐨勭戶鎵挎爲涓笉鑳芥湁2涓垨鏇村涓猠nable_shared_from_this<T>銆備緥濡傚涓嬩唬鐮佹槸閿欒鐨勶細 <div> <div><ol><li>class A:public boost::enable_shared_from_this<A></li><li>{</li><li>public:</li><li>    A():a(1){}</li><li>    virtual ~A(){}</li><li>    boost::shared_ptr<A> get_ptra(){return shared_from_this();}</li><li>    int a;</li><li>};</li><li>class B:public A,public boost::enable_shared_from_this<B></li><li>{</li><li>public:</li><li>    B():b(2){}</li><li>    boost::shared_ptr<B> get_ptrb()</li><li>    {</li><li>        return boost::enable_shared_from_this<B>::shared_from_this();</li><li>    }</li><li>    int b;</li><li>};</li><li>int _tmain(int argc, _TCHAR* argv[])</li><li>{</li><li>    {</li><li>        boost::shared_ptr<B> x(new B);</li><li>        boost::shared_ptr<A> a1 = x->get_ptra();</li><li>        boost::shared_ptr<B> b1 = x->get_ptrb();</li><li>    }</li><li>    return 0;</li><li>}</li></ol></div> 澶嶅埗浠g爜</div> 娉?鎰忎笂闈唬鐮佷腑錛孊鍚屾椂鎷ユ湁2涓猠nable_shared_from_this鐨勫熀綾伙紝涓涓槸 enable_shared_from_this<A>錛屽彟涓涓槸enable_shared_from_this<B>銆傚湪 boost::shared_ptr<B> x(new B);榪欒浠g爜涓紝shared_ptr<B>鐨勬瀯閫犲嚱鏁頒粎浼氳緗?涓熀綾諱腑鐨勪竴涓殑weak_ptr銆傚湪涓婇潰鐨勪緥瀛愪腑錛屼粎璁劇疆 enable_shared_from_this<A>鐨勩傚鏋滀慨鏀笲鐨勫畾涔変負錛?<br /><br />class B:public boost::enable_shared_from_this<B>,public A錛?<br /><br />鍒欎粎璁劇疆enable_shared_from_this<B>鐨剋eak_ptr銆傚緢鏄庢樉閮芥槸閿欒鐨勩?<br /><br />閭d箞enable_shared_from_this浠ュ強shared_ptr涓轟綍瑕佸姝ゅ疄鐜板憿錛熷張涓轟粈涔堜細鏈夊姝ゆ紓鐨勭粨鏋滃憿錛?<br /><br />棣栧厛鑰冨療shared_ptr鐨勬瀯閫犲嚱鏁幫細 <div> <div><ol><li>template<class Y></li><li>explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete</li><li>{</li><li>    boost::detail::sp_enable_shared_from_this( pn, p, p );</li><li>}</li><li>template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )</li><li>{</li><li>    if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);</li><li>}</li></ol></div> 澶嶅埗浠g爜</div> 娉?鎰忚繖涓猻p_enable_shared_from_this鏄竴涓ā鏉垮嚱鏁幫紝鑰屼笖浠呰皟鐢ㄤ簡涓嬈★紝鎵浠ヤ笉鍙兘2涓? enable_shared_from_this鍩虹被鐨剋eak_ptr閮借璧嬪箋備絾闂鍦ㄤ簬錛屽湪璋冩崲浜咮鐨勫畾涔変箣鍚庣粨鏋滃眳鐒舵槸涓嶄竴鏍風殑銆傝繖閲屾湁涓涓緢闅? 縐樼殑緙栬瘧鍣˙UG銆傛寜閬撶悊鏉ヨ錛岀紪璇戝櫒鍦ㄧ紪璇戣繖孌典唬鐮佹椂錛屽簲璇ユ敞鎰忓埌鏃犳硶鐪熸鍐蟲柇璇ユ庝箞瀹炰緥鍖杝p_enable_shared_from_this騫朵笖 鎶ヤ竴涓敊錛屼絾vc 2008騫舵病鏈夋姤閿欙紝鑰屾槸閫氳繃緙栬瘧浜嗐傦紙g++浼氬湪姝ゅ鎶ラ敊錛?<br /><br />閭d箞姝g‘鐨勮В娉曟槸鎬庢牱鐨勫憿錛?<div> <div><ol><li>class B:public A</li><li>{</li><li>public:</li><li>    B():b(2){}</li><li>    boost::shared_ptr<B> get_ptrb()</li><li>    {</li><li>        return boost::dynamic_pointer_cast<B>(shared_from_this());</li><li>    }</li><li>    int b;</li><li>};</li></ol></div> 澶嶅埗浠g爜</div> 娉?鎰忓埌榪欓噷B騫舵病鏈夌洿鎺ョ戶鎵縠nable_shared_from_this錛岃屾槸浣跨敤dynamic_pointer_cast榪涜浜嗙被鍨嬭漿鎹€?<br /><br />鍏充簬涓轟粈涔坋nable_shared_from_this鏄繖鏍峰疄鐜扮殑錛屽彲浠ュ弬鐪嬩綔鑰呭師鏂囷細 <br /><br />Every enable_shared_from_this base contains a weak_ptr, The shared_ptr constructor looks up the enable_shared_from_this base and initializes its weak_ptr accordingly. This doesn't work when there are<br />two or more enable_shared_from_this bases, though. <br /><br />I could put the weak_ptr in a virtual polymorphic base. This would force polymorphism on all clients of enable_shared_from_this... probably acceptable. It will also force a dynamic_pointer_cast in every<br />shared_from_this, and this may be harder to swallow, particularly in cases where RTTI is off. So I'm not sure. <br /><br />If you do want the above behavior, it's easy to duplicate, as I already responded in my first post on the topic. Just make FooB return dynamic_pointer_cast<B>( FooA() ) and remove the enable_shared_from_this<B><br />base (A needs to be made polymorphic, of course). <br /><br /><span style="color: #ff0000;" color="#ff0000">娉ㄦ剰涓轟簡璁ヾynamic_pointer_cast鑳藉伐浣滐紝A蹇呴』鍏鋒湁铏氬嚱鏁幫紝閭d箞鏈綆鍗曠殑鍋氭硶褰撶劧鏄護鍏舵瀽鏋勫嚱 鏁頒負铏氬嚱鏁幫紙閫氬父涓涓猚lass濡傛灉甯屾湜琚戶鎵匡紝鏋愭瀯鍑芥暟灝卞簲璇ヤ負铏氬嚱鏁幫級</span>銆?/div><img src ="http://www.shnenglu.com/colorful/aggbug/179817.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/colorful/" target="_blank">澶氬僵浜虹敓</a> 2012-06-22 22:42 <a href="http://www.shnenglu.com/colorful/archive/2012/06/22/179817.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>enable_shared_from_this錛堣漿杞斤級http://www.shnenglu.com/colorful/archive/2012/06/22/179816.html澶氬僵浜虹敓澶氬僵浜虹敓Fri, 22 Jun 2012 14:41:00 GMThttp://www.shnenglu.com/colorful/archive/2012/06/22/179816.htmlhttp://www.shnenglu.com/colorful/comments/179816.htmlhttp://www.shnenglu.com/colorful/archive/2012/06/22/179816.html#Feedback0http://www.shnenglu.com/colorful/comments/commentRss/179816.htmlhttp://www.shnenglu.com/colorful/services/trackbacks/179816.html

鍦?C++ 涓渶瑕佽嚜宸辨潵澶勭悊鍐呭瓨錛岀◢寰鐞嗕笉褰擄紝灝變細瀛樺湪闈炲父閮侀椃鐨勫唴瀛樻硠婕忛棶棰?

 

榪樺ソ錛岀幇鍦?C++ 涓帹鍑轟簡寮哄ぇ鐨勬櫤鑳芥寚閽堬紝鍗?smart_ptr 錛屾湰鏂囧厛紼嶅井浠嬬粛涓涓?smart_ptr 錛岀劧鍚庡叿浣撹璇?shared_ptr 鍜?weak_ptr 錛岀壒鍒槸 enable_shared_from_this 鍜?shared_from_this

 

闄や簡鏍囧噯搴撲腑鐨?auto_ptr 涔嬪

鍦?boost 鎴栬?tr1 涓殑 smart_ptr 涓昏鏄湁涓嬮潰鍑犵

  • scoped_ptr
  • scoped_array
  • shared_ptr
  • shared_array
  • intrusive_ptr
  • weak_ptr

榪欎簺閲岄潰鏈闅劇悊瑙g殑鏄患鍚堝簲鐢ㄤ簡 weak_ptr 鍜?shared_ptr 鐨?enable_shared_from_this 綾伙紝鍦ㄨ綾諱腑瀹氫簡鎴愬憳鍑芥暟 shared_from_this() 錛岃繑鍥?shared_ptr<T> 銆傝繖涓嚱鏁頒粎鍦?shared_ptr<T> 鐨勬瀯閫犲嚱鏁拌璋冪敤涔嬪悗鎵嶈兘浣跨敤銆傚師鍥犳槸 enable_shared_from_this::weak_ptr 騫朵笉鍦ㄦ瀯閫犲嚱鏁頒腑璁劇疆錛堟澶勭殑鏋勯犲嚱鏁版寚鐨勬槸綾誨瀷 T 鐨勬瀯閫犲嚱鏁幫級錛岃屾槸鍦?shared_ptr<T> 鐨勬瀯閫犲嚱鏁頒腑璁劇疆錛堟澶勭殑鏋勯犲嚱鏁版寚鐨勬槸綾誨瀷 shared_ptr<T> 鐨勬瀯閫犲嚱鏁幫級銆?

 

鍦ㄤ笅闈㈢殑浠g爜涓細

 

Cpp浠g爜
  1. #include <iostream>   
  2.   
  3. #include <string>   
  4.   
  5.     
  6.   
  7. #include <boost/shared_ptr.hpp>   
  8.   
  9. #include <boost/weak_ptr.hpp>   
  10.   
  11. #include <boost/enable_shared_from_this.hpp>   
  12.   
  13.     
  14.   
  15. using namespace std;   
  16.   
  17.     
  18.   
  19. struct Ansible   
  20.   
  21.   : public boost::enable_shared_from_this<Ansible>   
  22.   
  23. {   
  24.   
  25.     boost::shared_ptr<Ansible> get_shared()   
  26.   
  27.     {   
  28.   
  29.         boost::shared_ptr<Ansible> r(this);   
  30.   
  31.     
  32.   
  33.         return r;   
  34.   
  35.     }   
  36.   
  37.     
  38.   
  39.     ~Ansible()   
  40.   
  41.     {   
  42.   
  43.         cout<<"Destructor"<<endl;   
  44.   
  45.     }   
  46.   
  47. };   
  48.   
  49.     
  50.   
  51. int main(int argc,char* argv[])   
  52.   
  53. {   
  54.   
  55.     boost::shared_ptr<Ansible> a(new Ansible);   
  56.   
  57.     Ansible& r = *a;   
  58.   
  59.     //boost::shared_ptr<Ansible> b = r.get_shared();   
  60.   
  61.     boost::shared_ptr<Ansible> b = r.shared_from_this();   
  62.   
  63.     
  64.   
  65.     cout<<"Reference Number "<<a.use_count()<<" "<<b.use_count()<<endl;   
  66.   
  67.     
  68.   
  69.     return 0;   
  70.   
  71. }  
#include <iostream> #include <string> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> #include <boost/enable_shared_from_this.hpp> using namespace std; struct Ansible : public boost::enable_shared_from_this<Ansible> { boost::shared_ptr<Ansible> get_shared() { boost::shared_ptr<Ansible> r(this); return r; } ~Ansible() { cout<<"Destructor"<<endl; } }; int main(int argc,char* argv[]) { boost::shared_ptr<Ansible> a(new Ansible); Ansible& r = *a; //boost::shared_ptr<Ansible> b = r.get_shared(); boost::shared_ptr<Ansible> b = r.shared_from_this(); cout<<"Reference Number "<<a.use_count()<<" "<<b.use_count()<<endl; return 0; }

 

鑻ヤ笉浣跨敤 shared_from_this() 鎴愬憳鍑芥暟錛屽垯浼氳緭鍑?a 鍜?b 鐨?use_count() 閮戒負 1 錛岀劧鍚庤皟鐢?2 嬈$被鍨?Ansible 鐨勬瀽鏋勫嚱鏁幫紝鑻ユ坊鍔犱簡璇ユ垚鍛樺嚱鏁幫紝鍦?a 鍜?b 鐨?use_count() 杈撳嚭涓?2 錛屽彧鏄皟鐢ㄤ竴嬈?Ansible 鐨勬瀽鏋勫嚱鏁般傚師鍥犳槸 enable_shared_from_this 閲岄潰鍦?shared_ptr<T> 鐨勬椂鍊欐瀯閫犱簡涓涓?weak_ptr 綾伙紝鑰?weak_ptr 鍙槸鐩戣錛屼笉澧炲姞寮曠敤璁℃暟

 

錛堜笅闈㈡槸杞澆錛?http://huyuguang1976.spaces.live.com/blog/cns!2A9E272E3C33AFF1!185.entry 錛?

鎵浠ュ涓嬩唬鐮佹槸閿欒鐨勶細

 

class D:public boost::enable_shared_from_this<D>

{

public:

    D()

    {

        boost::shared_ptr<D> p=shared_from_this();

    }

};

 

鍘熷洜寰堢畝鍗曪紝鍦?D 鐨勬瀯閫犲嚱鏁頒腑铏界劧鍙互淇濊瘉 enable_shared_from_this<D> 鐨勬瀯閫犲嚱鏁板凡緇忚璋冪敤錛屼絾姝e鍓嶉潰鎵璇達紝 weak_ptr 榪樻病鏈夎緗?

 

濡備笅浠g爜涔熸槸閿欒鐨勶細

 

class D:public boost::enable_shared_from_this<D>

{

public:

    void func()

    {

        boost::shared_ptr<D> p=shared_from_this();

    }

};

 

void main()

{

    D d;

    d.func();

}

 

閿欒鍘熷洜鍚屼笂銆?

 

濡備笅浠g爜鏄紜殑錛?

 

void main()

{

    boost::shared_ptr<D> d(new D);

    d->func();

}

 

榪欓噷 boost::shared_ptr<D> d(new D) 瀹為檯涓婃墽琛屼簡 3 涓姩浣滐細棣栧厛璋冪敤 enable_shared_from_this<D> 鐨勬瀯閫犲嚱鏁幫紱鍏舵璋冪敤 D 鐨勬瀯閫犲嚱鏁幫紱鏈鍚庤皟鐢?shared_ptr<D> 鐨勬瀯閫犲嚱鏁般傛槸絎?3 涓姩浣滆緗簡 enable_shared_from_this<D> 鐨?weak_ptr 錛岃屼笉鏄 1 涓姩浣溿傝繖涓湴鏂規槸寰堣繚鑳?c++ 甯哥悊鍜岄昏緫鐨勶紝蹇呴』灝忓績銆?

 

緇撹鏄紝涓嶈鍦ㄦ瀯閫犲嚱鏁頒腑浣跨敤 shared_from_this 錛涘叾嬈★紝濡傛灉瑕佷嬌鐢?shared_ptr 錛屽垯搴旇鍦ㄦ墍鏈夊湴鏂瑰潎浣跨敤錛屼笉鑳戒嬌鐢?D d 榪欑鏂瑰紡錛屼篃鍐充笉瑕佷紶閫掕8鎸囬拡銆?br />

鍙﹁В錛氾細錛氾細錛?br />struct X

{

         boost::shared_ptr<X> getX()

{

         boost::shared_ptr<X> r ;//????濡備綍瀹炵幇

         return r;

}

};

 

瑕佸緱鍒癤鐨勬櫤鑳芥寚閽?鍙槸鍦ㄥ璞℃寚閽堟槸鍙梥hared_ptr淇濇姢鐨勫熀紜涓婄殑,涓句緥濡備笅:

void test_X()

{

         {

X x;

                  boost::shared_ptr<X> px = x.getX();//閿欒

}

         {

X* x = new X();

boost::shared_ptr<X> px = x->getX();//閿欒

}

         {

boost::shared_ptr<X>  x (new X());

boost::shared_ptr<X> px = x->getX();//姝g‘

}

}



]]>
鍏充簬boost::thread 鐨勪竴浠舵簨http://www.shnenglu.com/colorful/archive/2012/05/14/174897.html澶氬僵浜虹敓澶氬僵浜虹敓Mon, 14 May 2012 13:50:00 GMThttp://www.shnenglu.com/colorful/archive/2012/05/14/174897.htmlhttp://www.shnenglu.com/colorful/comments/174897.htmlhttp://www.shnenglu.com/colorful/archive/2012/05/14/174897.html#Feedback0http://www.shnenglu.com/colorful/comments/commentRss/174897.htmlhttp://www.shnenglu.com/colorful/services/trackbacks/174897.html
#include <cstdio>
#include 
<boost/thread.hpp>
#include 
<boost/bind.hpp>

int fun1()
{
   
for(int i=0; i<10; i++)
      printf(
"鍝堝搱鍝堝搱鍝堝搱\n");
   
return 0;
}

int fun2(int count)
{
   
for(int i=0; i<count; i++)
      printf(
"鍛靛懙鍛靛懙鍛靛懙\n");
   
return 0;
}

int main()
{
   boost::thread(fun1);     
// 涓嶄互鎵撳嵃
   boost::thread(fun2, 10); // 鍙互鎵撳嵃
   boost::thread t(fun1);   // 鍙互鎵撳嵃

   getchar();
   
return 0;
}
涓嶇煡浠涔堝師鍥狅紝鐭ラ亾鐨勫彲浠ュ憡璇夋垜鍚?img src ="http://www.shnenglu.com/colorful/aggbug/174897.html" width = "1" height = "1" />

]]>
久久亚洲精品无码AV红樱桃| 亚洲成人精品久久| 久久精品国产清自在天天线| 久久久久久国产精品免费无码| av无码久久久久不卡免费网站| 91精品观看91久久久久久| 亚洲欧美一级久久精品| 狠狠色婷婷综合天天久久丁香 | 伊人久久无码中文字幕| 国产精品久久久久久福利69堂| 精品久久久久久国产免费了| 亚洲国产精品无码久久久秋霞2| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 91亚洲国产成人久久精品| 亚洲午夜精品久久久久久app| 久久人妻少妇嫩草AV无码专区| 欧美精品一区二区久久| 久久久久久免费一区二区三区| 2021最新久久久视精品爱| 国产 亚洲 欧美 另类 久久| 精品国产VA久久久久久久冰| 久久久久国产精品嫩草影院| 手机看片久久高清国产日韩| 久久精品国产半推半就| 97久久精品无码一区二区天美| 中文字幕无码免费久久| 久久综合视频网| 2021久久精品免费观看| 亚洲欧美日韩精品久久亚洲区| 99久久国产综合精品网成人影院| 成人妇女免费播放久久久| 少妇久久久久久被弄高潮| 狠狠色丁香久久婷婷综合| 久久乐国产综合亚洲精品| 少妇被又大又粗又爽毛片久久黑人 | 午夜精品久久久久久99热| 久久频这里精品99香蕉久| 久久精品青青草原伊人| 热99RE久久精品这里都是精品免费| 久久亚洲精品国产精品婷婷| 久久久久久久免费视频|