以下是我的代码:
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;
}
我想根据*键值对上面的lde向量进行排序。它没有按照上面给出的方式工作。
注意:我无法更改上面列出的任何结构的数据类型。
答案 0 :(得分:0)
我想根据*键值对上面的lde向量进行排序。请以某种方式向我推荐......它&gt;不按上述方式工作。
您的代码在您编写时有效,但无效。 你的关键点是char [8],它由size_t和char [4]组成。
然后调用return strcoll((char *)a.key,(char *)b.key)&gt; 0;和 cout&lt;&lt; &#34; KEY ::&#34; &LT;&LT; (char *)(lde [j] .key);。
但键不指向字符串,它指向size_t,如果你添加&#39; \ 0&#39;在关键, 并使用((char *)键)+ 4,所有都应按预期工作
答案 1 :(得分:0)
您的代码中存在两个问题:
首先,你将LeafDataEntry的键中的字符串保存为[字符串大小的4个字节] + [字符串],但是strcoll将字符串以'\ 0'结尾并且没有大小引导它们,正如你所说的那样不希望您的数据类型发生任何变化,这可以解决您的问题:
bool leadNode_Key_asc( const LeafDataEntry &a, const LeafDataEntry &b){
size_t size1 = *(unsigned*)(a.key), size2 = *(unsigned*)(b.key);
string a1 = string((char*)a.key + 4, size1), a2 = string((char*)b.key + 4, size2);
return a1 < a2;
}
第二,这两行:
cout << ", RID ::" << "{" << lde[j].rid.pageNum << ", " <<
lde[j].rid.slotNum << "}";
应替换为:
cout << ", RID ::" << "{" << lde[j].rid.page << ", " <<
lde[j].rid.slot << "}";
答案 2 :(得分:0)
typedef struct
{
unsigned page;
unsigned slot;
} RID;
struct KeyStruct
{
unsigned size;
char szKey[4];
KeyStruct(unsigned s, char *k) : size(s) { memcpy(szKey, k, sizeof(szKey)); }
};
//Below struct has the Key on which I want to apply the sorting
struct LeafDataEntry
{
KeyStruct key;
RID rid;
bool operator<(const LeafDataEntry& lde)
{
for (int i = 0; i < sizeof(key.szKey); ++i)
{
if (key.szKey[i] != lde.key.szKey[i]) return key.szKey[i] < lde.key.szKey[i];
}
return false;
}
};
//This is the sorting function I am using
bool leadNode_Key_asc( const LeafDataEntry &a, const LeafDataEntry &b)
{
return true;
}
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.reserve(2);
LeafDataEntry lde_a = { KeyStruct(size, a), aRID };
lde.push_back(lde_a);
LeafDataEntry lde_b = { KeyStruct(size, b), bRID };
lde.push_back(lde_b);
std::sort(lde.begin(), lde.end());
std::cout << "Sorted Data :: " << std::endl;
for(int j=0; j<2; j++)
{
std::cout << "KEY :: " << lde[j].key.size << ":" << lde[j].key.szKey;
std::cout << ", RID ::" << "{" << lde[j].rid.page << ", " << lde[j].rid.slot << "}" << std::endl;
}
return 0;
}