Most experimented C/C++ programmers agree that the use of array is difficult and prone to errors; it is always easy to reference elements past the end of array. This kind of bug is especially hard to track since it will not occur all the time, and what is worse is that it has a tendency of appearing when the program is under heavy stress. Also, counting the actual number of elements from an array is not an easy task.
The STL has a very good replacement for arrays: the vector container. What is so wonderful about this container is that you can use it much like an array, thus the learning curve is very smooth.
Unlike an array a vector is automatically managing its size. While there is still memory you can always insert elements into it. The programmer is not forced to guess the maximum number of required elements and to allocate that number right from the start.
Vector operations:
The size of the resulting vector is 0 and there is no allocated space for it; when the first element is inserted in the vector then a certain amount of memory will be allocated to it; (the amount of allocated memory is system-dependent);
Note:
When the vector's allocated space will be fulfilled, a bigger chuck of memory is reallocated; this reallocation is a costly operation, thus if you know that your vector will grow up very fast it may be useful to preallocate memory so that you can avoid futile reallocations.
The above code will allocate enough memory to hold 100 objects of type CAtom. The constructors for each object will NOT be called; the elements of the vector are NOT initialized, only the memory will be allocated and thus it is NOT correct to make a call like:
You can verify that by calling the size() member function; the size() will return a value of 0, which is correct. At this point the vector is empty.
This call is equivalent to
The constructor will be called for each contained object, thus it is safely to write:
The function that will perform this operation is push_back; thus, to insert an element in our vector we will do:
Note A1:
The [ ] operator is NOT inserting elements in a vector. The [ ] operator should only be used to access elements.
The following code is not correct:
After the above code the size() member function will return 0.
Note A2:
On all the platforms that I know the above code will not crash the application. Even so, its behavior is undefined. It is illegal to call the [ ] operator for an index out of the range 0..size-1.
Note B:
When inserting elements in a vector reallocation will occur if the vector is too small to contain all the required elements. When reallocation occurs the vector's size will be doubled and all iterators (for now think to an iterator like to a special kind of pointer) and references will become invalid.
Practically you can think of it this way: an STL vector is just an array. When reallocation occurs all the vector's elements are copied to a new location, thus any pointers to the old vector will become invalid.
Warning:
This is an expensive operation; if you need this functionality often then consider using a list instead.
If reallocation occurs, the vector's size will double and all iterators and references will become invalid. If the reallocation will not occur then the iterators will become invalid from the point of the insertion to the end of the vector.
Note:
For a vector only the methods push_back() and pop_back() are implemented. Their logical pair: push_front() and pop_front() are not implemented because they cannot be implemented in constant time.
Note:
The same operation can be applied to a standard array:
Warning:
This is an expensive operation; if you need this functionality often then consider using a list instead.
The last operation will assign all elements to the previous one and it will destruct the last element. Because the vector will actually shrink no reallocation will occur but all the iterators will be invalidated.
Note:
Before deleting elements from a vector make sure that the vector is not empty.
The vector is sorted with a template function called sort. Before sorting a vector the developer must define the "<" operator for the contained object.
Sample code: