青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

簡單的Int容器類

from http://www.learncpp.com/cpp-tutorial/104-container-classes/

   1: #ifndef INTARRAY_H
   2: #define INTARRAY_H
   3:  
   4: #include <assert.h> // for assert()
   5:  
   6: class IntArray
   7: {
   8: private:
   9:     int m_nLength;
  10:     int *m_pnData;
  11:  
  12: public:
  13:     IntArray()
  14:     {
  15:         m_nLength = 0;
  16:         m_pnData = 0;
  17:     }
  18:  
  19:     IntArray(int nLength)
  20:     {
  21:         m_pnData = new int[nLength];
  22:         m_nLength = nLength;
  23:     }
  24:  
  25:     ~IntArray()
  26:     {
  27:         delete[] m_pnData;
  28:     }
  29:  
  30:     void Erase()
  31:     {
  32:         delete[] m_pnData;
  33:         // We need to make sure we set m_pnData to 0 here, otherwise it will
  34:         // be left pointing at deallocated memory!
  35:         m_pnData = 0;
  36:         m_nLength = 0;
  37:     }
  38:  
  39:     int& operator[](int nIndex)
  40:     {
  41:         assert(nIndex >= 0 && nIndex < m_nLength);
  42:         return m_pnData[nIndex];
  43:     }
  44:  
  45:     // Reallocate resizes the array.  Any existing elements will be destroyed.
  46:     // This function operates quickly.
  47:     void Reallocate(int nNewLength)
  48:     {
  49:         // First we delete any existing elements
  50:         Erase();
  51:  
  52:         // If our array is going to be empty now, return here
  53:         if (nNewLength<= 0)
  54:             return;
  55:  
  56:         // Then we have to allocate new elements
  57:         m_pnData = new int[nNewLength];
  58:         m_nLength = nNewLength;
  59:     }
  60:  
  61:     // Resize resizes the array.  Any existing elements will be kept.
  62:     // This function operates slowly.
  63:     void Resize(int nNewLength)
  64:     {
  65:         // If we are resizing to an empty array, do that and return
  66:         if (nNewLength <= 0)
  67:         {
  68:             Erase();
  69:             return;
  70:         }
  71:  
  72:         // Now we can assume nNewLength is at least 1 element.  This algorithm
  73:         // works as follows: First we are going to allocate a new array.  Then we
  74:         // are going to copy elements from the existing array to the new array.
  75:         // Once that is done, we can destroy the old array, and make m_pnData
  76:         // point to the new array.
  77:  
  78:         // First we have to allocate a new array
  79:         int *pnData = new int[nNewLength];
  80:  
  81:         // Then we have to figure out how many elements to copy from the existing
  82:         // array to the new array.  We want to copy as many elements as there are
  83:         // in the smaller of the two arrays.
  84:         if (m_nLength > 0)
  85:         {
  86:             int nElementsToCopy = (nNewLength > m_nLength) ? m_nLength : nNewLength;
  87:  
  88:             // Now copy the elements one by one
  89:             for (int nIndex=0; nIndex < nElementsToCopy; nIndex++)
  90:                 pnData[nIndex] = m_pnData[nIndex];
  91:         }
  92:  
  93:         // Now we can delete the old array because we don't need it any more
  94:         delete[] m_pnData;
  95:  
  96:         // And use the new array instead!  Note that this simply makes m_pnData point
  97:         // to the same address as the new array we dynamically allocated.  Because
  98:         // pnData was dynamically allocated, it won't be destroyed when it goes out of scope.
  99:         m_pnData = pnData;
 100:         m_nLength = nNewLength;
 101:     }
 102:  
 103:         void InsertBefore(int nValue, int nIndex)
 104:     {
 105:         // Sanity check our nIndex value
 106:         assert(nIndex >= 0 && nIndex <= m_nLength);
 107:  
 108:         // First create a new array one element larger than the old array
 109:         int *pnData = new int[m_nLength+1];
 110:  
 111:         // Copy all of the elements up to the index
 112:         for (int nBefore=0; nBefore < nIndex; nBefore++)
 113:             pnData[nBefore] = m_pnData[nBefore];
 114:  
 115:         // insert our new element into the new array
 116:         pnData[nIndex] = nValue;
 117:  
 118:         // Copy all of the values after the inserted element
 119:         for (int nAfter=nIndex; nAfter < m_nLength; nAfter++)
 120:             pnData[nAfter+1] = m_pnData[nAfter];
 121:  
 122:         // Finally, delete the old array, and use the new array instead
 123:         delete[] m_pnData;
 124:         m_pnData = pnData;
 125:         m_nLength += 1;
 126:     }
 127:  
 128:     void Remove(int nIndex)
 129:     {
 130:         // Sanity check our nIndex value
 131:         assert(nIndex >= 0 && nIndex < m_nLength);
 132:  
 133:         // First create a new array one element smaller than the old array
 134:         int *pnData = new int[m_nLength-1];
 135:  
 136:         // Copy all of the elements up to the index
 137:         for (int nBefore=0; nBefore < nIndex; nBefore++)
 138:             pnData[nBefore] = m_pnData[nBefore];
 139:  
 140:         // Copy all of the values after the inserted element
 141:         for (int nAfter=nIndex+1; nAfter < m_nLength; nAfter++)
 142:             pnData[nAfter-1] = m_pnData[nAfter];
 143:  
 144:         // Finally, delete the old array, and use the new array instead
 145:         delete[] m_pnData;
 146:         m_pnData = pnData;
 147:         m_nLength -= 1;
 148:     }
 149:  
 150:     // A couple of additional functions just for convenience
 151:     void InsertAtBeginning(int nValue) { InsertBefore(nValue, 0); }
 152:     void InsertAtEnd(int nValue) { InsertBefore(nValue, m_nLength); }
 153:  
 154:     int GetLength() { return m_nLength; }
 155: };
 156:  
 157: #endif
 158:  
 159: Now, let’s test it just to prove it works:
 160: 1
 161: 2
 162: 3
 163: 4
 164: 5
 165: 6
 166: 7
 167: 8
 168: 9
 169: 10
 170: 11
 171: 12
 172: 13
 173: 14
 174: 15
 175: 16
 176: 17
 177: 18
 178: 19
 179: 20
 180: 21
 181: 22
 182: 23
 183: 24
 184: 25
 185: 26
 186: 27
 187: 28
 188: 29
 189: 30
 190: 31
 191: 32
 192: 33
 193:     
 194: #include <iostream>
 195: #include "IntArray.h"
 196:  
 197: using namespace std;
 198:  
 199: int main()
 200: {
 201:     // Declare an array with 10 elements
 202:     IntArray cArray(10);
 203:  
 204:     // Fill the array with numbers 1 through 10
 205:     for (int i=0; i<10; i++)
 206:         cArray[i] = i+1;
 207:  
 208:     // Resize the array to 8 elements
 209:     cArray.Resize(8);
 210:  
 211:     // Insert the number 20 before the 5th element
 212:     cArray.InsertBefore(20, 5);
 213:  
 214:     // Remove the 3rd element
 215:     cArray.Remove(3);
 216:  
 217:     // Add 30 and 40 to the end and beginning
 218:     cArray.InsertAtEnd(30);
 219:     cArray.InsertAtBeginning(40);
 220:  
 221:     // Print out all the numbers
 222:     for (int j=0; j<cArray.GetLength(); j++)
 223:         cout << cArray[j] << " ";
 224:  
 225:     return 0;
 226: }

posted on 2012-06-08 21:44 鐘謝偉 閱讀(1200) 評論(0)  編輯 收藏 引用

<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

導航

統計

常用鏈接

留言簿(1)

隨筆檔案

IT網站

My Friends

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久香蕉国产线看观看av| 国产精品视频九色porn| 日韩视频二区| 亚洲人精品午夜在线观看| 久久视频在线视频| 久久久久久亚洲精品中文字幕| 久久精品视频一| 免费观看成人www动漫视频| 欧美电影打屁股sp| 亚洲人成网站999久久久综合| 亚洲乱亚洲高清| 亚洲免费网站| 美女尤物久久精品| 国产精品99免费看 | 欧美aⅴ99久久黑人专区| 久久天堂成人| 亚洲人成在线观看网站高清| 亚洲视频播放| 久久综合精品国产一区二区三区| 欧美激情一区二区三区全黄| 国产精品一国产精品k频道56| 在线观看成人小视频| 一区二区免费在线视频| 久久青草福利网站| 亚洲美女在线国产| 久久精品二区三区| 国产精品99免费看| 亚洲欧洲日夜超级视频| 欧美一区二区视频97| 亚洲第一色在线| 一区二区三区欧美在线| 久久午夜羞羞影院免费观看| 日韩午夜激情电影| 美女露胸一区二区三区| 国产精品一二三视频| 亚洲精品视频一区二区三区| 久久久99久久精品女同性| 一区二区电影免费在线观看| 美国十次成人| 黄色小说综合网站| 欧美伊人久久| 亚洲免费观看视频| 欧美成人小视频| 在线成人小视频| 久久久999精品免费| 亚洲一区在线播放| 欧美三区在线视频| 在线综合亚洲| 亚洲乱码国产乱码精品精| 欧美大片在线观看一区二区| 亚洲国产精品久久久久婷婷老年| 久久久久久尹人网香蕉| 欧美一区成人| 国产一区二区三区黄| 久久爱另类一区二区小说| 亚洲欧美中文日韩在线| 国产精品久久久久免费a∨大胸| 亚洲最新中文字幕| 亚洲三级观看| 欧美日韩国产美| 一区二区三区四区国产| 亚洲精品在线视频观看| 欧美日韩性视频在线| 亚洲深夜福利在线| 亚洲视频一区| 国产欧美一区二区三区久久| 欧美伊人精品成人久久综合97| 亚洲专区免费| 国产视频一区在线| 久久综合久久综合九色| 久久影音先锋| 99re热这里只有精品免费视频| 亚洲三级视频在线观看| 欧美亚洲第一区| 欧美在线免费观看| 久久国产精品毛片| 亚洲国产欧美日韩| 日韩视频免费大全中文字幕| 国产精品成av人在线视午夜片| 性色av香蕉一区二区| 久久www成人_看片免费不卡| 亚洲高清色综合| 亚洲精品看片| 国产欧美在线观看| 亚洲第一久久影院| 国产精品v欧美精品v日韩| 久久国内精品视频| 母乳一区在线观看| 先锋a资源在线看亚洲| 久久在线免费视频| 亚洲在线视频免费观看| 麻豆freexxxx性91精品| 欧美福利视频在线观看| 欧美一区二区三区另类| 久久一区中文字幕| 亚洲一区二区三区欧美| 久久久亚洲一区| 亚洲性线免费观看视频成熟| 久久精品盗摄| 亚洲深夜激情| 麻豆精品网站| 午夜在线视频一区二区区别| 免费精品99久久国产综合精品| 午夜精品福利在线观看| 欧美精品成人| 久久亚洲欧美| 国产精品久久国产愉拍| 欧美激情第五页| 国产亚洲欧美一区| 亚洲精品视频中文字幕| 伊人久久av导航| 午夜在线观看免费一区| 亚洲小少妇裸体bbw| 麻豆成人在线播放| 久久精品论坛| 国产精品嫩草99av在线| 亚洲精品日韩在线观看| 亚洲国产婷婷| 久久亚洲高清| 久久免费午夜影院| 国产欧美在线观看| 亚洲免费一区二区| 性欧美精品高清| 国产精品vvv| av成人激情| 一区二区激情| 欧美精品三级| 欧美激情女人20p| 在线成人小视频| 久久久激情视频| 美女成人午夜| 在线看日韩av| 久久在线视频在线| 免播放器亚洲一区| 亚洲国产精品激情在线观看| 麻豆国产va免费精品高清在线| 另类综合日韩欧美亚洲| 精品9999| 免费一级欧美片在线观看| 欧美高清成人| 亚洲精品一区二区三区樱花| 欧美另类在线播放| 一区二区三区不卡视频在线观看| 亚洲无亚洲人成网站77777| 国产精品s色| 欧美亚洲免费电影| 久久久99爱| 亚洲欧洲另类国产综合| 欧美片网站免费| 亚洲一级二级| 久久精品一区二区国产| 狠狠色综合网| 欧美国产成人在线| 夜夜嗨av一区二区三区网页 | 久久精品综合一区| 黄色一区二区在线| 欧美激情黄色片| 在线亚洲精品福利网址导航| 久久国产天堂福利天堂| 欧美国产高潮xxxx1819| 亚洲欧洲午夜| 午夜国产精品影院在线观看| 国产视频一区二区在线观看| 麻豆精品一区二区综合av| 亚洲精品一区二区在线| 欧美一区二区三区在线| 在线观看亚洲视频| 欧美日本不卡高清| 亚洲欧美一区二区原创| 裸体歌舞表演一区二区| 一区二区三区成人 | 亚洲一区二区日本| 国产目拍亚洲精品99久久精品 | 夜夜狂射影院欧美极品| 欧美一区二区三区免费大片| 亚洲国产日韩欧美| 国产精品丝袜久久久久久app| 久久乐国产精品| 一区二区三区国产精品| 欧美成人三级在线| 午夜视频一区二区| 亚洲精品久久久久久下一站| 国产精品影片在线观看| 欧美激情视频一区二区三区免费| 亚洲欧美日本日韩| 亚洲免费久久| 欧美激情视频一区二区三区免费| 午夜亚洲性色福利视频| 99riav1国产精品视频| 怡红院精品视频在线观看极品| 国产精品国产三级国产a| 欧美成人激情视频免费观看| 久久精品一二三区| 欧美一区二区在线免费播放| 一区二区三区.www| 亚洲九九爱视频| 亚洲激情一区二区| 欧美二区在线| 欧美不卡高清| 欧美成人精品激情在线观看|