• <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

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(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]這樣的方式正確運(yùn)行. 但是,如果要在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ù)制指針,還把指針?biāo)赶虻膬?nèi)容給拷貝一份,這樣就不會導(dǎo)致被最后一個成員釋放的時候一起釋放掉了. 2, 如果有引用記數(shù)的話,如智能指針, 就不會被釋放掉了。不過如果一般編碼里面不需要用到引用記數(shù)的話,還是方法1比較簡便

             

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

            評論

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

            不明白為什么成員變量是指針就把指針?biāo)傅牡胤轿鰳?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é)任維護(hù)你給他的東西,但沒理由維護(hù)這個東西里面的東西。不僅僅是vector,STL所有的容器如果按你這種思維去用,都會出問題:
            class Test
            {
            int *a;
            ~Test()
            {
            delete a;
            }
            };
            std::vector<Test> 維護(hù)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ù)  更多評論   

            但沒理由維護(hù)這個東西里面的東西。不僅僅是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é)任。的容器如果按你員變量是指針就把指針?biāo)傅牡胤轿鰳?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ù)  更多評論   

            但沒理由維護(hù)這個東西里面的東西。所有的容器如果按你這種思維去用,則應(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ù)  更多評論   

            但沒理由維護(hù)這個東西里面的東西。所有的容器如果按你這種思維去用,則應(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ù)  更多評論   

            但沒理由維護(hù)這個東西里面的東西。所有的容器如果按你這種思維去用,則應(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é)任維護(hù)你給他的東西,但沒理由維護(hù)這個東西里面的東西。不僅僅是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
            欧美久久一级内射wwwwww.| 污污内射久久一区二区欧美日韩 | 综合网日日天干夜夜久久| 岛国搬运www久久| 久久99精品国产| 狠狠色丁香婷婷久久综合不卡| 久久人人爽人人爽人人av东京热 | 丁香色欲久久久久久综合网| 久久人人爽人人爽人人片AV东京热 | 精品久久久久久成人AV| 色综合久久久久综合体桃花网| 久久精品免费全国观看国产| 一本久道久久综合狠狠躁AV| 亚洲一区精品伊人久久伊人| 久久久午夜精品| 亚洲香蕉网久久综合影视| 日韩精品久久无码人妻中文字幕| 一本色道久久HEZYO无码| 久久久国产乱子伦精品作者| 久久精品人成免费| 久久国产高清字幕中文| 久久久91精品国产一区二区三区 | 久久无码国产| 久久青青草视频| 久久久久亚洲AV无码永不| 狠狠久久亚洲欧美专区| 国产精品午夜久久| 久久这里只精品99re66| 久久AV无码精品人妻糸列| 久久精品aⅴ无码中文字字幕重口| 九九久久99综合一区二区| 久久99国产精品成人欧美| 欧美亚洲国产精品久久久久| 熟妇人妻久久中文字幕| 国产精品九九久久免费视频 | 久久婷婷国产麻豆91天堂| 久久久久这里只有精品| 国产精品乱码久久久久久软件| 久久久久人妻一区二区三区vr| 久久99免费视频| 亚洲精品99久久久久中文字幕 |