<algorithm> The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements using iterators. A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms operate through iterators directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container). A range is [first, last) – include first, do not include last. Non-modifying sequence operations: count count number of times a value is found in a range int count (InputIterator first, InputIterator last, const T& val); int myCount = count (myints, myints+8, 10); //count number of 10’s in the range count_if Returns the number of elements in range satisfying a condition int count (InputIterator first, InputIterator last, UnaryPredicate pred); //count values for which isOdd returns true int myCount = count_if (vec.begin(), vec.end(), IsOdd); find Find a value in range InputIterator find (InputIterator first, InputIterator last, const T& val); it = find (myDeque.begin(), myDeque.end(), 30); for_each Apply a function to every element in a range Function for_each (InputIterator first, InputIterator last, Function fn); for_each (myList.begin(), myList.end(), square); Modifying sequence operations: copy Copy a range of elements to destination OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); copy (myints, myints+10, myvector.begin()); copy (myints, myints+10, ostream_iterator<int>(cout, “ “)); generate Generate values for a range of elements using a function void generate (ForwardIterator first, ForwardIterator last, Generator gen); generate (myVector.begin(), myVector.end(), UniqueNumber); random_shuffle Randomly rearrange elements in a range using a built-in random number generator void random_shuffle (raIterator first, raIterator last); random_shuffle (myVector.begin(), myVector.end()); Sorting: binary_search Test to determine if a value exists in a sorted sequence bool binary_search (ForwardIterator first, ForwardIterator last, const T& val); if (binary_search (v.begin(), v.end(), 3)) cout << “value was found” << endl; sort Sort elements in a range void sort (RandomAccessIterator first, RandomAccessIterator last); sort (myDeque.begin(), myDeque.end()); Min/Max: min_element Return the smallest element in a range ForwardIterator min_element (ForwardIterator first, ForwardIterator last); cout << "The smallest element is " << *min_element(myints, myints+7) << endl; max_element Return the largest element in a range ForwardIterator max_element (ForwardIterator first, ForwardIterator last); cout << "The largest element is " << *max_element(myints, myints+7) << endl; Numeric: requires #include <numeric> Accumulate Accumulate values in a range T accumulate ( InputIterator first, InputIterator last, T iniitialValue ); cout << accumulate(numbers, numbers+3, 0); //initial value of accumulation is 0