previous | start | next

## Standard Algorithms

- Why iterators?
- It is possible to supply generic functions that can carry out a task with the elements in any container that uses iterators.
- Example: The accumulate function can compute the sum of all elements in a vector or a list.

  >     vector<double> data;
  >     /* do something with data */

  >     double vsum = 0;
  >     accumulate(data.begin, data.end(), vsum);
  >     /* now vsum contains the sum of the elements in the vector */
  >     list<double> salaries;
  >     /* do something with salaries */

  >     double lsum = 0;
  >     accumulate(salaries.begin(), salaries.end(), lsum);
  >     /* now lsum contains the sum of the elements in the list */

previous | start | next