Q:

Use accumulate to sum the elements in a vector<int>

0

Use accumulate to sum the elements in a vector<int>.

All Answers

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

#include <numeric>
#include <vector>
#include <iostream>

int main() {
  std::vector<int> vi;
  for (int i; std::cin >> i; vi.push_back(i)) {}
  std::cout << "The sum of the sequence is "
            << std::accumulate(vi.cbegin(), vi.cend(), 0) << 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