• <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://m.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://m.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++技術

            公告

            導航

            隨筆分類

            最新隨筆

            最新評論

            閱讀排行榜

            精品久久一区二区| 久久综合伊人77777麻豆| 久久亚洲精品人成综合网| 国产成人综合久久综合| 久久久久国产| 97精品久久天干天天天按摩| 精品久久人人妻人人做精品| 久久国语露脸国产精品电影| MM131亚洲国产美女久久| 精品久久久久久久久久久久久久久| 久久99国产精品久久99小说| 国产精品久久久久久一区二区三区 | 久久精品国产一区二区三区| 亚洲七七久久精品中文国产| 精品久久一区二区三区| 久久99九九国产免费看小说| 国产综合精品久久亚洲| 午夜精品久久久久久毛片| 欧洲性大片xxxxx久久久| 99麻豆久久久国产精品免费| 亚洲午夜久久久久妓女影院| 伊人久久无码精品中文字幕| 久久狠狠一本精品综合网| 久久久中文字幕| 国内精品伊人久久久久| 狠狠88综合久久久久综合网| 久久久久无码精品国产不卡| 欧美日韩久久中文字幕| 中文字幕精品久久| 性做久久久久久久久| 久久久久国产一区二区| 国产综合免费精品久久久| 夜夜亚洲天天久久| 久久久久久久综合日本亚洲| 久久久青草久久久青草| 久久亚洲精品中文字幕三区| 亚洲国产成人久久综合一| 91久久成人免费| 久久免费大片| 久久99精品国产麻豆宅宅| 久久亚洲精品无码AV红樱桃|