c++STL standard library sorting function std::sort uses

Qt series article directory

foreword

C++ sort() sorting function
The sort() function in the C++ STL standard library is essentially a template function. As described in Table 1, this function is specially used to sort the elements within the specified range in the container or ordinary array. The sorting rules are sorted in ascending order by the size of the element values ​​by default. In addition, we can also choose the standard library provided Other sorting rules (such as std::greater descending sorting rules), and even custom sorting rules.
The sort() function is implemented based on quick sort. For the specific implementation process of quick sort, interested readers can read the article "Quick sort (QSort, quick sort) algorithm".

It should be noted that the sort() function is limited by the underlying implementation, it is only applicable to ordinary arrays and some types of containers. In other words, the sort() function can only be used for ordinary arrays and containers that meet the following conditions: the
iterator type supported by the container must be a random access iterator. This means that sort() only supports three containers: array, vector, and deque.
If the elements in the specified area in the container are sorted in ascending order by default, the element type must support the <less than operator; similarly, if other sorting rules provided by the standard library are selected, the element type must also support the comparison operators used by the underlying implementation of the rule;
When the sort() function implements sorting, it needs to exchange the storage locations of the elements in the container. In this case, if a custom class object is stored in the container, the class must provide a move constructor and a move assignment operator.

Another thing to note is that for elements with equal values ​​in the specified area, the sort() function cannot guarantee that their relative positions will not change. For example, there is the following set of data:

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

}

When using the c++STL standard library sorting function std::sort compiler error: 1.E:\work\ImageManageSys\MainFramework.cpp:586: error: C3867: “MainFramework::sortStrips”: non-standard syntax; please use "&" to create a pointer to member
2.E:\work\ImageManageSys\MainFramework.cpp:586: error: C2672: "std::sort": no matching overloaded function found
3.E:\work\ImageManageSys \MainFramework.cpp:586: error: C2780: “void std::sort(const _RanIt, const _RanIt)”: 2 parameters should be entered, but 3 were provided
MainFramework.cpp(586): error C2780: “void std ::sort(const _RanIt,const _RanIt)”: expected 2 parameters, but provided 3
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\ include\algorithm(7403): note: see declaration of "std::sort"

insert image description here

1. The cause of the error

This error is because when using std::sort(), you pass a member function pointer instead of a normal function pointer. To solve this problem, you can use C++11 lambda expressions as comparators. Here is the modified groupStrips() method:

2. Modified code


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

We define a lambda expression called sortStripsLambda inside the groupStrips() method, which will call the sortStrips() member function. We then pass the sortStripsLambda as a comparator to the std::sort() function. The code should now compile and run fine.

Guess you like

Origin blog.csdn.net/aoxuestudy/article/details/130375273