• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            使用std::vector 的陷阱

            在使用std的容器的時候,不少人喜歡用vector, 因為比起list,更省空間,而且可以根據(jù)index直接讀取某個值,而不用一個個枚舉來取.

            但是,std::vector確實有一些值得注意的陷阱, 這里先說其中一個, 請看以下代碼.

            std::vector< int >  values;

            values.push_back(1);

            values.push_back(2);

            values.push_back(3);

            values.erase(values.begin() + 1);

            乍看之下,這幾行簡單的代碼沒什么 問題. 實際執(zhí)行起來, 還是沒什么問題 , 但卻有一個陷阱. 由于例子里面用的是intvector,所以這樣做沒有任何問題, ,假如不是一個int, 而是一個類,一個結(jié)構(gòu)體,類或結(jié)構(gòu)體里面還有指針, 那就很可能出問題了. why?

            因為vector不象list,vector始終要保持一個完整的內(nèi)存結(jié)構(gòu)(因為就是一個數(shù)組),這樣才可以讓values[1]這樣的方式正確運行. 但是,如果要在vector中間刪掉一個成員的話,vector是這樣做的, 先把該成員后面的一個成員,一直到最后一個成員往前一位置拷貝,這樣需要刪除的成員已經(jīng)被后面的覆蓋了, 然后再刪除最后一個成員,這樣,vector又能保持一段完整的內(nèi)存結(jié)構(gòu)了注意,因為最后一個成員會被刪除,而如果這個成員里面有一個成員變量是指針, 那析構(gòu)函數(shù)很有可能會把這個指針指向的地方釋放掉這樣,即使最后一個成員被復(fù)制了一份 到倒數(shù)第2的位置,也因為在他本身被刪除的時候,把倒數(shù)第2(也就是它的復(fù)制) 的指針成員所指向的地方給釋放了! 如圖:

                                  clip_image002

            解決的辦法也很簡單, 最少有2. 1,  增加作為vector類型的類的拷貝構(gòu)造函數(shù), 因為vectorerase的時候會發(fā)生一次拷貝,讓拷貝構(gòu)造函數(shù)不單單是復(fù)制指針,還把指針所指向的內(nèi)容給拷貝一份,這樣就不會導(dǎo)致被最后一個成員釋放的時候一起釋放掉了. 2, 如果有引用記數(shù)的話,如智能指針, 就不會被釋放掉了。不過如果一般編碼里面不需要用到引用記數(shù)的話,還是方法1比較簡便

             

            posted on 2009-09-02 22:36 肥仔 閱讀(7529) 評論(30)  編輯 收藏 引用 所屬分類: Boost & STL

            評論

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            不明白為什么成員變量是指針就把指針所指的地方析構(gòu),是你的類實現(xiàn)的問題嗎?

            int *p1 = new int(1);
            int *p2 = p1;

            vector< int* > ivec;
            ivec.push_back(p1);
            ivec.push_back(p2);

            當我把vector中的p2 erase掉之后,p1所指無物?
            2009-09-03 09:20 | nelson

            # re: 使用std::vector 的陷阱[未登錄]  回復(fù)  更多評論   

            其實我覺得這不是vector給你設(shè)的“陷阱”,STL容器只有責任維護你給他的東西,但沒理由維護這個東西里面的東西。不僅僅是vector,STL所有的容器如果按你這種思維去用,都會出問題:
            class Test
            {
            int *a;
            ~Test()
            {
            delete a;
            }
            };
            std::vector<Test> 維護Test::a其實應(yīng)該是你的責任。
            2009-09-03 13:37 | Kevin Lynx

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            所有容器里面都應(yīng)該只存儲簡單數(shù)據(jù)結(jié)構(gòu),一旦數(shù)據(jù)結(jié)構(gòu)為復(fù)雜數(shù)據(jù)結(jié)構(gòu)時,則應(yīng)該存儲該數(shù)據(jù)的指針。
            2009-09-15 16:55 | davidfan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            Last time I downloaded the mp ringtones with the help of the <a href="http://www.milliontones.com">ringtones</a> site and used to be completely satisfied.
            2010-06-24 17:54 | ConnerThelma30

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            I think that you really know how not easy could the custom term paper creating be. But, you shouldn’t be confused, simply because the term paper writing services present the do my essay papers and there’s no problem to buy custom writing services and be satisfied.
            2010-09-28 17:58 | buy essays cheap

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            Frequently, that is better to buy essays, particularly if you do not have an opportunity to compose even an easy academic task.
            2010-12-10 20:13 | custom essays

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            Excellent post. I was checking continuously this blog and I'm impressed! Very useful information specifically the last part :) I care for such info a lot. I was looking for this certain information for a very long time. Thank you and good luck.
            2013-10-03 07:48 | have a peek here

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            I do not even understand how I stopped up here, but I assumed this publish used to be good. I don't realize who you might be but certainly you're going to a well-known blogger if you aren't already ;) Cheers!
            2013-10-22 17:32 | agen bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            I like the valuable info you provide in your articles. I'll bookmark your weblog and check again here frequently. I'm quite certain I'll learn a lot of new stuff right here! Best of luck for the next!
            2013-11-08 10:14 | Navigate Here

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            但沒理由維護這個東西里面的東西。不僅僅是vector,所有的容器如果按你這種思維去用,都會出問題 為復(fù)雜數(shù)據(jù)結(jié)構(gòu)時,則應(yīng)該存儲該數(shù)據(jù)
            2014-09-10 21:40 | Agen Tangkas

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            果按你這種思維去用,都會出問題 為復(fù)雜數(shù)據(jù)結(jié)構(gòu)時,則應(yīng)該

            http://uefa88.net
            2014-09-10 21:42 | Agen Tangkas Terpercaya

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            實應(yīng)該是你的責任。的容器如果按你員變量是指針就把指針所指的地方析構(gòu)

            http://www.sidikjaritermurah.com
            2014-09-10 21:44 | Mesin Absensi Sidik Jari

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            一旦數(shù)據(jù)結(jié)構(gòu)為復(fù)雜數(shù)據(jù)結(jié)構(gòu)時,則應(yīng)該存儲該數(shù)據(jù)的指針。

            http://multibet88.org
            2014-10-16 03:22 | judi online

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            http://agent108.org
            2014-10-16 16:16 | judi bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            則應(yīng)該存儲該數(shù)據(jù)的指針。

            http://homebet88.com
            2014-10-16 16:18 | agen bola terpercaya

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            都會出問題 為復(fù)雜數(shù) http://citibet88.com
            2014-10-16 16:18 | taruhan bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            一旦數(shù)據(jù)結(jié)構(gòu)為復(fù)雜數(shù)據(jù)結(jié)構(gòu)時 http://speedbet88.com
            2014-10-16 16:19 | agen bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            但沒理由維護這個東西里面的東西。所有的容器如果按你這種思維去用,則應(yīng)該存儲該數(shù)據(jù) http://www.speedbet88.biz
            2014-10-25 03:24 | agen bola

            # # re: 使用std::vector 的陷阱 回復(fù) 更多評論   回復(fù)  更多評論   

            我們的<a代理href="http://agent108.org">朱迪·博拉網(wǎng)</A>最好的,最大的,最可靠的agent108 - 給促銷獎金10萬每存款將獲得5%的獎金
            存取速度非常快,馬上加入我們的行列。
            2014-10-28 17:30 | milestone

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            但沒理由維護這個東西里面的東西。所有的容器如果按你這種思維去用,則應(yīng)該存儲該數(shù)據(jù) http://www.citibet88.com
            2014-10-29 18:03 | taruhan bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            很不錯的博客,謝謝分享這個非常有趣的信息。保持成功
            2014-11-18 22:03 | sbobet online

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            但沒理由維護這個東西里面的東西。所有的容器如果按你這種思維去用,則應(yīng)該存儲該數(shù)據(jù)
            2014-11-20 20:51 | agen bola terpercaya

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            www.on303.com adalah agen judi bola terpercaya di indonesia , dengan customer service yang ramah siap membantu anda semua.
            2015-07-14 13:42 | judi bola

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            この記事では、読者のために非常に良いと便利です。知識の共有をありがとうございました
            2016-02-18 11:12 | obat penggugur kandungan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            私のような初心者のための読書の多くを必要とし、様々なブログ上の情報を検索します。あなたはとても素敵を共有し、私を鼓舞し、記事
            2016-03-18 10:30 | cara menggugurkan kandungan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            容器只有責任維護你給他的東西,但沒理由維護這個東西里面的東西。不僅僅是vector,STL所有的容器如果按你這種思維去用
            2016-04-22 13:26 | Cara Menggugurkan Kandungan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            如果有引用記數(shù)的話,如智能指針, 就不會被釋放掉了。不過如果一般編碼里面不需要用到引用記數(shù)的話,還是方法1比較簡便
            2016-04-22 13:27 | Obat Penggugur Kandungan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            Mate this is a very nice blog here. I wanted to comment & say that I enjoyed reading your posts & they are all very well written out. You make blogging look easy lol I’ll attemp to start a blog later today and I hope it’s half as good as your blog! Much success to you!
            2016-06-13 20:18 | Dr.Aborsi Kandungan

            # re: 使用std::vector 的陷阱  回復(fù)  更多評論   

            I would like to start a pressure group of Australian authors whose purpose is to remind the buyers at Council Libraries that one of their functions is to support and nurture Australian Literature.
            2016-06-16 11:14 | klinik apotik24
            色婷婷久久久SWAG精品| 久久久久人妻精品一区二区三区| 国产精品免费久久| 日韩va亚洲va欧美va久久| 久久人与动人物a级毛片| 国产69精品久久久久777| 久久天天婷婷五月俺也去| 国产一级做a爰片久久毛片| 久久亚洲2019中文字幕| 国内精品久久久人妻中文字幕| 久久夜色撩人精品国产| AAA级久久久精品无码片| 一极黄色视频久久网站| 国产福利电影一区二区三区久久久久成人精品综合 | 欧美日韩精品久久久免费观看| 精品久久人妻av中文字幕| 亚洲精品WWW久久久久久| 中文字幕久久欲求不满| 久久ww精品w免费人成| 99久久香蕉国产线看观香| 国产精品午夜久久| 精品精品国产自在久久高清| 亚洲AV成人无码久久精品老人| 人妻无码久久精品| 久久九色综合九色99伊人| 好属妞这里只有精品久久| 久久久久亚洲AV成人片| 伊人久久无码中文字幕| 97精品伊人久久久大香线蕉| 一级女性全黄久久生活片免费| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久这里只有精品久久| 色综合久久中文色婷婷| 办公室久久精品| 久久精品国产精品亚洲人人| 九九久久精品无码专区| 国产精品青草久久久久福利99| 88久久精品无码一区二区毛片| 一级做a爰片久久毛片16| 国产精品免费久久久久电影网| 久久综合视频网站|