Skip to content

Instantly share code, notes, and snippets.

@Khalefa
Created September 25, 2018 01:36
Show Gist options
  • Save Khalefa/f14bcc366aa466022a93682632b4283f to your computer and use it in GitHub Desktop.
Save Khalefa/f14bcc366aa466022a93682632b4283f to your computer and use it in GitHub Desktop.
sort record
#include <algorithm>
#include <iostream>
#include<random>
using namespace std;
template <int size>
struct record_int {
int x;
char P[size - sizeof(int)];
bool operator<(const record_int<size> & rhs) {
return x < rhs.x;
}
record_int(){}
record_int(int xx) :x{ xx } {}
};
template <int size>
struct record_float {
float x;
char P[size - sizeof(float)];
bool operator<(const record_float<size> & rhs) {
return x < rhs.x;
}
record_float() {}
record_float(float xx) :x{ xx } {}
};
template <int size>
struct record_string {
string x;
char P[size - sizeof(string)];
bool operator<(const record_string<size> & rhs) {
return x < rhs.x;
}
record_string() {}
record_string(string xx) :x{ xx } {}
};
template <int size>
void generate_record_int(record_int<size> * arr, int n) {
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(0, 26 - 1);
for (int i = 0; i < n; ++i) {
arr[i] = dis(gen);
}
}
int main() {
record_int<16> *arr= new record_int<16>[100];
generate_record_int<16>(arr, 100);
sort(arr, arr + 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment