0

下面是我的代码:

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 向量进行排序。它不适用于上面给出的方式。

注意:我无法更改上面列出的任何结构的数据类型。

4

3 回答 3

0

我想根据 *key 值对上面的 lde 向量进行排序。请给我一些建议...它>不适用于上面给出的方式。

您的代码在您编写时工作,但它是无效的。您的关键点是 char[8],它由 size_t 和 char[4] 组成。

但随后调用 return strcoll((char *)a.key, (char *)b.key) > 0; 和 cout << "KEY ::" << (char *)(lde[j].key);.

但是key不指向字符串,它指向size_t,如果你在key的and处添加'\0',并使用((char *)key) + 4,一切都应该按预期工作

于 2012-11-15T23:24:57.713 回答
0

好吧,您的代码中有两个问题:

首先,您将 LeafDataEntry 的键中的字符串保存为 [4 bytes for the size of the string] + [the string] 但 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 << "}";
于 2012-11-15T23:29:15.393 回答
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;
}
于 2012-11-16T03:42:59.040 回答