c++STL标准库排序函数std::sort使用 您所在的位置:网站首页 sort排序函数的理解 c++STL标准库排序函数std::sort使用

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

#c++STL标准库排序函数std::sort使用| 来源: 网络整理| 查看: 265

Qt系列文章目录

文章目录Qt系列文章目录前言一、错误原因二、修改后的代码

前言

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

需要注意的是,sort() 函数受到底层实现方式的限制,它仅适用于普通数组和部分类型的容器。换句话说,只有普通数组和具备以下条件的容器,才能使用 sort() 函数: 容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对 array、vector、deque 这 3 个容器提供支持。 如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持 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”的声明

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

一、错误原因

这个错误是因为在使用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()函数。现在这段代码应该能够正常编译并运行。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有