Codeloft
0 0 XP

Vectors

8 min 20 XP + 10 for no hints

A vector is a list that can grow. Add #include <vector>, then create one with its item type in angle brackets: std::vector<int> scores = {8, 6, 9};

scores.push_back(7); adds an item to the end, and scores.size() tells you how many items it holds.

A range-based for loop visits every item in turn: for (int s : scores) { … }

Example (inside main)
std::vector<std::string> pets = {"cat", "dog"};
pets.push_back("fish");
for (std::string p : pets) {
    std::cout << p << " ";
}
Output
cat dog fish
Vectors count from 0 like JavaScript arrays: scores[0] is the first item.

Your tasks

0 of 4 done
  1. Include the <vector> header
  2. Create std::vector<int> marks with 3 numbers
  3. Add a mark with marks.push_back
  4. Loop over marks and print each one

Quick check

What does marks.size() return?

marks.cppChecked as you type
No problems found. Unclosed tags, missing semicolons and errors show up here in plain words.