Section 13.2 Creating vectors
To use vectors, we must first include the
<vector>
library. Then we can declare a variable with the type vector<TYPE>
where TYPE
is a data type like int
, double
, or string
. (The <>
are referred to as angle brackets). For example, the following lines declare that counts
is an βvector of integersβ and values
is a βvector of doublesβ:
vector<int> counts;
vector<double> values;
We do not have to initialize vectors. Declaring a vector automatically creates an empty vector. Just like a string, we can ask a vector for its
.size()
which will be a size_t
. Doing so on one of those vectors would return 0:
If we want to initialize the vector, we can do so with a list of specific values by enclosing those values in braces and separating them with commas. This is known as list initialization:
vector<int> counts = {5, 6, 3, 2};
vector<string> words = {"pear", "apple", "banana"};
The first line creates a vector of four integers, and the second line creates a vector of three strings.
Finally, you can create a vector with a specific size and default value. The syntax to do this is a little odd; it looks like a combination of a variable declarations and a function call:
vector<int>(10, 0)
. In fact, thatβs exactly what it is. The function we are invoking is an vector
constructor. A constructor is a special function that creates new objects and initializes their instance variables. In this case, the constructor takes two arguments, the size of the vector to make and what value to use for all of the elements.
// Create a vector of size 5 and initialize it with 0s
vector<int> counts(5, 0);
// Create a vector of size 3 and initialize it with 100.0
vector<double> values(3, 100.0);
values
created by the previous code sample.Once you have a vector, you can copy it by doing an assignment:
vector<int> numbers = {1, 2, 3};
// Declare copy and assign it the same value as numbers
vector<int> copy = numbers;
This results in two vectors that have identical lists of elements as diagramed below. Despite having the same data, the two vectors are independent - if you modified
numbers
at this point, copy
would be unaffected.
You may have noticed that we have not tried to print any of the vectors. That is because you canβt use
cout
to print an entire vector. Trying to write something like `cout << values;` will result in an error message that begins with something like:
test.cpp: In function βint main()β: test.cpp:13:10: error: no match for βoperator<<β (operand types are βstd::ostreamβ {aka βstd::basic_ostream<char>β} and βstd::vector<int>β) 13 | cout << values; | ~~~~ ^~ ~~~~~~
It is trying to tell us that there is no way to use
<<
to send the vector to the output stream cout
. If we want to print the vector we have to it by printing the individual elements. In the next section, we will learn how to access the elements so that we can see what is in our vector and modify those values.
Checkpoint 13.2.2.
How would you create a vector of five words and initialize all of them to empty strings?
vector<string> words ("", 5);
- Incorrect! Vector parameters are in the wrong order.
vector<string> words (5);
- Correct! Vector elements are default constructed to empty strings.
vector<string> words (5, "");
- Correct! We made a vector of strings with 5 elements, initialized to empty strings.
vector<char> words (5, '');
- Incorrect! words should be a vector of strings.
Checkpoint 13.2.3.
Checkpoint 13.2.4.
Checkpoint 13.2.5.
You have attempted of activities on this page.