Vector (STL)
From Wikipedia, the free encyclopedia
Vector (or std::vector) is a C++ implementation of the dynamic array data structure. Its interface emulates the behavior of a C array (i.e., capable of fast random access) but with the additional ability to automatically resize itself when inserting or erasing an object.
Vector is a template class that is a part of the C++ Standard Template Library. It can store any type, but is limited to storing only one type per instance. It provides a standard set of methods for accessing elements, adding elements to the start or end, deleting elements and finding how many elements are stored.
Contents |
[edit] Comparison with Arrays
In C++, arrays are contiguous pieces of memory. They are somewhat like blocks aligned together, each block is then given a number, and to look at the contents of each block one only has to supply the number of the block. All the elements of an array must be of the same type.
A vector is similar to an array but with extended functionality. One of the features of a vector is that if you use the at() function, an exception will be thrown if you try and access an element that doesn't exist. A vector is a template class, it is a generic array of whatever type you want it to be. This gives vectors a great deal of flexibility, as they can be used as an array of anything. For instance, you can even have a vector of vectors. Vectors are difficult to get out of bounds errors with (see Bounds checking) and they are very fast for random access. Vectors also have a number of useful functions which can tell you certain properties of the vector. For a full description of each function see functions further down the page.
[edit] Usage
A vector is included into a C++ program by including the header <vector>. <vector.h> should not be used as it is non-standard. The implementation that GCC provides has the old headers, which give a compiler warning when an attempt is made to use them.
A vector is initialised using:
std::vector<type> instance;
where "type" is the type you want to store in the vector and instance is the name of the variable that you refer to the new vector with.
"Type" can be a primitive or any user defined class or struct.
[edit] Functions
- vector::front - Returns reference to first element of vector.
- vector::back - Returns reference to last element of vector.
- vector::push_back - Appends (inserts) an element to the end of a vector, allocating memory for it if necessary. Amortized O(1) time.
- vector::pop_back - Erases the last element of the vector, possibly reducing capacity. Amortized O(1) time.
- vector::size - Returns number of elements in the vector.
- vector::begin - Returns an iterator to start traversal of the vector.
- vector::end - Returns an iterator for the last element of the vector.
- vector::empty - Returns true if vector has no elements.
- vector::insert - Inserts elements into a vector (single & range), shifts later elements up. Inefficient.
- vector::erase - Deletes elements from a vector (single & range), shifts later elements down. Inefficient.
... and many more not listed here