c++STL标准库排序函数std::sort使用

Qt系列文章目录

前言

C++ sort()排序函数
C++ STL 标准库中的 sort() 函数,本质就是一个模板函数。正如表 1 中描述的,该函数专门用来对容器或普通数组中指定范围内的元素进行排序,排序规则默认以元素值的大小做升序排序,除此之外我们也可以选择标准库提供的其它排序规则(比如std::greater降序排序规则),甚至还可以自定义排序规则。
sort() 函数是基于快速排序实现的,有关快速排序的具体实现过程,感兴趣的读者可阅读《快速排序(QSort,快排)算法》一文。

需要注意的是,sort() 函数受到底层实现方式的限制,它仅适用于普通数组和部分类型的容器。换句话说,只有普通数组和具备以下条件的容器,才能使用 sort() 函数:
容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对 array、vector、deque 这 3 个容器提供支持。
如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持<小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
sort() 函数在实现排序时,需要交换容器中元素的存储位置。这种情况下,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。

另外还需要注意的一点是,对于指定区域内值相等的元素,sort() 函数无法保证它们的相对位置不发生改变。例如,有如下一组数据:

bool MainFramework::sortStrips(const QString &a, const QString &b)
{
    
    
    QStringList aSplit = a.split('_');
    QStringList bSplit = b.split('_');

    int aFirstPart = aSplit[0].toInt();
    int bFirstPart = bSplit[0].toInt();

    if (aFirstPart == bFirstPart) {
    
    
        int aSecondPart = aSplit[5].toInt();
        int bSecondPart = bSplit[5].toInt();
        return aSecondPart > bSecondPart;
    } else {
    
    
        return aFirstPart < bFirstPart;
    }
}

void MainFramework::groupStrips()
{
    
    
    for(const QString& fileName : m_colorCAllFiles)
    {
    
    
        if(fileName.contains("_04_")) {
    
    
            m_colorCfiles04.append(fileName);
        } else if(fileName.contains("_02_")) {
    
    
            m_colorCfiles02.append(fileName);
        } else if(fileName.contains("_05_")) {
    
    
            m_colorCfiles05.append(fileName);
        }
    }

    std::sort(m_colorCfiles04.begin(), m_colorCfiles04.end(), sortStrips);
    std::sort(m_colorCfiles02.begin(), m_colorCfiles02.end(), sortStrips);
    std::sort(m_colorCfiles05.begin(), m_colorCfiles05.end(), sortStrips);

}

在使用c++STL标准库排序函数std::sort编译器报错:1.E:\work\ImageManageSys\MainFramework.cpp:586: error: C3867: “MainFramework::sortStrips”: 非标准语法;请使用 “&” 来创建指向成员的指针
2.E:\work\ImageManageSys\MainFramework.cpp:586: error: C2672: “std::sort”: 未找到匹配的重载函数
3.E:\work\ImageManageSys\MainFramework.cpp:586: error: C2780: “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个
MainFramework.cpp(586): error C2780: “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\algorithm(7403): note: 参见“std::sort”的声明

在这里插入图片描述

一、错误原因

这个错误是因为在使用std::sort()时,你传递了一个成员函数指针,而非普通函数指针。为了解决这个问题,你可以使用C++11的lambda表达式作为比较器。以下是修改后的groupStrips()方法:

二、修改后的代码


bool MainFramework::sortStrips(const QString &a, const QString &b)
{
    
    
    QStringList aSplit = a.split('_');
    QStringList bSplit = b.split('_');

    int aFirstPart = aSplit[0].toInt();
    int bFirstPart = bSplit[0].toInt();

    if (aFirstPart == bFirstPart) {
    
    
        int aSecondPart = aSplit[5].toInt();
        int bSecondPart = bSplit[5].toInt();
        return aSecondPart > bSecondPart;
    } else {
    
    
        return aFirstPart < bFirstPart;
    }
}

void MainFramework::groupStrips()
{
    
    
    for(const QString& fileName : m_colorCAllFiles)
    {
    
    
        if(fileName.contains("_04_")) {
    
    
            m_colorCfiles04.append(fileName);
        } else if(fileName.contains("_02_")) {
    
    
            m_colorCfiles02.append(fileName);
        } else if(fileName.contains("_05_")) {
    
    
            m_colorCfiles05.append(fileName);
        }
    }

    auto sortStripsLambda = [this](const QString &a, const QString &b) {
    
    
        return this->sortStrips(a, b);
    };

    std::sort(m_colorCfiles04.begin(), m_colorCfiles04.end(), sortStripsLambda);
    std::sort(m_colorCfiles02.begin(), m_colorCfiles02.end(), sortStripsLambda);
    std::sort(m_colorCfiles05.begin(), m_colorCfiles05.end(), sortStripsLambda);
}

我们在groupStrips()方法内定义了一个名为sortStripsLambda的lambda表达式,这个表达式将调用sortStrips()成员函数。然后,我们将sortStripsLambda作为比较器传递给std::sort()函数。现在这段代码应该能够正常编译并运行。

猜你喜欢

转载自blog.csdn.net/aoxuestudy/article/details/130375273
今日推荐