简体   繁体   English

对具有指针的结构的向量进行排序

[英]Sorting a vector of structs having pointer

Below is my code: 下面是我的代码:

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;
}

I want to sort the above lde vector based on the *key value. 我想根据* key值对上述lde向量进行排序。 It's not working with the way given above. 它与上面给出的方式不兼容。

NOTE: I cannot change the data types of any of the structs listed above. 注意:我不能更改上面列出的任何结构的数据类型。

I want to sort the above lde vector based on the *key value. 我想根据* key值对上述lde向量进行排序。 Please suggest me some way...Its >not working with the way given above. 请以某种方式建议我...它与以上给出的方式不兼容。

Your code works as you write, but it is invalid. 您的代码可以在编写时使用,但无效。 Your key point to char[8], which consists of size_t and char[4]. 您的关键点是char [8],它由size_t和char [4]组成。

But then call return strcoll((char *)a.key, (char *)b.key) > 0; 但是然后调用return strcoll((char *)a.key,(char *)b.key)> 0; and cout << "KEY :: " << (char *)(lde[j].key);. 和cout <<“ KEY ::” <<(char *)(lde [j] .key);。

but key not point to string, it point to size_t, if you add '\\0' at the and of key, and use ((char *)key) + 4, all should works as expected 但是键不是指向字符串,而是指向size_t,如果您在键的和处添加'\\ 0',并使用((char *)key)+ 4,则所有操作都应按预期进行

Well there are two problems in your code: 好了,您的代码中有两个问题:

first, you're saving string in key of LeafDataEntry as [4 bytes for size of the string] + [the string] but strcoll takes strings ended with '\\0' and without size leading them, as you said that you don't want any change in your datatypes, this would solve your problem: 首先,您要在LeafDataEntry的键中将字符串保存为[字符串大小的4个字节] + [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;
}

second, these two lines: 第二,这两行:

    cout << ", RID ::" << "{" << lde[j].rid.pageNum << ", " <<        
    lde[j].rid.slotNum << "}";

should be replaced with: 应替换为:

    cout << ", RID ::" << "{" << lde[j].rid.page << ", " <<        
    lde[j].rid.slot << "}";
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM