Template Function thrust::sort_by_key(RandomAccessIterator1, RandomAccessIterator1, RandomAccessIterator2, StrictWeakOrdering)¶
Function Documentation¶
-
template<typename
RandomAccessIterator1, typenameRandomAccessIterator2, typenameStrictWeakOrdering>
voidthrust::sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp) sort_by_keyperforms a key-value sort. That is,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.Note:
sort_by_keyis not guaranteed to be stable. That is, suppose that*iand*jare equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two keys or the relative order of their corresponding values will be preserved bysort_by_key.This version of
sort_by_keycompares key objects using a function objectcomp.The following code snippet demonstrates how to use
sort_by_keyto sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater<int> comparison operator.- 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.comp: Comparison operator.
- Template Parameters
RandomAccessIterator1: is a model of Random Access Iterator,RandomAccessIterator1is mutable, andRandomAccessIterator1'svalue_typeis convertible toStrictWeakOrdering'sfirst_argument_typeandsecond_argument_type.RandomAccessIterator2: is a model of Random Access Iterator, andRandomAccessIterator2is mutable.StrictWeakOrdering: is a model of Strict Weak Ordering.
#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::sort_by_key(keys, keys + N, values, thrust::greater<int>()); // keys is now { 8, 7, 5, 4, 2, 1} // values is now {'d', 'f', 'e', 'b', 'c', 'a'}
- See
- See
stable_sort_by_key- See
sort