Vector

The vector class has the behavior of a dynamic array; that is, there is indexed random access to items and the container will grow as more items are inserted into it. You can create a vector object with an initialization list, and with a specified number of copies of an item. You can also base a vector on values in another container by passing iterators that indicate the range of items in that container. You can create a vector with a pre-determined size by providing a capacity as the constructor parameter, and the specified number of default items will be created in the container. If, at a later stage, you need to specify the container size, you can call the reserve method to specify the minimum size or the resize method, which may mean deleting excess items or creating new items depending on whether the existing vector object is bigger or smaller than the requested size.

When you insert items into a vector container and there is not enough memory allocated, then the container will allocate enough memory. This will involve allocating new memory, copying the existing items into the new memory, creating the new item, and, finally, destroying the old copy of the items and deallocating the old memory. Clearly, if you know the number of items and you know that the vector container will not be able to contain them without a new allocation, you should indicate how much space you need by calling the reserve method.

Inserting items other than the constructor is straightforward. You can use push_back to insert an item at the end (which is a fast action, assuming no allocation is needed) and there is also pop_back to remove the last item. You can also use the assign method to clear the entire container and insert the specified items (either a multiple of the same item, an initializer list of items, or items in another container specified with iterators). As with list objects, you can clear the entire vector, erase items at a position, or insert items at a specified position. However, there is no equivalent of the remove method to remove items with a specific value.

The main reason to use the vector class is to get random access using either the at method or the [] indexing operator:

   vector<int> distrib(10); // ten intervals 
for (int count = 0; count < 1000; ++count)
{
int val = rand() % 10;
++distrib[val];
}
for (int i : distrib) cout << i << endl;

The first line creates a vector with ten items, and then in the loop the C runtime function rand is called a thousand times each time to get a pseudo random number between 0 and 32767. The modulus operator is used to get, in approximate terms, a random number between 0 and 9. This random number is then used as an index for the distrib object to select a specified item, which is then incremented. Finally, the distribution is printed out and, as you would expect, this gives a value of roughly 100 in each item.

This code relies on the fact that the [] operator returns a reference to the item, which is why the item can be incremented in this manner. The [] operator can be used to read and write to an item in the container. The container gives iterator access through the begin and end methods, and (since they are needed by the container adapters) the front and back methods.

A vector object can hold any type that has a copy constructor and assignment operator, which means all the built-in types. As it stands, a vector of bool items would be a waste of memory because a Boolean value can be stored as a single bit and the compiler will treat a bool as an integer (32 bits). The Standard Library has a specialization of the vector class for bool that stores items more efficiently. However, although the class at first sight looks like a good idea, the problem is that, since the container holds Boolean values as bits, this means that the [] operator doesn't return a reference to a bool (instead it returns an object that behaves like one).

If you want to hold Boolean values and manipulate them then, as long as you know at compile time how many items there are, the bitset class is probably a better choice.