Advertisement
Guest User

Sort macro

a guest
Dec 28th, 2015
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. #define by(T, x) [](const T& a, const T& b) { return a.x < b.x; }
  7.  
  8. struct Item {
  9.     int a, b;
  10. };
  11.  
  12. const int N = 10;
  13.  
  14. void print(Item arr[N]) {
  15.     for (int i = 0; i < N; i++) {
  16.         printf("(%d, %d)", arr[i].a, arr[i].b);
  17.  
  18.         if (i != N - 1) {
  19.             printf(", ");
  20.         } else {
  21.             printf("\n");
  22.         }
  23.     }
  24. }
  25.  
  26. int main() {
  27.     Item arr[N];
  28.  
  29.     for (int i = 0; i < N; i++) {
  30.         arr[i].a = i;
  31.         arr[i].b = N - i;
  32.     }
  33.  
  34.  
  35.     sort(arr, arr + N, by(Item, a));
  36.     print(arr);
  37.  
  38.     sort(arr, arr + N, by(Item, b));
  39.     print(arr);
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement