Creating a Vector
In the lesson on conditions, you learned how a program makes decisions. In this lesson, you'll learn to store several values together.
Imagine you want to store your scores from five tests. With what you know so far, you would need five variables:
int score1 = 85;
int score2 = 92;
int score3 = 78;
int score4 = 90;
int score5 = 88;That's a lot of typing. And what if there's a sixth test? You would have to create a new variable for every new score. That's what a vector is for. A vector stores several values of the same type in one variable:
std::vector<int> scores = {85, 92, 78, 90, 88};The values in a vector are called its elements. This vector has five elements, and their order matters: 85 is the first element, 88 the last one.
Let's go through it from left to right:
std::vectorsays that you're creating a vector. It comes from the standard library, just likestd::coutandstd::string, so it starts withstd::.- The angle brackets
< >hold the data type of the elements.std::vector<int>is a vector of whole numbers,std::vector<double>a vector of decimal numbers andstd::vector<std::string>a vector of texts. scoresis the name of the variable. The same rules apply to it as to the names of other variables.- The curly braces
{ }hold the elements, separated by commas.
To use vectors, write #include <vector> at the top of your program, just like #include <string> for texts. Sometimes a program works without it, because another #include brings vectors in. Don't rely on that and always write it.
More examples:
std::vector<std::string> names = {"Jennifer", "Boris", "Nancy"};
std::vector<double> prices = {29.9, 149.9, 99.9};
std::vector<int> points;The vector points has no elements, it's empty. Unlike an int variable without a starting value, it doesn't hold any random values. There's simply nothing in it yet. You'll learn to add elements to a vector later in this lesson.
All elements must have the type written in the angle brackets. If you put a decimal number into a vector of whole numbers, the program doesn't compile:
std::vector<int> scores = {85, 92.5, 78};main.cpp:6:36: error: type 'double' cannot be narrowed to 'value_type' (aka 'int') in initializer list [-Wc++11-narrowing]The message says that a double value can't be narrowed to the type of the vector's elements (value_type), which is int. The decimal part would be lost, so C++ reports an error instead. You can ignore the name in square brackets at the end of the message.
You can't print a whole vector at once, only element by element. To print an element, write its number in square brackets after the vector's name. names[0] is the first element of the vector names:
std::cout << names[0] << std::endl;This prints:
JenniferYou'll find out why counting starts at zero in the next exercise. The program in the editor prints the first colleague the same way.
Instructions
- 1
Below the comment, create a vector of whole numbers named
scoreswith the elements85,92,78and90, in this order. - 2
Below the vector, print the first score with a label, just like the program prints the first colleague. The output should include the line
First score: 85.