Zum Inhalt springen

C++-Referenz/ Standardbibliothek/ Algorithmen

Aus Wikibooks
Alte Seite

Die Arbeit am Buch »C++-Referenz« wurde vom Hauptautor eingestellt. Ein Lehrbuch zum Thema C++ ist unter »C++-Programmierung« zu finden. Eine sehr umfangreiche und gute Referenz gibt es unter cppreference.com.

Diese Seite beschreibt C++98, einen stark veralteten Standard. Aktuelle Referenz.

Nicht-Modifizierende sequenzoperationen

[Bearbeiten]

for_each

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename Function>
Function for_each(InputIterator first, InputIterator last, Function f);
// Header: algorithm
template<typename InputIterator, typename T>
InputIterator find(InputIterator first, InputIterator last, const T& value);

find_if

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred);

find_end

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator1, typename ForwardIterator2>
ForwardIterator1 find_end(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2
);

template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
ForwardIterator1 find_end(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2,
    BinaryPredicate pred
);

find_first_of

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator1, typename ForwardIterator2>
ForwardIterator1 find_first_of(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2
);

template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
ForwardIterator1 find_first_of(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2,
    BinaryPredicate pred
);

adjacent_find

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);

template<typename ForwardIterator, typename BinaryPredicate>
ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);

count

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename T>
iterator_traits<InputIterator>::difference_type count(
    InputIterator first, InputIterator last, const T& value
);

count_if

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename Predicate>
iterator_traits<InputIterator>::difference_type count_if(
    InputIterator first, InputIterator last, Predicate pred
);

mismatch

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2>
pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 first1, InputIterator1 last1, InputIterator2 first2
);

template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred
);

equal

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);

template<typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator1, typename ForwardIterator2>
ForwardIterator1 search(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2
);

template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
ForwardIterator1 search(
    ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2,
    BinaryPredicate pred
);

search_n

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename Size, typename T>
ForwardIterator search_n(
    ForwardIterator first, ForwardIterator last, Size count, const T& value
);

template<typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>
ForwardIterator search_n(
    ForwardIterator first, ForwardIterator last, Size count, const T& value, BinaryPredicate pred
);

Modifizierende sequenzoperationen

[Bearbeiten]

Kopieren

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);

copy_backward

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
BidirectionalIterator2 copy_backward(
    BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result
);

Vertauschen

[Bearbeiten]
// Header: algorithm
template<typename T>
void swap(T& a, T& b);

swap_ranges

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator1, typename ForwardIterator2>
ForwardIterator2 swap_ranges(
    ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2
);

iter_swap

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator1, typename ForwardIterator2>
void iter_swap(ForwardIterator1 a, ForwardIterator2 b);

transform

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator, typename UnaryOperation>
OutputIterator transform(
    InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op
);

transform

[Bearbeiten]
// Header: algorithm
template<
    typename InputIterator1, typename InputIterator2,
    typename OutputIterator,
    typename BinaryOperation
>
OutputIterator transform(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2,
    OutputIterator result,
    BinaryOperation binary_op
);

replace

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);

replace_if

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename Predicate, typename T>
void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);

replace_copy

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator, typename T>
OutputIterator replace_copy(
    InputIterator first, InputIterator last,
    OutputIterator result,
    const T& old_value, const T& new_value
);

replace_copy_if

[Bearbeiten]
// Header: algorithm
template<typename Iterator, typename OutputIterator, typename Predicate, typename T>
OutputIterator replace_copy_if(
    Iterator first, Iterator last,
    OutputIterator result,
    Predicate pred,
    const T& new_value
);
// Header: algorithm
template<typename ForwardIterator, typename T>
void fill(ForwardIterator first, ForwardIterator last, const T& value);

fill_n

[Bearbeiten]
// Header: algorithm
template<typename OutputIterator, typename Size, typename T>
void fill_n(OutputIterator first, Size n, const T& value);

generate

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename Generator>
void generate(ForwardIterator first, ForwardIterator last, Generator gen);

generate_n

[Bearbeiten]
// Header: algorithm
template<typename OutputIterator, typename Size, typename Generator>
void generate_n(OutputIterator first, Size n, Generator gen);

remove

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value);

remove_if

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename Predicate>
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);

remove_copy

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator, typename T>
OutputIterator remove_copy(
    InputIterator first, InputIterator last, OutputIterator result, const T& value
);

remove_copy_if

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator remove_copy_if(
    InputIterator first, InputIterator last, OutputIterator result, Predicate pred
);

unique

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last);

template<typename ForwardIterator, typename BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);

unique_copy

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename OutputIterator>
OutputIterator unique_copy(
    InputIterator first, InputIterator last, OutputIterator result
);

template<typename InputIterator, typename OutputIterator, typename BinaryPredicate>
OutputIterator unique_copy(
    InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred
);

reverse

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last);

reverse_copy

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator, typename OutputIterator>
OutputIterator reverse_copy(
    BidirectionalIterator first, BidirectionalIterator last, OutputIterator result
);

rotate

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator>
void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);

rotate_copy

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename OutputIterator>
OutputIterator rotate_copy(
    ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result
);

random_shuffle

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void random_shuffle(
    RandomAccessIterator first, RandomAccessIterator last
);

template<typename RandomAccessIterator, typename RandomNumberGenerator>
void random_shuffle(
    RandomAccessIterator first, RandomAccessIterator last,
    RandomNumberGenerator& rand
);

Verteilen

[Bearbeiten]

partition

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator, typename Predicate>
BidirectionalIterator partition(
    BidirectionalIterator first, BidirectionalIterator last, Predicate pred
);

stable_partition

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator, typename Predicate>
BidirectionalIterator stable_partition(
    BidirectionalIterator first, BidirectionalIterator last, Predicate pred
);

Sortier- und Zuordnungsoperationen

[Bearbeiten]

Sortieren

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

stable_sort

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

partial_sort

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void partial_sort(
    RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last
);

template<typename RandomAccessIterator, typename Compare>
void partial_sort(
    RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last,
    Compare comp
);

partial_sort_copy

[Bearbeiten]
// Header: algorithm
template<typename InputIterator, typename RandomAccessIterator>
RandomAccessIterator partial_sort_copy(
    InputIterator first, InputIterator last,
    RandomAccessIterator result_first, RandomAccessIterator result_last
);

template<typename InputIterator, typename RandomAccessIterator, typename Compare>
RandomAccessIterator partial_sort_copy(
    InputIterator first, InputIterator last,
    RandomAccessIterator result_first, RandomAccessIterator result_last,
    Compare comp
);

nth_element

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void nth_element(
    RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last
);

template<typename RandomAccessIterator, typename Compare>
void nth_element(
    RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp
);

Binäre Suche

[Bearbeiten]

lower_bound

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
ForwardIterator lower_bound(
    ForwardIterator first, ForwardIterator last, const T& value
);

template<typename ForwardIterator, typename T, typename Compare>
ForwardIterator lower_bound(
    ForwardIterator first, ForwardIterator last, const T& value, Compare comp
);

upper_bound

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
ForwardIterator upper_bound(
    ForwardIterator first, ForwardIterator last, const T& value
);

template<typename ForwardIterator, typename T, typename Compare>
ForwardIterator upper_bound(
    ForwardIterator first, ForwardIterator last, const T& value, Compare comp
);

equal_range

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
pair<ForwardIterator, ForwardIterator> equal_range(
    ForwardIterator first, ForwardIterator last, const T& value
);

template<typename ForwardIterator, typename T, typename Compare>
pair<ForwardIterator, ForwardIterator> equal_range(
    ForwardIterator first, ForwardIterator last, const T& value, Compare comp
);
[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator, typename T>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& value);

template<typename ForwardIterator, typename T, typename Compare>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);

Verbinden

[Bearbeiten]

merge

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator merge(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result
);

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Compare>
OutputIterator merge(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result,
    Compare comp
);

inplace_merge

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator>
void inplace_merge(
    BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last
);

template<typename BidirectionalIterator, typename Compare>
void inplace_merge(
    BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last,
    Compare comp
);

Setzen

[Bearbeiten]

includes

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2>
bool includes(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2
);

template<typename InputIterator1, typename InputIterator2, typename Compare>
bool includes(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    Compare comp
);

set_union

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_union(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result
);

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Compare>
OutputIterator set_union(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result,
    Compare comp
);

set_intersection

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_intersection(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result
);

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Compare>
OutputIterator set_intersection(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result,
    Compare comp
);

set_difference

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_difference(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result
);

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Compare>
OutputIterator set_difference(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result,
    Compare comp
);

set_symmetric_difference

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2, typename OutputIterator>
OutputIterator set_symmetric_difference(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result
);

template<typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Compare>
OutputIterator set_symmetric_difference(
    InputIterator1 first1, InputIterator1 last1,
    InputIterator2 first2, InputIterator2 last2,
    OutputIterator result,
    Compare comp
);

heap-Operationen

[Bearbeiten]

push_heap

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void push_heap(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

pop_heap

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

make_heap

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

sort_heap

[Bearbeiten]
// Header: algorithm
template<typename RandomAccessIterator>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last);

template<typename RandomAccessIterator, typename Compare>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Minimum und Maximum

[Bearbeiten]
// Header: algorithm
template<typename T>
const T& min(const T& a, const T& b);

template<typename T, typename Compare>
const T& min(const T& a, const T& b, Compare comp);
// Header: algorithm
template<typename T>
const T& max(const T& a, const T& b);

template<typename T, typename Compare>
const T& max(const T& a, const T& b, Compare comp);

min_element

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator>
ForwardIterator min_element(ForwardIterator first,  ForwardIterator last);

template<typename ForwardIterator, typename Compare>
ForwardIterator min_element(ForwardIterator first,  ForwardIterator last, Compare comp);

max_element

[Bearbeiten]
// Header: algorithm
template<typename ForwardIterator>
ForwardIterator max_element(ForwardIterator first,  ForwardIterator last);

template<typename ForwardIterator, typename Compare>
ForwardIterator max_element(ForwardIterator first,  ForwardIterator last, Compare comp);

lexicographical_compare

[Bearbeiten]
// Header: algorithm
template<typename InputIterator1, typename InputIterator2>
bool lexicographical_compare(
    InputIterator1 first1, InputIterator1  last1,
    InputIterator2 first2, InputIterator2  last2
);

template<typename InputIterator1, typename InputIterator2, typename Compare>
bool lexicographical_compare(
    InputIterator1 first1, InputIterator1  last1,
    InputIterator2 first2, InputIterator2  last2,
    Compare comp
);

Permutationen

[Bearbeiten]

next_permutation

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator>
bool next_permutation(BidirectionalIterator first, BidirectionalIterator last);

template<typename BidirectionalIterator, typename Compare>
bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);

prev_permutation

[Bearbeiten]
// Header: algorithm
template<typename BidirectionalIterator>
bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last);

template<typename BidirectionalIterator, typename Compare>
bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);