Lesson 10 · 10 of 20
Vectors · C++
Store lists in C++ with std::vector: create them, read with [] and at(), get their size with size() and add elements with push_back.

`std::vector` is the C++ list: it holds elements of one type, knows how many it has and can grow. The type goes between < >: std::vector<std::string> for the players and std::vector<int> for their points. It needs #include <vector>.
`size()` gives the number of elements, `[]` reads or changes one by its position, starting at 0, and `back()` returns the last one. scores[1] = 110 updates Luis's points.
To go through two vectors together you use a for with an index. The index is a std::size_t, the unsigned integer that size() returns; with an int, some compilers would warn that you are comparing different types.
`push_back()` adds an element at the end: Pablo and his 130 points. Then the range-based for adds up all the points, and static_cast<double> avoids integer division when working out the average.
`at()` does the same as [] but checks that the position exists and, if it does not, throws an error instead of reading someone else's memory. Finally, std::vector<int> lives(4, 3) creates a vector of 4 elements, all set to 3.
Code · main.cpp
#include <iostream>#include <string>#include <vector>int main() { // A vector is a list that knows its size and can grow. std::vector<std::string> players = {"Ana", "Luis", "Marta"}; std::vector<int> scores = {120, 95, 140}; std::cout << "Players: " << players.size() << '\n'; std::cout << "First: " << players[0] << ", last: " << players.back() << '\n'; scores[1] = 110; // [] also changes an element; positions start at 0 // A for with an index, to use two vectors at the same time. for (std::size_t i = 0; i < players.size(); i++) std::cout << players[i] << ": " << scores[i] << " points\n"; // push_back adds an element at the end. players.push_back("Pablo"); scores.push_back(130); // A range-based for reads every value. int total = 0; for (int s : scores) total += s; std::cout << "Total: " << total << ", average: " << total / static_cast<double>(scores.size()) << '\n'; // at() checks that the position exists; [] does not. std::cout << "Fourth player: " << players.at(3) << '\n'; // A vector of 4 elements, all of them 3. std::vector<int> lives(4, 3); std::cout << "Lives of player 2: " << lives[1] << " (" << lives.size() << " players)\n"; return 0;}Output · c++ -std=c++17 main.cpp -o main && ./main
Players: 3 First: Ana, last: Marta Ana: 120 points Luis: 110 points Marta: 140 points Total: 500, average: 125 Fourth player: Pablo Lives of player 2: 3 (4 players)
Key points
std::vector<type>is a list that can grow.v[i]reads or changes elementi, counting from 0.size()gives the number of elements andback()the last one.push_back(x)adds at the end.at(i)checks that the position exists.
Exercise
Create a vector with the highest temperatures from Monday to Saturday, add Sunday's with push_back, and print the week's average and how many days went above 25 degrees.