STL Algorithms

#include <algorithm>

All algorithms can be find on the reference.

Sort

Sort vectors with STL std:sort function.

Modifies vector, arranging elements in ascending order according to the < relation.

Specify a region to sort by feeding in an iterator to start and end.

vector<float> grades = {...};
sort(grades.begin(), grades.end()); // sorts everything in grades

Find

Use to search for a particular element in a STL container.

int arr[] = (1, 20, -2, 4);
int *p;

p = std::find (arr, arr+4, 30);

If it doesn't find the value, it returns a pointer pointing to one past the last search element.

Using find() with vector:

vector<int> vec (arr, arr+4);
vecotr<int>::iterator it;
it = std::find(vec.begin(), vec.end(), -2);

if (it!=vec.end())
	cout << "value found at " << *it << '\n';

Count

See how many times a particular vector appears in a STL container.

int arr[] = {10, 20, 30, 30, 20, 10, 10, 20}; // 8 elements
int mycount = std::count (arr, arr+8, 10); // 

is_permutation

See if you have the same set of values in two containers:

#include <iostrream>	// std::count
#include <algorithm> 	// std::is_permutation
#include <array>	// std::array

int main () {
	std::array<int,5> foo = {1, 2, 3, 4, 5};
	std::array<int,5> bar = {1, 2, 3, 4, 5};
	
	if (std::is_permutation(foo.begin(), foo.end(), bar.begin(0)))
		std::cout << "foo and bar contain the same elements.\n";
	
	return 0;
}