C++/STL Algorithms

From Wikiversity

< C++
Jump to: navigation, search

The STL contains functions for common algorithms within C++.

Contents

[edit] find

Finds a value within a range.


[edit] copy

Copies a range of elements.

copy_backward reverese the order of elements.

[edit] swap

Swaps two elements.

[edit] iter_swap

Swaps to elements pointed to by iterators.

[edit] transform

Applies a function to a range.

[edit] random_shuffle

Randomly shuffles a range.

[edit] sort

Sorts elements in a range.

A stable_sort retains the original order of equal elements. A stable_sort does not provide any advantage over a simple sort of, say, a list of integer values (if the list contains multiple values of "2", it doesn't matter which "2" comes first). However, consider the example of sorting a list of Employee objects by their first name only. Two different employees may share the same first name (which gives them the ordering relative to the rest of the list). If "Bob Smith" was before to "Bob Jones" in the list prior to the sort, then stable_sort would guarantee Smith was before Jones in the sorted list; sort does not make such a guarantee. The stable_sort algorithm is marginally less efficient due to added constraint.

[edit] binary_search

Searches a sorted range for a value.

[edit] next_permutation

Transforms a range to the next permutation.

Reversed by prev_permutation.

[edit] Lessons

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science