Skip to content

Instantly share code, notes, and snippets.

@yzchen
Created April 28, 2019 13:46
Show Gist options
  • Save yzchen/56e2bbdb1b79a61f0a50a14b663671de to your computer and use it in GitHub Desktop.
Save yzchen/56e2bbdb1b79a61f0a50a14b663671de to your computer and use it in GitHub Desktop.
Truncate elements during conversion
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define IndexType int
typedef struct Int1 { IndexType x[1]; } Int1;
typedef struct Int2 { IndexType x[2]; } Int2;
typedef struct Int3 { IndexType x[3]; } Int3;
typedef struct Int4 { IndexType x[4]; } Int4;
typedef struct Int5 { IndexType x[5]; } Int5;
int compInt1(const void *elem1, const void *elem2) {
Int1 f = *((Int1 *) elem1);
Int1 s = *((Int1 *) elem2);
if (f.x[0] > s.x[0]) return 1;
if (f.x[0] < s.x[0]) return -1;
return 0;
}
int compInt2(const void *elem1, const void *elem2) {
Int2 f = *((Int2 *) elem1);
Int2 s = *((Int2 *) elem2);
if (f.x[1] > s.x[1]) return 1;
if (f.x[1] < s.x[1]) return -1;
return 0;
}
int compInt3(const void *elem1, const void *elem2) {
Int3 f = *((Int3 *) elem1);
Int3 s = *((Int3 *) elem2);
if (f.x[2] > s.x[2]) return 1;
if (f.x[2] < s.x[2]) return -1;
return 0;
}
int main() {
int y[6] = {2,6,7,5,4,3};
qsort(y, 2, 3 * sizeof(int), compInt1);
for (int i = 0; i < 6; i++)
cout << y[i] << " ";
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment