Consider a scenario where you have a std::vector<int> named numbers containing the values [10,
20, 30, 40, 50].
You need to use an iterator to find and print the sum of all the elements in the vector.
Which code snippet accomplishes this task correctly?
a.
vector<int> numbers = { 10,20,30,40,50 };
int sum = 0;
for (int num : numbers) {
sum += num;
}
b. vector<int> numbers = { 10,20,30,40,50 };
int sum = 0;
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
sum += *it;
}
c. vector<int> numbers = { 10,20,30,40,50 };
int sum = 0;
for (unsigned int i = 0; i < numbers.size(); ++i) {
sum += numbers[i];
}
d. vector<int> numbers = { 10,20,30,40,50 };
int sum = 0;
for (auto& num : numbers) {
sum += num;
}