• <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 閱讀(2386) 評論(0)  編輯 收藏 引用 所屬分類: C++技術

            公告

            導航

            隨筆分類

            最新隨筆

            最新評論

            閱讀排行榜

            日韩亚洲国产综合久久久| 国内精品久久久久久99蜜桃| 国产免费久久精品丫丫| 国产激情久久久久影院小草| 久久久这里有精品| 97热久久免费频精品99| 欧美午夜A∨大片久久| 久久水蜜桃亚洲av无码精品麻豆| 国产巨作麻豆欧美亚洲综合久久| 久久综合亚洲色HEZYO社区 | 九九久久自然熟的香蕉图片| 久久最新精品国产| 亚洲av成人无码久久精品| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区| 国产精品九九久久免费视频| 日日噜噜夜夜狠狠久久丁香五月| 精品久久久无码中文字幕天天| 天天躁日日躁狠狠久久| 亚洲精品视频久久久| 91久久福利国产成人精品| 久久这里只有精品18| 亚洲精品蜜桃久久久久久| 欧美精品九九99久久在观看| 国产午夜福利精品久久| 热久久这里只有精品| 国产亚洲精久久久久久无码 | 亚洲狠狠婷婷综合久久蜜芽| 国产日韩久久久精品影院首页| 狠狠色丁香久久婷婷综合五月| 久久久久波多野结衣高潮| 久久成人小视频| 亚洲国产精品无码久久九九| 久久青青草原精品国产软件| 久久国产免费| 亚洲国产成人精品91久久久| 欧美日韩成人精品久久久免费看| 久久无码一区二区三区少妇| 久久精品国产精品亚洲艾草网美妙| 国产精品99久久久久久猫咪| 久久精品99无色码中文字幕| 久久亚洲高清综合|