In some instances, we will want to loop through a vector using indices.
The indices of a vector meals are all the integer values from 0 to meals.size() - 1, so we can iterate over them using an index variable i:
vector<string> meals = {"pancakes", "pasta", "pizza", "avocado"};
for (int i = 0; i < meals.size(); i++) {
cout << "Meal number " << (i + 1) << " is " << meals[i] << ".\n";
}
The output of this code is:
Meal number 1 is pancakes.
Meal number 2 is pasta.
Meal number 3 is pizza.
Meal number 4 is avocado.
Assignment
Now let's print the temperature on each day, in this format:
On day 1 the temperature is 10.
On day 2 the temperature is 60.
...
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
