libstdc++
Sorting
Collaboration diagram for Sorting:

Modules

Functions


Function Documentation

template<typename _BidirectionalIterator >
void std::inplace_merge ( _BidirectionalIterator  __first,
_BidirectionalIterator  __middle,
_BidirectionalIterator  __last 
)

Merges two sorted ranges in place.

Parameters:
firstAn iterator.
middleAnother iterator.
lastAnother iterator.
Returns:
Nothing.

Merges two sorted and consecutive ranges, [first,middle) and [middle,last), and puts the result in [first,last). The output will be sorted. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.

If enough additional memory is available, this takes (last-first)-1 comparisons. Otherwise an NlogN algorithm is used, where N is distance(first,last).

Definition at line 3122 of file stl_algo.h.

References std::distance(), std::_Temporary_buffer::begin(), std::__merge_without_buffer(), std::__merge_adaptive(), and std::_Temporary_buffer::size().

template<typename _BidirectionalIterator , typename _Compare >
void std::inplace_merge ( _BidirectionalIterator  __first,
_BidirectionalIterator  __middle,
_BidirectionalIterator  __last,
_Compare  __comp 
)

Merges two sorted ranges in place.

Parameters:
firstAn iterator.
middleAnother iterator.
lastAnother iterator.
compA functor to use for comparisons.
Returns:
Nothing.

Merges two sorted and consecutive ranges, [first,middle) and [middle,last), and puts the result in [first,last). The output will be sorted. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.

If enough additional memory is available, this takes (last-first)-1 comparisons. Otherwise an NlogN algorithm is used, where N is distance(first,last).

The comparison function should have the same effects on ordering as the function used for the initial sort.

Definition at line 3177 of file stl_algo.h.

References std::distance(), std::_Temporary_buffer::begin(), std::__merge_without_buffer(), std::__merge_adaptive(), and std::_Temporary_buffer::size().

template<typename _ForwardIterator >
bool std::is_sorted ( _ForwardIterator  __first,
_ForwardIterator  __last 
) [inline]

Determines whether the elements of a sequence are sorted.

Parameters:
firstAn iterator.
lastAnother iterator.
Returns:
True if the elements are sorted, false otherwise.

Definition at line 3917 of file stl_algo.h.

References std::is_sorted_until().

template<typename _ForwardIterator , typename _Compare >
bool std::is_sorted ( _ForwardIterator  __first,
_ForwardIterator  __last,
_Compare  __comp 
) [inline]

Determines whether the elements of a sequence are sorted according to a comparison functor.

Parameters:
firstAn iterator.
lastAnother iterator.
compA comparison functor.
Returns:
True if the elements are sorted, false otherwise.

Definition at line 3931 of file stl_algo.h.

References std::is_sorted_until().

template<typename _ForwardIterator >
_ForwardIterator std::is_sorted_until ( _ForwardIterator  __first,
_ForwardIterator  __last 
)

Determines the end of a sorted sequence.

Parameters:
firstAn iterator.
lastAnother iterator.
Returns:
An iterator pointing to the last iterator i in [first, last) for which the range [first, i) is sorted.

Definition at line 3945 of file stl_algo.h.

template<typename _ForwardIterator , typename _Compare >
_ForwardIterator std::is_sorted_until ( _ForwardIterator  __first,
_ForwardIterator  __last,
_Compare  __comp 
)

Determines the end of a sorted sequence using comparison functor.

Parameters:
firstAn iterator.
lastAnother iterator.
compA comparison functor.
Returns:
An iterator pointing to the last iterator i in [first, last) for which the range [first, i) is sorted.

Definition at line 3974 of file stl_algo.h.

Referenced by std::is_sorted().

template<typename _II1 , typename _II2 >
bool std::lexicographical_compare ( _II1  __first1,
_II1  __last1,
_II2  __first2,
_II2  __last2 
) [inline]

Performs dictionary comparison on ranges.

Parameters:
first1An input iterator.
last1An input iterator.
first2An input iterator.
last2An input iterator.
Returns:
A boolean true or false.

Returns true if the sequence of elements defined by the range [first1,last1) is lexicographically less than the sequence of elements defined by the range [first2,last2). Returns false otherwise. (Quoted from [25.3.8]/1.) If the iterators are all character pointers, then this is an inline call to memcmp.

Definition at line 1073 of file stl_algobase.h.

template<typename _II1 , typename _II2 , typename _Compare >
bool std::lexicographical_compare ( _II1  __first1,
_II1  __last1,
_II2  __first2,
_II2  __last2,
_Compare  __comp 
)

Performs dictionary comparison on ranges.

Parameters:
first1An input iterator.
last1An input iterator.
first2An input iterator.
last2An input iterator.
compA comparison functor.
Returns:
A boolean true or false.

The same as the four-parameter lexicographical_compare, but uses the comp parameter instead of <.

Definition at line 1107 of file stl_algobase.h.

Referenced by std::operator<().

template<typename _Tp >
const _Tp & std::max ( const _Tp &  __a,
const _Tp &  __b 
) [inline]

This does what you think it does.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
Returns:
The greater of the parameters.

This is the simple classic generic implementation. It will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

Definition at line 210 of file stl_algobase.h.

Referenced by std::deque::_M_reallocate_map(), std::basic_stringbuf::overflow(), and std::_Deque_base::_M_initialize_map().

template<typename _Tp , typename _Compare >
const _Tp & std::max ( const _Tp &  __a,
const _Tp &  __b,
_Compare  __comp 
) [inline]

This does what you think it does.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
compA comparison functor.
Returns:
The greater of the parameters.

This will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

Definition at line 254 of file stl_algobase.h.

template<typename _ForwardIterator >
_ForwardIterator std::max_element ( _ForwardIterator  __first,
_ForwardIterator  __last 
)

Return the maximum element in a range.

Parameters:
firstStart of range.
lastEnd of range.
Returns:
Iterator referencing the first instance of the largest value.

Definition at line 6229 of file stl_algo.h.

template<typename _ForwardIterator , typename _Compare >
_ForwardIterator std::max_element ( _ForwardIterator  __first,
_ForwardIterator  __last,
_Compare  __comp 
)

Return the maximum element in a range using comparison functor.

Parameters:
firstStart of range.
lastEnd of range.
compComparison functor.
Returns:
Iterator referencing the first instance of the largest value according to comp.

Definition at line 6257 of file stl_algo.h.

Referenced by std::valarray::max().

template<typename _InputIterator1 , typename _InputIterator2 , typename _OutputIterator >
_OutputIterator std::merge ( _InputIterator1  __first1,
_InputIterator1  __last1,
_InputIterator2  __first2,
_InputIterator2  __last2,
_OutputIterator  __result 
)

Merges two sorted ranges.

Parameters:
first1An iterator.
first2Another iterator.
last1Another iterator.
last2Another iterator.
resultAn iterator pointing to the end of the merged range.
Returns:
An iterator pointing to the first element not less than val.

Merges the ranges [first1,last1) and [first2,last2) into the sorted range [result, result + (last1-first1) + (last2-first2)). Both input ranges must be sorted, and the output range must not overlap with either of the input ranges. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.

Definition at line 5472 of file stl_algo.h.

template<typename _InputIterator1 , typename _InputIterator2 , typename _OutputIterator , typename _Compare >
_OutputIterator std::merge ( _InputIterator1  __first1,
_InputIterator1  __last1,
_InputIterator2  __first2,
_InputIterator2  __last2,
_OutputIterator  __result,
_Compare  __comp 
)

Merges two sorted ranges.

Parameters:
first1An iterator.
first2Another iterator.
last1Another iterator.
last2Another iterator.
resultAn iterator pointing to the end of the merged range.
compA functor to use for comparisons.
Returns:
An iterator pointing to the first element "not less than" val.

Merges the ranges [first1,last1) and [first2,last2) into the sorted range [result, result + (last1-first1) + (last2-first2)). Both input ranges must be sorted, and the output range must not overlap with either of the input ranges. The sort is stable, that is, for equivalent elements in the two ranges, elements from the first range will always come before elements from the second.

The comparison function should have the same effects on ordering as the function used for the initial sort.

Definition at line 5535 of file stl_algo.h.

template<typename _Tp >
const _Tp & std::min ( const _Tp &  __a,
const _Tp &  __b 
) [inline]

This does what you think it does.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
Returns:
The lesser of the parameters.

This is the simple classic generic implementation. It will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

Definition at line 187 of file stl_algobase.h.

Referenced by __gnu_cxx::random_sample_n(), std::basic_string< char >::compare(), std::basic_string::rfind(), std::basic_string::compare(), std::basic_filebuf::underflow(), std::basic_filebuf::xsputn(), std::basic_istream::readsome(), __gnu_profile::__report(), std::generate_canonical(), std::basic_stringbuf::overflow(), std::basic_streambuf::xsgetn(), std::basic_streambuf::xsputn(), __gnu_cxx::__versa_string::compare(), and __gnu_cxx::__versa_string::rfind().

template<typename _Tp , typename _Compare >
const _Tp & std::min ( const _Tp &  __a,
const _Tp &  __b,
_Compare  __comp 
) [inline]

This does what you think it does.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
compA comparison functor.
Returns:
The lesser of the parameters.

This will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

Definition at line 233 of file stl_algobase.h.

template<typename _ForwardIterator >
_ForwardIterator std::min_element ( _ForwardIterator  __first,
_ForwardIterator  __last 
)

Return the minimum element in a range.

Parameters:
firstStart of range.
lastEnd of range.
Returns:
Iterator referencing the first instance of the smallest value.

Definition at line 6173 of file stl_algo.h.

template<typename _ForwardIterator , typename _Compare >
_ForwardIterator std::min_element ( _ForwardIterator  __first,
_ForwardIterator  __last,
_Compare  __comp 
)

Return the minimum element in a range using comparison functor.

Parameters:
firstStart of range.
lastEnd of range.
compComparison functor.
Returns:
Iterator referencing the first instance of the smallest value according to comp.

Definition at line 6201 of file stl_algo.h.

Referenced by std::valarray::min().

template<typename _Tp >
pair< const _Tp &, const _Tp & > std::minmax ( const _Tp &  __a,
const _Tp &  __b 
) [inline]

Determines min and max at once as an ordered pair.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
Returns:
A pair(b, a) if b is smaller than a, pair(a, b) otherwise.

Definition at line 4003 of file stl_algo.h.

template<typename _Tp , typename _Compare >
pair< const _Tp &, const _Tp & > std::minmax ( const _Tp &  __a,
const _Tp &  __b,
_Compare  __comp 
) [inline]

Determines min and max at once as an ordered pair.

Parameters:
aA thing of arbitrary type.
bAnother thing of arbitrary type.
compA comparison functor.
Returns:
A pair(b, a) if b is smaller than a, pair(a, b) otherwise.

Definition at line 4022 of file stl_algo.h.

template<typename _ForwardIterator >
pair<_ForwardIterator, _ForwardIterator> std::minmax_element ( _ForwardIterator  __first,
_ForwardIterator  __last 
)

Return a pair of iterators pointing to the minimum and maximum elements in a range.

Parameters:
firstStart of range.
lastEnd of range.
Returns:
make_pair(m, M), where m is the first iterator i in [first, last) such that no other element in the range is smaller, and where M is the last iterator i in [first, last) such that no other element in the range is larger.

Definition at line 4041 of file stl_algo.h.

References std::make_pair().

template<typename _ForwardIterator , typename _Compare >
pair<_ForwardIterator, _ForwardIterator> std::minmax_element ( _ForwardIterator  __first,
_ForwardIterator  __last,
_Compare  __comp 
)

Return a pair of iterators pointing to the minimum and maximum elements in a range.

Parameters:
firstStart of range.
lastEnd of range.
compComparison functor.
Returns:
make_pair(m, M), where m is the first iterator i in [first, last) such that no other element in the range is smaller, and where M is the last iterator i in [first, last) such that no other element in the range is larger.

Definition at line 4117 of file stl_algo.h.

References std::make_pair().

template<typename _BidirectionalIterator >
bool std::next_permutation ( _BidirectionalIterator  __first,
_BidirectionalIterator  __last 
)

Permute range into the next dictionary ordering.

Parameters:
firstStart of range.
lastEnd of range.
Returns:
False if wrapped to first permutation, true otherwise.

Treats all permutations of the range as a set of dictionary sorted sequences. Permutes the current sequence into the next one of this set. Returns true if there are more sequences to generate. If the sequence is the largest of the set, the smallest is generated and false returned.

Definition at line 3619 of file stl_algo.h.

References std::iter_swap(), and std::reverse().

template<typename _BidirectionalIterator , typename _Compare >
bool std::next_permutation ( _BidirectionalIterator  __first,
_BidirectionalIterator  __last,
_Compare  __comp 
)

Permute range into the next dictionary ordering using comparison functor.

Parameters:
firstStart of range.
lastEnd of range.
compA comparison functor.
Returns:
False if wrapped to first permutation, true otherwise.

Treats all permutations of the range [first,last) as a set of dictionary sorted sequences ordered by comp. Permutes the current sequence into the next one of this set. Returns true if there are more sequences to generate. If the sequence is the largest of the set, the smallest is generated and false returned.

Definition at line 3676 of file stl_algo.h.

References std::iter_swap(), and std::reverse().

template<typename _RandomAccessIterator >
void std::nth_element ( _RandomAccessIterator  __first,
_RandomAccessIterator  __nth,
_RandomAccessIterator  __last 
) [inline]

Sort a sequence just enough to find a particular position.

Parameters:
firstAn iterator.
nthAnother iterator.
lastAnother iterator.
Returns:
Nothing.

Rearranges the elements in the range [first,last) so that *nth is the same element that would have been in that position had the whole sequence been sorted. whole sequence been sorted. The elements either side of *nth are not completely sorted, but for any iterator in the range [first,nth) and any iterator in the range [nth,last) it holds that *j<*i is false.

Definition at line 5317 of file stl_algo.h.

References std::__lg().

template<typename _RandomAccessIterator , typename _Compare >
void std::nth_element ( _RandomAccessIterator  __first,
_RandomAccessIterator  __nth,
_RandomAccessIterator  __last,
_Compare  __comp 
) [inline]

Sort a sequence just enough to find a particular position using a predicate for comparison.

Parameters:
firstAn iterator.
nthAnother iterator.
lastAnother iterator.
compA comparison functor.
Returns:
Nothing.

Rearranges the elements in the range [first,last) so that *nth is the same element that would have been in that position had the whole sequence been sorted. The elements either side of *nth are not completely sorted, but for any iterator in the range [first,nth) and any iterator in the range [nth,last) it holds that comp(*j,*i) is false.

Definition at line 5356 of file stl_algo.h.

References std::__lg().

template<typename _RandomAccessIterator >
void std::partial_sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __middle,
_RandomAccessIterator  __last 
) [inline]

Sort the smallest elements of a sequence.

Parameters:
firstAn iterator.
middleAnother iterator.
lastAnother iterator.
Returns:
Nothing.

Sorts the smallest (middle-first) elements in the range [first,last) and moves them to the range [first,middle). The order of the remaining elements in the range [middle,last) is undefined. After the sort if i and are iterators in the range [first,middle) such that precedes and is an iterator in the range [middle,last) then *j<*i and *k<*i are both false.

Definition at line 5240 of file stl_algo.h.

References std::__heap_select(), and std::sort_heap().

template<typename _RandomAccessIterator , typename _Compare >
void std::partial_sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __middle,
_RandomAccessIterator  __last,
_Compare  __comp 
) [inline]

Sort the smallest elements of a sequence using a predicate for comparison.

Parameters:
firstAn iterator.
middleAnother iterator.
lastAnother iterator.
compA comparison functor.
Returns:
Nothing.

Sorts the smallest (middle-first) elements in the range [first,last) and moves them to the range [first,middle). The order of the remaining elements in the range [middle,last) is undefined. After the sort if i and are iterators in the range [first,middle) such that precedes and is an iterator in the range [middle,last) then *comp(j,*i) and comp(*k,*i) are both false.

Definition at line 5279 of file stl_algo.h.

References std::__heap_select(), and std::sort_heap().

template<typename _InputIterator , typename _RandomAccessIterator >
_RandomAccessIterator std::partial_sort_copy ( _InputIterator  __first,
_InputIterator  __last,
_RandomAccessIterator  __result_first,
_RandomAccessIterator  __result_last 
)

Copy the smallest elements of a sequence.

Parameters:
firstAn iterator.
lastAnother iterator.
result_firstA random-access iterator.
result_lastAnother random-access iterator.
Returns:
An iterator indicating the end of the resulting sequence.

Copies and sorts the smallest N values from the range [first,last) to the range beginning at result_first, where the number of elements to be copied, N, is the smaller of (last-first) and (result_last-result_first). After the sort if i and are iterators in the range [result_first,result_first+N) such that precedes then *j<*i is false. The value returned is result_first+N.

Definition at line 1944 of file stl_algo.h.

References std::make_heap(), and std::sort_heap().

template<typename _InputIterator , typename _RandomAccessIterator , typename _Compare >
_RandomAccessIterator std::partial_sort_copy ( _InputIterator  __first,
_InputIterator  __last,
_RandomAccessIterator  __result_first,
_RandomAccessIterator  __result_last,
_Compare  __comp 
)

Copy the smallest elements of a sequence using a predicate for comparison.

Parameters:
firstAn input iterator.
lastAnother input iterator.
result_firstA random-access iterator.
result_lastAnother random-access iterator.
compA comparison functor.
Returns:
An iterator indicating the end of the resulting sequence.

Copies and sorts the smallest N values from the range [first,last) to the range beginning at result_first, where the number of elements to be copied, N, is the smaller of (last-first) and (result_last-result_first). After the sort if i and are iterators in the range [result_first,result_first+N) such that precedes then comp(*j,*i) is false. The value returned is result_first+N.

Definition at line 2010 of file stl_algo.h.

References std::make_heap(), and std::sort_heap().

template<typename _BidirectionalIterator >
bool std::prev_permutation ( _BidirectionalIterator  __first,
_BidirectionalIterator  __last 
)

Permute range into the previous dictionary ordering.

Parameters:
firstStart of range.
lastEnd of range.
Returns:
False if wrapped to last permutation, true otherwise.

Treats all permutations of the range as a set of dictionary sorted sequences. Permutes the current sequence into the previous one of this set. Returns true if there are more sequences to generate. If the sequence is the smallest of the set, the largest is generated and false returned.

Definition at line 3732 of file stl_algo.h.

References std::iter_swap(), and std::reverse().

template<typename _BidirectionalIterator , typename _Compare >
bool std::prev_permutation ( _BidirectionalIterator  __first,
_BidirectionalIterator  __last,
_Compare  __comp 
)

Permute range into the previous dictionary ordering using comparison functor.

Parameters:
firstStart of range.
lastEnd of range.
compA comparison functor.
Returns:
False if wrapped to last permutation, true otherwise.

Treats all permutations of the range [first,last) as a set of dictionary sorted sequences ordered by comp. Permutes the current sequence into the previous one of this set. Returns true if there are more sequences to generate. If the sequence is the smallest of the set, the largest is generated and false returned.

Definition at line 3789 of file stl_algo.h.

References std::iter_swap(), and std::reverse().

template<typename _RandomAccessIterator >
void std::sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __last 
) [inline]

Sort the elements of a sequence.

Parameters:
firstAn iterator.
lastAnother iterator.
Returns:
Nothing.

Sorts the elements in the range [first,last) in ascending order, such that *(i+1)<*i is false for each iterator i in the range [first,last-1).

The relative ordering of equivalent elements is not preserved, use stable_sort() if this is needed.

Definition at line 5394 of file stl_algo.h.

References std::__introsort_loop(), std::__lg(), and std::__final_insertion_sort().

template<typename _RandomAccessIterator , typename _Compare >
void std::sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __last,
_Compare  __comp 
) [inline]

Sort the elements of a sequence using a predicate for comparison.

Parameters:
firstAn iterator.
lastAnother iterator.
compA comparison functor.
Returns:
Nothing.

Sorts the elements in the range [first,last) in ascending order, such that comp(*(i+1),*i) is false for every iterator i in the range [first,last-1).

The relative ordering of equivalent elements is not preserved, use stable_sort() if this is needed.

Definition at line 5430 of file stl_algo.h.

References std::__introsort_loop(), std::__lg(), and std::__final_insertion_sort().

template<typename _RandomAccessIterator >
void std::stable_sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __last 
) [inline]

Sort the elements of a sequence, preserving the relative order of equivalent elements.

Parameters:
firstAn iterator.
lastAnother iterator.
Returns:
Nothing.

Sorts the elements in the range [first,last) in ascending order, such that *(i+1)<*i is false for each iterator i in the range [first,last-1).

The relative ordering of equivalent elements is preserved, so any two elements x and y in the range [first,last) such that x<y is false and y<x is false will have the same relative ordering after calling stable_sort().

Definition at line 5594 of file stl_algo.h.

References std::_Temporary_buffer::begin(), std::__inplace_stable_sort(), and std::_Temporary_buffer::size().

template<typename _RandomAccessIterator , typename _Compare >
void std::stable_sort ( _RandomAccessIterator  __first,
_RandomAccessIterator  __last,
_Compare  __comp 
) [inline]

Sort the elements of a sequence using a predicate for comparison, preserving the relative order of equivalent elements.

Parameters:
firstAn iterator.
lastAnother iterator.
compA comparison functor.
Returns:
Nothing.

Sorts the elements in the range [first,last) in ascending order, such that comp(*(i+1),*i) is false for each iterator i in the range [first,last-1).

The relative ordering of equivalent elements is preserved, so any two elements x and y in the range [first,last) such that comp(x,y) is false and comp(y,x) is false will have the same relative ordering after calling stable_sort().

Definition at line 5636 of file stl_algo.h.

References std::_Temporary_buffer::begin(), std::__inplace_stable_sort(), and std::_Temporary_buffer::size().