• <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
            數(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, 而是一個類,一個結構體,類或結構體里面還有指針, 那就很可能出問題了. why?

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

                                  clip_image002

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

             

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

            評論

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

            不明白為什么成員變量是指針就把指針所指的地方析構,是你的類實現(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 的陷阱[未登錄]  回復  更多評論   

            其實我覺得這不是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 的陷阱  回復  更多評論   

            所有容器里面都應該只存儲簡單數(shù)據(jù)結構,一旦數(shù)據(jù)結構為復雜數(shù)據(jù)結構時,則應該存儲該數(shù)據(jù)的指針。
            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,所有的容器如果按你這種思維去用,都會出問題 為復雜數(shù)據(jù)結構時,則應該存儲該數(shù)據(jù)
            2014-09-10 21:40 | Agen Tangkas

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            但沒理由維護這個東西里面的東西。所有的容器如果按你這種思維去用,則應該存儲該數(shù)據(jù)
            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 的陷阱  回復  更多評論   

            如果有引用記數(shù)的話,如智能指針, 就不會被釋放掉了。不過如果一般編碼里面不需要用到引用記數(shù)的話,還是方法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
            久久国产精品无码网站| 久久综合噜噜激激的五月天| 久久久精品国产亚洲成人满18免费网站| 伊人久久精品线影院| 一级做a爰片久久毛片看看| 久久精品国产亚洲av影院| 日本免费久久久久久久网站| 综合久久精品色| 久久青青草原综合伊人| 久久无码中文字幕东京热| 久久精品嫩草影院| 无码AV中文字幕久久专区| 激情久久久久久久久久| 亚洲精品无码久久久影院相关影片| 久久综合综合久久狠狠狠97色88| 亚洲精品97久久中文字幕无码| 国产精品久久久天天影视| 精产国品久久一二三产区区别| 精品九九久久国内精品| 无码精品久久久久久人妻中字| 久久er国产精品免费观看8| 国内精品久久国产大陆| 奇米影视7777久久精品| 99久久精品国产一区二区 | 久久免费视频1| 久久91精品综合国产首页| 九九久久99综合一区二区| 国产亚洲欧美成人久久片| 久久综合狠狠综合久久| 日本人妻丰满熟妇久久久久久| 超级碰碰碰碰97久久久久| 亚洲精品国产第一综合99久久| 色8激情欧美成人久久综合电| 一级做a爰片久久毛片免费陪| 激情五月综合综合久久69| 久久综合九色欧美综合狠狠| 久久精品中文字幕有码| 午夜视频久久久久一区 | 久久久久国产视频电影| 久久精品国产亚洲Aⅴ香蕉| 久久精品国产精品亚洲艾草网美妙 |