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

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

            使用std::vector 的陷阱

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

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

            std::vector< int >  values;

            values.push_back(1);

            values.push_back(2);

            values.push_back(3);

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

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

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

                                  clip_image002

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

             

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

            評論

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

            不明白為什么成員變量是指針就把指針所指的地方析構,是你的類實現的問題嗎?

            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 的陷阱[未登錄]  回復  更多評論   

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

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

            所有容器里面都應該只存儲簡單數據結構,一旦數據結構為復雜數據結構時,則應該存儲該數據的指針。
            2009-09-15 16:55 | davidfan

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

            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 的陷阱  回復  更多評論   

            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 的陷阱  回復  更多評論   

            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 的陷阱  回復  更多評論   

            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 的陷阱  回復  更多評論   

            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 的陷阱  回復  更多評論   

            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 的陷阱  回復  更多評論   

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

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

            果按你這種思維去用,都會出問題 為復雜數據結構時,則應該

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

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

            實應該是你的責任。的容器如果按你員變量是指針就把指針所指的地方析構

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

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

            一旦數據結構為復雜數據結構時,則應該存儲該數據的指針。

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

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

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

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

            則應該存儲該數據的指針。

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

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

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

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

            一旦數據結構為復雜數據結構時 http://speedbet88.com
            2014-10-16 16:19 | agen bola

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

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

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

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

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

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

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

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

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

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

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

            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 的陷阱  回復  更多評論   

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

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

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

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

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

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

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

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

            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 的陷阱  回復  更多評論   

            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
            国产午夜电影久久| 伊人久久久AV老熟妇色| 91视频国产91久久久| 国产精品久久久久久久久鸭 | 久久久青草久久久青草| 91精品国产综合久久精品| 色综合久久中文色婷婷| 国产精品嫩草影院久久| 久久精品青青草原伊人| 国产午夜久久影院| 亚洲&#228;v永久无码精品天堂久久 | 国产精品欧美久久久久无广告 | 少妇被又大又粗又爽毛片久久黑人 | 欧美午夜A∨大片久久| 亚洲国产精品无码久久| 亚洲欧美精品伊人久久| 亚洲欧洲精品成人久久曰影片| 久久亚洲AV成人出白浆无码国产| 久久精品中文字幕有码| 精品国产乱码久久久久久1区2区| 国产激情久久久久影院| 一本色道久久99一综合| 香蕉久久一区二区不卡无毒影院 | 人妻久久久一区二区三区| 国产女人aaa级久久久级| 亚洲AV日韩精品久久久久久| 国产激情久久久久影院小草| 久久精品人人槡人妻人人玩AV| 久久亚洲欧洲国产综合| 色综合久久最新中文字幕| 亚洲精品无码成人片久久| 日韩va亚洲va欧美va久久| 久久精品成人免费观看97| 国产一区二区三区久久精品| 一本色道久久HEZYO无码| 久久久久亚洲精品日久生情| 久久性精品| 伊人久久大香线蕉无码麻豆| 性做久久久久久久久| 久久99热这里只频精品6| 久久午夜综合久久|