vector class is provided in the C++ “standard template library” (STL), and available with
#include <vector>. It uses arrays but provides a variety of convenience functions. Because it is a template class (see the Template meta-programming notes), you can store any type of value in a vector, but only one type of value in each different vector.
Because vectors use arrays to store data, access to individual elements in the vector is fast. However, adding new elements can be slow, if it is forced to grow the vector (which involves copying the whole array).
Here is how you create a vector full of integers:
#include <vector> and
using namespace std; at the top of your files when you use vectors).
If you want doubles instead:
It’s not possible to have a vector full of doubles and ints, for example (“heterogeneous” containers are not possible in C++).
You can put elements in your vector with several methods. Most common is
push_back is fast because the vector usually keeps an array that’s bigger than the data in it, so there are free spaces at the end. When those spaces run out, it creates a new (oversized) array and copies the old values into the new array.
You can also use
insert_front, but that requires copying all the values down one position, so it’s quite slow.
You can retrieve the size of a vector with the
And you can get to an element in the vector with
some position (“index”):
Note that positions for vectors start at 0, just like arrays, so
 refers to the second element (the value 0.66).
size function and the
 syntax, we can print the contents of a vector using a simple
The last element can be obtained with
back(), and the
pop_back() method returns and removes the last element.
Example 1 - simple vector
Size of vals: 3 Value at index 0 is 5 Value at index 1 is 6 Value at index 2 is 1
Example 2 - vector with initial values
You can create a vector of some specific size and give it an initial (repeated) value. This is most often used to give a vector a bunch of zeros.
Size of vals: 13 Value at index 0 is 0 Value at index 1 is 0 Value at index 2 is 0 Value at index 3 is 0 Value at index 4 is 0 Value at index 5 is 0 Value at index 6 is 0 Value at index 7 is 0 Value at index 8 is 0 Value at index 9 is 0 Value at index 10 is 5 Value at index 11 is 6 Value at index 12 is 1
Example 3 - clearing a vector
clear() function deletes all the values in the vector.
Size of vals: 13 Value at index 0 is 0 Value at index 1 is 0 Value at index 2 is 0 Value at index 3 is 0 Value at index 4 is 0 Value at index 5 is 0 Value at index 6 is 0 Value at index 7 is 0 Value at index 8 is 0 Value at index 9 is 0 Value at index 10 is 5 Value at index 11 is 6 Value at index 12 is 1 Size of vals: 0
Example 4 - vector of strings
You can put anything in vectors, even strings (which are themselves more-or-less vectors, too). You can put vectors inside vectors, ad nauseum.
Size of names: 4 Name at index 0 is Euler Name at index 1 is Descartes Name at index 2 is Turing Name at index 3 is Church
Example 5 - using the empty() function
clear() function deletes all the values in the vector. The
empty() function tells us if a vector has no values.
Names vector is empty.
Example 6 - sorting
Vectors can sort themselves (using the “quick sort” technique).
Name at index 0 is Church Name at index 1 is Descartes Name at index 2 is Euler Name at index 3 is Turing
Example 7 - random shuffling
The reverse of sorting is shuffling; sometimes useful to randomize the order of our data for experiments.
--Sorted names: Church Curry Descartes Euler Gauss Hopper Lovelace McCarthy Riemann Turing --Randomly shuffled names: Curry Hopper Turing Descartes Gauss Lovelace McCarthy Church Euler Riemann
Example 8 - passing a vector to a function
When you pass a vector to the function, the whole package is copied and given to the function; so if you change the vector in the function, the rest of the world is not affected. Also note that while you have to provide a function the size of the array when you use arrays and functions together, the vector already knows how big it is, so we don’t need a “size” parameter.
Sum of integers 1 to 1000: 500500
Example 11 - returning a vector from a function
Vectors can be returned from functions, in the usual way:
Example 12 - passing a vector by pointer to a function
If we want the function to be able to modify the vector given in a parameter, or we simply want to avoid the cost of copying a large vector, we can give a pointer to the vector:
Strings are (just like) vectors
Interestingly enough, strings are just vectors of
char values (more
or less). We can use the same vector functions on strings
(mostly). For example, we can ask a string its size
mystring.size()), reverse it (
Using 2D vectors
Since a vector can store any single type of value, and vectors themselves are types of values, you can put vectors inside a vector:
Note that the extra space in the first line of code:
> > is
necessary, otherwise the compiler thinks you are writing
cin >> x.
Storing 2D structures in 1D structures
If you want to keep a “2D matrix” in a 1D structure, you have to use a
simple calculation to figure out where row i, column j exists
inside the 1D structure. If we essentially lay out the matrix with
each row appended to the prior, then we get the following calculation:
[i][j] is the same as
[i * cols + j] (assuming
counting at 0).