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 a 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;
| ~~~~ ^~ ~~~~~~
That message 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. (We will see that soon.)
Checkpoint 13.2.2.
How could 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.
Multiple Response Which of the following could be an element of a
vector<string> ?
- 1
- Incorrect! This is an integer, not a string.
- "a"
- Correct!
- โaโ
- Incorrect! This is a character, not a string.
- "word"
- Correct!
- "1"
- Correct!
Checkpoint 13.2.4.
What are the values of
numberโs elements after this declaration?
vector<int> numbers(6);
- undefined (we donโt know the values)
- Integers are default constructed to a known value.
- 0
- Integers are default constructed to a zero value.
- 6
- 6 is the size we want the vector to be.
Checkpoint 13.2.5.
You have attempted of activities on this page.
