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

            簡單的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 鐘謝偉 閱讀(1195) 評論(0)  編輯 收藏 引用

            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日本精品久久久久中文字幕8| 99精品久久精品一区二区| 久久久噜噜噜久久| 一本色道久久88综合日韩精品 | 亚洲一级Av无码毛片久久精品| 色综合久久久久综合99| 久久久无码一区二区三区| 狠狠精品久久久无码中文字幕| 思思久久精品在热线热| 高清免费久久午夜精品| 久久久久久无码国产精品中文字幕| 久久久久亚洲AV片无码下载蜜桃| 99久久精品免费看国产一区二区三区| 久久国产精品久久国产精品| 一本久久a久久精品综合香蕉 | 久久精品国产亚洲精品2020 | 少妇内射兰兰久久| 久久er热视频在这里精品| 久久久噜噜噜久久中文字幕色伊伊| 色婷婷综合久久久久中文一区二区 | 久久天堂AV综合合色蜜桃网| 久久久久久亚洲精品不卡 | 一本久久a久久精品综合香蕉 | 一本久久a久久精品vr综合| 久久国产综合精品五月天| 国内精品久久久久伊人av| 久久久久se色偷偷亚洲精品av| 亚洲国产精品久久| 久久Av无码精品人妻系列 | 岛国搬运www久久| 久久婷婷五月综合97色| 久久综合亚洲鲁鲁五月天| 四虎国产精品成人免费久久| 99久久精品免费看国产免费| 精品久久一区二区三区| 久久99国产乱子伦精品免费| 亚洲va久久久噜噜噜久久天堂| 亚洲七七久久精品中文国产| 蜜臀久久99精品久久久久久| 久久久久99精品成人片| 国产激情久久久久影院小草|