Template Function thrust::stable_sort_by_key(RandomAccessIterator1, RandomAccessIterator1, RandomAccessIterator2)¶
Function Documentation¶
-
template<typename
RandomAccessIterator1, typenameRandomAccessIterator2>
voidthrust::stable_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first) stable_sort_by_keyperforms a key-value sort. That is,stable_sort_by_keysorts the elements in[keys_first, keys_last)and[values_first, values_first + (keys_last - keys_first))into ascending key order, meaning that ifiandjare any two valid iterators in[keys_first, keys_last)such thatiprecedesj, andpandqare iterators in[values_first, values_first + (keys_last - keys_first))corresponding toiandjrespectively, then*jis not less than*i.As the name suggests,
stable_sort_by_keyis stable: it preserves the relative ordering of equivalent elements. That is, ifxandyare elements in[keys_first, keys_last)such thatxprecedesy, and if the two elements are equivalent (neitherx < ynory < x) then a postcondition ofstable_sort_by_keyis thatxstill precedesy.This version of
stable_sort_by_keycompares key objects usingoperator<.The following code snippet demonstrates how to use
stable_sort_by_keyto sort an array of characters using integers as sorting keys.- Pre
The range
[keys_first, keys_last))shall not overlap the range[values_first, values_first + (keys_last - keys_first)).- Parameters
keys_first: The beginning of the key sequence.keys_last: The end of the key sequence.values_first: The beginning of the value sequence.
- Template Parameters
RandomAccessIterator1: is a model of Random Access Iterator,RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis a model of LessThan Comparable, and the ordering relation onRandomAccessIterator1'svalue_typeis a strict weak ordering, as defined in the LessThan Comparable requirements.RandomAccessIterator2: is a model of Random Access Iterator, andRandomAccessIterator2is mutable.
#include <thrust/sort.h> ... const int N = 6; int keys[N] = { 1, 4, 2, 8, 5, 7}; char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'}; thrust::stable_sort_by_key(keys, keys + N, values); // keys is now { 1, 2, 4, 5, 7, 8} // values is now {'a', 'c', 'b', 'e', 'f', 'd'}
- See
- See
sort_by_key- See
stable_sort