In real life, we use containers all the time. Your breakfast cereal comes in a box, the pages in your book come inside a cover and binding, and you might store any number of items in containers in your garage. Without containers, it would be extremely inconvenient to work with many of these objects. Imagine trying to read a book that didn’t have any sort of binding, or eat cereal that didn’t come in a box without using a bowl. It would be a mess. The value the container provides is largely in it’s ability to help organize and store items that are put inside it.
Similarly, a
container class is a class designed to hold and organize multiple instances of another class. There are many different kinds of container classes, each of which has various advantages, disadvantages, and restrictions in their use. By far the most commonly used container in programming is the array, which you have already seen many examples of. Although C++ has built-in array functionality, programmers will often use an array container class instead because of the additional benefits it provides. Unlike built-in arrays, array container classes generally provide dynamically resizing (when elements are added or removed) and do bounds-checking. This not only makes array container classes more convenient than normal arrays, but safer too.
Container classes typically implement a fairly standardized minimal set of functionality. Most well-defined containers will include functions that:
- Create an empty container (via a constructor)
- Insert a new object into the container
- Remove an object from the container
- Report the number of objects currently in the container
- Empty the container of all objects
- Provide access to the stored objects
- Sort the elements (optional)
Sometimes certain container classes will omit some of this functionality. For example, arrays container classes often omit the insert and delete functions because they are slow and the class designer does not want to encourage their use.
Container classes generally come in two different varieties.
Value containers are compositions that store copies of the objects that they are holding (and thus are responsible for creating and destroying those copies).
Reference containers are aggregations that store pointers or references to other objects (and thus are not responsible for creation or destruction of those objects).
Unlike in real life, where containers can hold whatever you put in them, in C++, containers typically only hold one type of data. For example, if you have an array of integers, it will only hold integers. Unlike some other languages, C++ generally does not allow you to mix types inside a container. If you want one container class that holds integers and another that holds doubles, you will have to write two separate containers to do this (or use templates, which is an advanced C++ feature). Despite the restrictions on their use, containers are immensely useful, and they make programming easier, safer, and faster.
An array container class
In this example, we are going to write an integer array class that implements most of the common functionality that containers should have. This array class is going to be a value container, which will hold copies of the elements its organizing.
First, let’s create the IntArray.h file:
Our IntArray is going to need to keep track of two values: the data itself, and the size of the array. Because we want our array to be able to change in size, we’ll have to do some dynamic allocation, which means we’ll have to use a pointer to store the data.
Now we need to add some constructors that will allow us to create IntArrays. We are going to add two constructors: one that constructs an empty array, and one that will allow us to construct an array of a predetermined size.
19 | m_pnData = new int [nLength]; |
We’ll also need some functions to help us clean up IntArrays. First, we’ll write a destructor, which simply deallocates any dynamically allocated data. Second, we’ll write a function called Erase(), which will erase the array and set the length to 0.
Now let’s overload the [] operator so we can access the elements of the array. We should bounds check the index to make sure it’s valid, which is best done using the assert() function. We’ll also add an access function to return the length of the array.
04 | #include <assert.h> // for assert() |
21 | m_pnData = new int [nLength]; |
39 | int & operator[]( int nIndex) |
41 | assert (nIndex >= 0 && nIndex < m_nLength); |
42 | return m_pnData[nIndex]; |
45 | int GetLength() { return m_nLength; } |
At this point, we already have an IntArray class that we can use. We can allocate IntArrays of a given size, and we can use the [] operator to retrieve or change the value of the elements.
However, there are still a few thing we can’t do with our IntArray. We still can’t change it’s size, still can’t insert or delete elements, and we still can’t sort it.
First, let’s write some code that will allow us to resize an array. We are going to write two different functions to do this. The first function, Reallocate(), will destroy any existing elements in the array when it is resized, but it will be fast. The second function, Resize(), will keep any existing elements in the array when it is resized, but it will be slow.
03 | void Reallocate( int nNewLength) |
13 | m_pnData = new int [nNewLength]; |
14 | m_nLength = nNewLength; |
19 | void Resize( int nNewLength) |
35 | int *pnData = new int [nNewLength]; |
42 | int nElementsToCopy = (nNewLength > m_nLength) ? m_nLength : nNewLength; |
45 | for ( int nIndex=0; nIndex < nElementsToCopy; nIndex++) |
46 | pnData[nIndex] = m_pnData[nIndex]; |
56 | m_nLength = nNewLength; |
Whew! That was a little tricky!
Many array container classes would stop here. However, just in case you want to see how insert and delete functionality would be implemented we’ll go ahead and write those too. Both of these algorithms are very similar to Resize().
01 | void InsertBefore( int nValue, int nIndex) |
04 | assert (nIndex >= 0 && nIndex <= m_nLength); |
07 | int *pnData = new int [m_nLength+1]; |
10 | for ( int nBefore=0; nBefore < nIndex; nBefore++) |
11 | pnData[nBefore] = m_pnData[nBefore]; |
14 | pnData[nIndex] = nValue; |
17 | for ( int nAfter=nIndex; nAfter < m_nLength; nAfter++) |
18 | pnData[nAfter+1] = m_pnData[nAfter]; |
26 | void Remove( int nIndex) |
29 | assert (nIndex >= 0 && nIndex < m_nLength); |
32 | int *pnData = new int [m_nLength-1]; |
35 | for ( int nBefore=0; nBefore < nIndex; nBefore++) |
36 | pnData[nBefore] = m_pnData[nBefore]; |
39 | for ( int nAfter=nIndex+1; nAfter < m_nLength; nAfter++) |
40 | pnData[nAfter-1] = m_pnData[nAfter]; |
49 | void InsertAtBeginning( int nValue) { InsertBefore(nValue, 0); } |
50 | void InsertAtEnd( int nValue) { InsertBefore(nValue, m_nLength); } |
Here is our IntArray container class in it’s entirety:
004 | #include <assert.h> // for assert() |
019 | IntArray( int nLength) |
021 | m_pnData = new int [nLength]; |
039 | int & operator[]( int nIndex) |
041 | assert (nIndex >= 0 && nIndex < m_nLength); |
042 | return m_pnData[nIndex]; |
047 | void Reallocate( int nNewLength) |
057 | m_pnData = new int [nNewLength]; |
058 | m_nLength = nNewLength; |
063 | void Resize( int nNewLength) |
079 | int *pnData = new int [nNewLength]; |
086 | int nElementsToCopy = (nNewLength > m_nLength) ? m_nLength : nNewLength; |
089 | for ( int nIndex=0; nIndex < nElementsToCopy; nIndex++) |
090 | pnData[nIndex] = m_pnData[nIndex]; |
100 | m_nLength = nNewLength; |
103 | void InsertBefore( int nValue, int nIndex) |
106 | assert (nIndex >= 0 && nIndex <= m_nLength); |
109 | int *pnData = new int [m_nLength+1]; |
112 | for ( int nBefore=0; nBefore < nIndex; nBefore++) |
113 | pnData[nBefore] = m_pnData[nBefore]; |
116 | pnData[nIndex] = nValue; |
119 | for ( int nAfter=nIndex; nAfter < m_nLength; nAfter++) |
120 | pnData[nAfter+1] = m_pnData[nAfter]; |
128 | void Remove( int nIndex) |
131 | assert (nIndex >= 0 && nIndex < m_nLength); |
134 | int *pnData = new int [m_nLength-1]; |
137 | for ( int nBefore=0; nBefore < nIndex; nBefore++) |
138 | pnData[nBefore] = m_pnData[nBefore]; |
141 | for ( int nAfter=nIndex+1; nAfter < m_nLength; nAfter++) |
142 | pnData[nAfter-1] = m_pnData[nAfter]; |
151 | void InsertAtBeginning( int nValue) { InsertBefore(nValue, 0); } |
152 | void InsertAtEnd( int nValue) { InsertBefore(nValue, m_nLength); } |
154 | int GetLength() { return m_nLength; } |
Now, let’s test it just to prove it works:
12 | for ( int i=0; i<10; i++) |
19 | cArray.InsertBefore(20, 5); |
25 | cArray.InsertAtEnd(30); |
26 | cArray.InsertAtBeginning(40); |
29 | for ( int j=0; j<cArray.GetLength(); j++) |
30 | cout << cArray[j] << " " ; |
This produces the result:
40 1 2 3 5 20 6 7 8 30
Although writing container classes can be pretty complex, the good news is that you only have to write them once. Once the container class is working, you can use and reuse it as often as you like without any additional programming effort required.
It is also worth explicitly mentioning that even though our sample IntArray container class holds a built-in data type (int), we could have just as easily used a user-defined type (eg. a point class).
No comments:
Post a Comment