以下は私のコードです:
typedef struct
{
unsigned page;
unsigned slot;
} RID;
//Below struct has the Key on which I want to apply the sorting
struct LeafDataEntry
{
void *key;
RID rid;
};
//This is the sorting function I am using
bool leadNode_Key_asc( const LeafDataEntry &a, const LeafDataEntry &b){
return strcoll((char *)a.key, (char *)b.key) > 0;
//(strcmp((char *)a.key, (char *)b.key) > 0);
}
int main(){
vector<LeafDataEntry> lde;
char a[4] = {'D', 'B', 'C', 'D'};
RID aRID = {0,0};
char b[4] = {'A', 'C', 'B', 'A'};
RID bRID = {0,1};
unsigned size = sizeof(unsigned);
lde.resize(2);
char *tempPtr = (char *)malloc(8 + sizeof(RID));
memcpy(tempPtr, &size, 4);
tempPtr += 4;
memcpy(tempPtr, a, 4);
tempPtr -= 4;
lde[0].key = malloc(8);
memcpy(lde[0].key, tempPtr, 8);
memcpy(&lde[0].rid, &aRID, sizeof(RID));
memcpy(tempPtr, &size, 4);
tempPtr += 4;
memcpy(tempPtr, b, 4);
tempPtr -= 4;
lde[1].key = malloc(8);
memcpy(lde[1].key, tempPtr, 8);
memcpy(&lde[1].rid, &bRID, sizeof(RID));
std::sort(lde.begin(), lde.end(), leadNode_Key_asc);
cout << "Sorted Data :: " << endl;
for(int j=0; j<2; j++){
cout << "KEY :: " << (char *)(lde[j].key);
cout << ", RID ::" << "{" << lde[j].rid.pageNum << ", " <<
lde[j].rid.slotNum << "}";
}
return 0;
}
*key値に基づいて上記のldeベクトルをソートしたいと思います。上記の方法では機能しません。
注:上記の構造体のデータ型を変更することはできません。