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 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
December 11, 170
171: 12
December 13, 172
December 14, 173
December 15, 174
175: 16
176: 17
177: 18
December 19, 178
179: 20
180: 21
181: 22
182: 23
December 24, 183
December 25, 184
December 26, 185
December 27, 186
December 28, 187
December 29, 188
189: 30www.2cto.com
December 31, 190
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 :}
Zhong xiwei