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

            大規模高性能網絡服務器編程 大型游戲服務器編程


            大規模高性能網絡服務器編程 大型游戲服務器編程 完成端口模型 TCP UDP P2P 網絡編程

                       C++博客 | 首頁 | 發新隨筆 | 發新文章 | 聯系 | 聚合 | 管理

                          

            STL容器使用之一:vector


            本文原創發表地址為:http://www.shnenglu.com/kusamba/archive/2010/09/16/126786.html,轉載請保留原鏈接,謝謝!

            std::vector作為順序容器,具有如下特點:
            /************************************************************************
            * vector
            *    1, 順序存儲,內部數據結構為數組
            *    2, 使用之前調用reserve()預分配元素數目,可提高性能
            *    3, insert/erase操作效率低,跟被操作元素在vector中的位置成正比
            *       在末尾增加或刪除元素所需時間與元素數目無關,
            *       在中間或開頭增加或刪除元素所需時間隨元素數目呈線性變化
            *    4, insert/erase操作將有可能導致vector內存重新分配,iterator失效
            *    5, 時間復雜度:
            *            隨機索引訪問[index]: O(1)
            *            查找:O(n)
            *    by Kusamba@126.com    http://www.shnenglu.com/kusamba
            */

            技術要點:

            1,加載頭文件
            #include <vector>
            using namespace std;

            2,預分配內存, 調用reserve函數

            3, 插入對象,使用push_back或[index]

            4, insert/erase 如何避免iterator失效


            代碼如下:

              1 /************************************************************************
              2 * vector
              3 *    1, 順序存儲,內部數據結構為數組
              4 *    2, 使用之前調用reserve()預分配元素數目,可提高性能
              5 *    3, insert/erase操作效率低,跟被操作元素在vector中的位置成正比
              6 *       在末尾增加或刪除元素所需時間與元素數目無關,
              7 *       在中間或開頭增加或刪除元素所需時間隨元素數目呈線性變化
              8 *    4, insert/erase操作將有可能導致vector內存重新分配,iterator失效
              9 *    5, 時間復雜度:
             10 *            隨機索引訪問[index]: O(1)
             11 *            查找:O(n)
             12 */
             13 
             14 /**
             15 * 謂詞表達式
             16 */
             17 struct sComplexObj 
             18 {
             19     int  nID;
             20     char szNickName[32];
             21 };
             22 bool Equal_ID(sComplexObj obj)
             23 {
             24     return obj.nID == 4 ? true : false;
             25 }
             26 
             27 /**
             28 * 測試代碼
             29 */
             30 void vector_test()
             31 {
             32     vector<int> vInt;
             33 
             34     /**
             35     * reserve : 預分配空間
             36     * capacity: 獲取預分配空間數目
             37     * size    : 當前元素個數
             38     */
             39     int nCapacity = 0, nSize = 0;
             40     nCapacity = vInt.capacity();
             41 
             42     vInt.reserve(10);
             43     nSize = vInt.size();
             44 
             45     /**
             46     * insert
             47     */
             48     for (int i = 0; i < 10++i)
             49     {
             50         vInt.push_back(i + 1);
             51     }
             52     for (int i = 0; i < 10++i)
             53     {
             54         vInt[i] = vInt[i] * 2;
             55     }
             56 
             57     // insert: 在4倍數元素的后面插入一個元素,值為該元素的2倍
             58 
             59     // 錯誤的做法
             60     ////for (vector<int>::iterator it = vInt.begin(); it != vInt.end(); ++it)
             61     ////{
             62     ////    if (*it % 4 == 0)
             63     ////    {
             64     ////        vInt.insert(it, *it * 2);-->it失效
             65     ////        //重新分配了空間,it和begin(), end()均失效
             66     ////    }
             67     ////}
             68 
             69     // 利用計數插入
             70     for (int i = 0; i < vInt.size(); ++i)
             71     {
             72         if (vInt[i] % 4 == 0)
             73         {
             74             vInt.insert(vInt.begin() + i + 1, vInt[i] * 2);
             75             i += 1;
             76         }
             77     }
             78     // 利用iterator以及insert返回的被插入元素的位置
             79     for (vector<int>::iterator it = vInt.begin(); it != vInt.end(); ++it)
             80     {
             81         if (*it % 3 == 0)
             82         {
             83             it = vInt.insert(it + 1*it * 2);//獲取當前插入元素的位置
             84         }
             85     }
             86 
             87     /**
             88     * erase
             89     */
             90     for (vector<int>::iterator it = vInt.begin(); it != vInt.end(); )
             91     {
             92         if (*it % 2 == 0)
             93         {
             94             it = vInt.erase(it);//返回end()或后一個元素的新位置
             95         }
             96         else
             97         {
             98             ++it;
             99         }
            100     }
            101 
            102     //使用謂詞 只能刪除一次
            103     vInt.erase(remove(vInt.begin(), vInt.end(), 4));
            104 
            105     //對于復雜對象,還可以使用remove_if算法
            106     {
            107         vector<sComplexObj> vComplexObj;
            108         for (int i = 0; i < 10++i)
            109         {
            110             sComplexObj obj;
            111             obj.nID = i;
            112             sprintf_s(obj.szNickName, "ID_%06d", i);
            113             vComplexObj.push_back(obj);
            114         }
            115         vComplexObj.erase(remove_if(vComplexObj.begin(), vComplexObj.end(), Equal_ID));
            116     }
            117 
            118     /**
            119     * traverse
            120     */
            121     printf("print vInt method1: ");
            122     for (int i = 0; i < vInt.size(); ++i)
            123     {
            124         printf("%d ", vInt[i]);
            125     }
            126     printf("\n");
            127 
            128     printf("print vInt method2: ");
            129     for (vector<int>::iterator it = vInt.begin(); it != vInt.end(); ++it)
            130     {
            131         printf("%d "*it);
            132     }
            133     printf("\n");
            134 }

            posted on 2010-09-16 17:22 iKusamba 閱讀(2391) 評論(0)  編輯 收藏 引用 所屬分類: C++技術

            公告

            導航

            隨筆分類

            最新隨筆

            最新評論

            閱讀排行榜

            av国内精品久久久久影院| 成人综合久久精品色婷婷| 97久久精品午夜一区二区| 国产精品9999久久久久| A级毛片无码久久精品免费| 亚洲?V乱码久久精品蜜桃| 久久人人妻人人爽人人爽| 久久精品免费观看| 欧美午夜A∨大片久久| 国内精品九九久久久精品| 久久久国产精品| 国产精品一久久香蕉产线看 | 久久综合九色综合欧美就去吻| 狠狠色丁香久久婷婷综合_中 | 亚洲国产精品久久久久| 久久综合九色综合网站| 99久久精品国产一区二区蜜芽| 亚洲欧美国产日韩综合久久| 一级做a爰片久久毛片人呢| 久久久久久精品无码人妻| 国产亚洲成人久久| 久久成人精品视频| 久久精品无码一区二区WWW| 久久99精品国产麻豆婷婷| 国产亚洲色婷婷久久99精品| 一本色道久久HEZYO无码| 久久久久亚洲精品男人的天堂| 91精品国产综合久久四虎久久无码一级| 97精品依人久久久大香线蕉97 | 伊人久久一区二区三区无码| 精品久久久久久亚洲| 久久精品无码一区二区无码| 色88久久久久高潮综合影院| 久久99热这里只有精品国产| 伊人久久成人成综合网222| 婷婷久久综合| 少妇人妻综合久久中文字幕| 久久亚洲中文字幕精品一区| 久久久久精品国产亚洲AV无码| 色妞色综合久久夜夜| 久久久久久精品无码人妻|