Q:

Write a function that takes an initializer_list<int> and produces the sum of the elements in the list

0

Write a function that takes an initializer_list<int> and produces the sum of the elements in the list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <initializer_list>
#include <iostream>

int sumIntList(std::initializer_list<int> il) {
  int sum = 0;
  // `il.begin()` or `il.end()` will return `const int *`
  for (auto &e : il)  // `e` is `const int &`, since element in `il` are `const int`
    sum += e;
  return sum;
}

int main() {
  std::cout << sumIntList({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now