I wrote this routine to order items, keep only unique items, where it takes in an array of type T, and the size of the array. It returns the new size of the array after processing.
template <class T>
int reduce(T array[], int size) {
T *begin = array;
T *end = array + size;
sort(begin, end);
T *end_new = unique(begin, end);
return end_new - array;
}
My question is I was expecting it to sort const char *data like
{"aa", "bb", "bc", "ca", "bc", "aa", "cc", "cd", "ca", "bb"};
into //aa bb bc ca cc cd
However it does it the opposite way, : “cd cc ca bc bb aa”
Why does it do that? Does it not use the standard string comparisons? If I wanted to, how could I alter it so it would order const char * alphabetically? thanks.
sort()usesoperator<per default, which would just compare the addresses in your case.If you want to sort C-strings, you have to pass a comparator to
sort(). To do this generically you can let the user pass a comparator, use specialization on a comparator function or a combination of these: