Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::accumulate example 1: summing a std::vector of integers

 

std::accumulate example 1: summing a std::vector of integers is a std::accumulate example to sum a std::vector of integers.

 

 

 

 

 

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppAccumulateExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <cassert>
#include <vector>
#include <numeric>

int main()
{
  //Create a std::vector
  std::vector<int> v;
  for (int i=0; i!=10; ++i) { v.push_back(i); }

  //Sum the std::vector
  const int sum = std::accumulate(v.begin(), v.end(), 0 ); // '0' is the initial value

  //Assume std::accumulate works correctly
  assert(sum == 45);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml