Help First three multiples c++

I just don’t understand first three multiples can someone help me understand.

Here is the starting code

#include <iostream>
#include <vector>

// Define first_three_multiples() here:


int main() {
  
  for (int element : first_three_multiples(8)) {
    std::cout << element << "\n";
  }
  
}
`

Here is the answer

`#include <iostream>
#include <vector>

// Define first_three_multiples() here:
std::vector<int> first_three_multiples(int num) {
  
  std::vector<int> multiples{num, num * 2, num * 3};
  
  return multiples;
  
}

int main() {
  
  for (int element : first_three_multiples(8)) {
    std::cout << element << "\n";
  }
  
}

The function firt_three_multiples takes a number, it then creates a vector of three elements, the number it was given, that number multiplied by two, and that number multiplied by three. Those three numbers now exist in a container(in our case a vector). The function hands that container to whatever called it.

In main you call the function inside the for loop.
THe for loop basically says, call the function first_three_multiples on the number 8, to give me a container of the and print out each element in that container.

2 Likes