Skip to content
Justice Tech School

Algorithms

<algorithm> has sort, find, count, reverse, min_element, max_element, accumulate and more.

Example
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main() {
  std::vector<int> v = {5, 2, 9, 1};
  std::sort(v.begin(), v.end());
  std::cout << v.front() << " " << v.back() << " " << std::count(v.begin(), v.end(), 2) << " ";
  std::cout << std::accumulate(v.begin(), v.end(), 0) << "\n";
  return 0;
}

Result

1 9 1 17

Exercise

Sort.

std::(v.begin(), v.end())

Quiz Test

Which header has accumulate?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.