Vectors
Vectors are one of many ways of keeping a collection of values “under one roof.” We can give one name to a collection of values and then access each value by a position. We can also add more values or remove values.
(The “traditional” way of representing a collection of values is the “array.” We won’t be looking at arrays this quarter because they’re much more difficult to use than vectors and, for our purposes, they aren’t any more powerful.)
Vector usage
When you create a vector, you have to decide what (single) kind of value it will hold. This type is specified in a funny way (using a “template” which we’ll learn about later).
Here is how you create a vector full of integers:
(Don’t forget #include <vector>
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
:
You can retrieve the size of a vector with the size
function:
And you can get to an element in the vector with [i]
where i
is
some position (“index”):
Note that positions for vectors start at 0, so [1]
refers to the
second element (the value 0.66).
Using the size
function and the []
syntax, we can print the
contents of a vector using a simple for()
loop:
Example 1 - simple vector
Output:
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.
Output:
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
The clear()
function deletes all the values in the vector.
Output:
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.
Output:
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
The clear()
function deletes all the values in the vector. The
empty()
function tells us if a vector has no values.
Output:
Names vector is empty.
Example 6 - sorting
Vectors can sort themselves (using the “quick sort” technique).
Output:
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.
Output:
--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.
Output:
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 reference 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 use “call-by-reference” in the parameter:
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 (mystring.reverse()
), etc.
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 >>
like
in 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 i
and j
start
counting at 0).