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

            簡(jiǎn)單的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) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


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

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網(wǎng)站

            My Friends

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久er国产精品免费观看2| 亚洲国产精品久久66| 久久精品一区二区三区中文字幕| 久久综合久久自在自线精品自| 久久综合五月丁香久久激情| 88久久精品无码一区二区毛片 | 欧美亚洲国产精品久久| 九九热久久免费视频| 亚洲成人精品久久| 中文精品99久久国产| 亚洲精品国产第一综合99久久| 久久青青草原精品国产软件| 午夜精品久久久久久毛片| 久久久亚洲裙底偷窥综合| 欧美亚洲国产精品久久| 国产亚洲美女精品久久久久狼| 一本一道久久a久久精品综合 | 国产精品久久久久久久久久影院 | 亚洲AV成人无码久久精品老人| 成人亚洲欧美久久久久| 国产无套内射久久久国产| 无码人妻少妇久久中文字幕蜜桃| 久久久91人妻无码精品蜜桃HD| 欧美喷潮久久久XXXXx| 亚洲国产香蕉人人爽成AV片久久| 久久99国产精品久久99| 国产精品99久久久精品无码| 少妇精品久久久一区二区三区| 久久这里只有精品视频99| 久久精品成人免费看| 日韩乱码人妻无码中文字幕久久 | 久久福利资源国产精品999| 91精品婷婷国产综合久久| 久久A级毛片免费观看| 久久综合九色综合精品| 久久综合九色综合久99| 久久99精品国产麻豆宅宅| 色综合久久无码中文字幕| 亚洲伊人久久综合影院| 久久亚洲2019中文字幕| 91精品国产91久久久久久蜜臀 |