简体   繁体   English

C ++排序数组=>向量迭代器不兼容

[英]C++ sorting array => vector iterators incompatible

I have a simple struct 我有一个简单的结构

struct test 
{
    std::vector<Data> data;

    void sort()
    {
        std::sort(data.begin(), data.end());
    }
}

Data is a struct as well and has only simple fields and implements a < operator like following: 数据也是一个结构,只有简单的字段,并实现了<运算符,如下所示:

EDIT: changes according to feedbacks are included 编辑:包括根据反馈的更改

struct Data 
{
    // ADAPTION 1 - comparator works with reference now
    bool operator<(const Data &data) const
    // bool operator<(const Data data)
    {
        // 1) sortieren nach Typ
        if (type < data.type)
            return true;
        else if (type > data.type)
            return false;

        // 2) nach name sortieren
        if(strlen(strName) > 0)
        {
            if (strncmp(strName, data.strName, 50) < 0) 
                return true;
            else if (strncmp(strName, data.strName, 50) > 0)  
                return false;
        }
        // ADAPTION 2 - added:
        else if (data.strName[0]) // at least 1 character...
             return true;

        // 3) nach Spezialtyp sortieren
        if(strlen(typeSpecial)>0)
        {
            if (strncmp(typeSpecial, data.typeSpecial, 50) < 0) 
                return true;
            else if (strncmp(typeSpecial, data.typeSpecial, 50) > 0) 
                return false;
        }
        // ADAPTION 3 - added:
        else if (data.strName[0]) // at least 1 character...
             return true;

        return false;
    }
}

That's it. 而已。 How can I get the vector iterators incompatible error that way? 如何以这种方式获得vector iterators incompatible错误? I'm not copying any vector, I'm directly calling the sort function with the vector... 我没有复制任何向量,而是直接用向量调用sort函数...

In Visual Studio 2005, I've never had a problem, in Visual Studio 2012 this problem appeared and I don't know why and how to avoid it 在Visual Studio 2005中,我从来没有遇到过问题,在Visual Studio 2012中,出现了这个问题,我不知道为什么以及如何避免它

There're a few issues with your code: 您的代码存在一些问题:

bool operator<(const Data data)

...should be... ...应该...

bool operator<(const Data& data) const

Then: 然后:

    if(strlen(strName) > 0)
    {
        if (strncmp(strName, data.strName, 50) < 0) 
            return true;
        else if (strncmp(strName, data.strName, 50) > 0)  
            return false;
    }

...needs... ...需要...

    else if (data.strName[0]) // at least 1 character...
        return true;

This is required to ensure strict weak ordering, which is a requirement of std::sort 's and means that when a < b , !(b < a) . 这是确保严格的弱排序所必需的,这是std::sort的要求,并且意味着当a < b!(b < a)

Similarly: 类似地:

    if(strlen(typeSpecial)>0)
    {
        if (strncmp(typeSpecial, data.typeSpecial, 50) < 0) 
            return true;
        else if (strncmp(typeSpecial, data.typeSpecial, 50) > 0) 
            return false;
    }

...needs... ...需要...

    else if (data.typeSpecial[0])
        return true;

Your string comparisons would be much cleaner if you used std::string s. 如果使用std::string则字符串比较会更加std::string If you do stick with ASCIIZ data, it would be better to use eg sizeof typeSpecial instead of 50 etc.. You can improve performance and code concision by doing less comparisons and trusting strncmp to handle empty strings appropriately (which it will): 如果您坚持使用ASCIIZ数据,最好使用例如sizeof typeSpecial而不是50等。您可以通过减少比较并信任strncmp适当地处理空字符串(它将这样做)来提高性能和代码简洁性:

if (type < data.Type) return true;
if (type > data.Type) return false;

int d = strncmp(strName, data.strName, sizeof strName);
if (d == 0)
    d = strncmp(typeSpecial, data.typeSpecial, sizeof typeSpecial);
return d < 0;

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

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