• <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 肥仔 閱讀(7487) 評論(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);

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

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

            其實我覺得這不是vector給你設(shè)的“陷阱”,STL容器只有責(zé)任維護你給他的東西,但沒理由維護這個東西里面的東西。不僅僅是vector,STL所有的容器如果按你這種思維去用,都會出問題:
            class Test
            {
            int *a;
            ~Test()
            {
            delete a;
            }
            };
            std::vector<Test> 維護Test::a其實應(yīng)該是你的責(zé)任。
            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)該是你的責(zé)任。的容器如果按你員變量是指針就把指針所指的地方析構(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ù)  更多評論   

            容器只有責(zé)任維護你給他的東西,但沒理由維護這個東西里面的東西。不僅僅是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
            粉嫩小泬无遮挡久久久久久| 日本久久中文字幕| 久久精品免费一区二区| 国产aⅴ激情无码久久| 狠狠色丁香久久综合婷婷| 久久本道综合久久伊人| 日日躁夜夜躁狠狠久久AV| 国产69精品久久久久99尤物| 久久亚洲中文字幕精品一区| 久久99国产精品久久| 久久精品国产欧美日韩99热| 狠狠88综合久久久久综合网 | 久久黄色视频| 一本一本久久A久久综合精品| 亚洲嫩草影院久久精品| 亚洲国产精品无码久久一线 | 99久久国产热无码精品免费| 国内精品久久久久久麻豆 | 久久久国产乱子伦精品作者| 亚洲а∨天堂久久精品9966| 狠狠狠色丁香婷婷综合久久五月| 久久精品国产男包| 久久夜色撩人精品国产| 国产精品成人久久久久久久| 久久r热这里有精品视频| 午夜欧美精品久久久久久久| 久久亚洲精品国产精品婷婷 | 亚洲中文字幕无码久久2020 | 亚洲午夜久久久久久久久电影网 | 久久强奷乱码老熟女网站| 成人综合伊人五月婷久久| 亚洲中文字幕无码久久综合网 | 好久久免费视频高清| 精品久久人妻av中文字幕| 久久一日本道色综合久久| 狠狠色婷婷久久综合频道日韩 | 2021国内久久精品| 欧美与黑人午夜性猛交久久久| 国产精品久久久天天影视香蕉| 国产精品99久久久久久董美香| 国产精品熟女福利久久AV|