Mega Code Archive

 
Categories / C++ Tutorial / STL Algorithms Sorting
 

Using the generic sort algorithm with a binary predicate

#include <iostream> #include <algorithm> #include <cassert> #include <functional> using namespace std; int main() {   int a[100];   int i;   for (i = 0; i < 100; ++i)     a[i] = i;   random_shuffle(&a[0], &a[100]);   for (i = 0; i < 100; ++i)     cout <<  a[i] << " ";   cout <<"\n\n\n\n";   // Sort into descending order:   sort(&a[0], &a[100], greater<int>());   for (i = 0; i < 100; ++i)     cout <<  a[i] << " ";   return 0; } 12 1 9 98 96 27 58 82 86 90 18 62 32 40 71 51 91 41 94 17 8 47 64 66 65 7 6 76 5 99 77 81 54 35 56 39 25 3 87 16 61 68 14 13 24 55 97 19 20 59 75 33 21 28 78 15 50 34 36 44 83 38 46 60 84 95 57 22 37 23 70 89 31 79 73 92 11 2 88 42 30 52 72 53 67 29 85 43 74 69 45 26 93 10 48 80 0 63 49 4 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 4 6 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0