(C++) Algorithm
March 10, 2018 ยท View on GitHub
Algorithm has multiple meanings:
- an algorithm in general
an STL algorithm
the Boost.Algorithm library: see
Boost.Algorithm
Algorithm (general)
An algorithm is a logical series of steps followed by a calculation.
Algorithms can be adapted by lambda expressions, adapters, binders and negaters.
Example: sorting a std::vector
#include <algorithm>
#include <cassert>
#include <vector>
int main()
{
std::vector<int> v = {4, 2, 3, 1};
std::sort(std::begin(v), std::end(v));
const std::vector<int> expected = {1, 2, 3, 4};
assert(v == expected);
}
Example: copy twice
#include <algorithm>
#include <cassert>
#include <vector>
int main()
{
const std::vector<int> v = {1,2};
std::vector<int> w;
std::copy(std::begin(v), std::end(v),
std::back_inserter(w)
);
std::copy(std::begin(v), std::end(v),
std::back_inserter(w)
);
const std::vector<int> expected = {1, 2, 1, 2};
assert(w == expected);
}
Example: copy if non-zero and positive
#include <algorithm>
#include <cassert>
#include <vector>
int main()
{
const std::vector<int> v = {-9, -4, 0, 4, 9};
std::vector<int> w;
std::copy_if(
std::begin(v),
std::end(v),
std::back_inserter(w),
[](const int i) { return i > 0; }
);
const std::vector<int> expected = { 4, 9 };
assert(w == expected);
}
Example: CoutVector
#include <iterator>
#include <iostream>
#include <ostream>
#include <vector>
int main()
{
//Create a std::vector
const std::vector<int> v = {1, 4, 9, 16, 25};
//Copy it to std::cout
std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout,"\n"));
}
STL algorithms
Most algorithms can be found in the header file algorithm.h.
Non-standard algorithms lack the std prefix.
All C++11 algorithm names [3]:
- std::accumulate
- accumulate_if
- std::adjacent_find
- std::all_of
- std::any_of
- std::binary_search
- std::copy
- std::copy_backward
- copy_if
- std::copy_if
- std::copy_n
- std::count
- std::count_if
- std::equal
- std::equal_range
- std::fill
- std::find
- std::find_end
- std::find_first_of
- std::find_if
- std::find_if_not
- std::for_each
- std::generate
- std::generate_n
- std::includes
- std::inner_product
- std::inplace_merge
- std::iota
- std::is_heap
- std::is_heap_until
- std::is_sorted
- std::is_sorted_until
- std::iter_swap
- std::lexicographical_compare
- std::lower_bound
- std::make_heap
- std::max
- std::max_element
- std::merge
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::mismatch
- std::move
- std::move_backward
- std::next_permutation
- std::none_of
- std::nth_element
- std::partial_sort
- std::partial_sort_copy
- std::partition
- std::partition_copy
- std::partition_point
- std::pop_heap
- std::prev_permutation
- std::push_heap
- std::random_shuffle
- std::remove
- std::remove_copy
- std::remove_copy_if
- std::remove_if
- std::replace
- std::replace_copy
- std::replace_copy_if
- std::replace_if
- std::reverse
- std::reverse_copy
- std::rotate
- std::rotate_copy
- std::search
- std::search_n
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::sort
- std::sort_heap
- std::stable_partition
- std::stable_sort
- std::swap
- std::swap_ranges
- std::transform
- std::unique
- std::unique_copy
- std::upper_bound
Advice
- Prefer algorithm calls over hand-written loops [1,2]
- Use templates to express algorithms that apply to many argument types [4]
External links
References
- [1] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops'.
- [2] Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'
- [3] Bjarne Stroustrup's C++0x FAQ page
- [4] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[1] Use templates to express algorithms that apply to many argument types'