Vectors

6.1 - Creating vectors


A vector is a collection of objects of a same type. For example you can have a vector of integers, a vector of strings, or even a vector of your own custom objects.

To use vectors, you need to include the <vector> header:

#include <vector>

To create a vector, you need to put in the type it will hold. For example, to create a vector called grades which holds integers:

std::vector<int> grades;

Your task is to create a vector called heights which holds double values on line number 7:

#include <iostream> #include <vector> int main() { // Create your vector heights after this line! // This code puts the following values into heights. // It will be explained later. heights.push_back(181.5); heights.push_back(153.25); heights.push_back(197.75); // This code takes the values from the vector heights and print them. std::cout << heights[0] << '\n' << heights[1] << '\n' << heights[2]; }