zoukankan      html  css  js  c++  java
  • Qt容器类之三:通用算法

    在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法。我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList、QLinkedList、QVector、QMap和QHash。如果在目标平台上可以使用STL,那么可以使用STL的算法来代替Qt的这些算法,因为STL提供了更多的算法,而Qt只提供了其中最重要的一些算法。

    在历史上,Qt曾经提供的函数是许多STL算法函数的直接等价物。从Qt 5.0开始,QT鼓励我们直接使用STL中可用的实现;大多数Qt已经被弃用(尽管它们仍然可以用于保持旧的代码编译,但会有警告)。

    大多数情况下,使用废弃的Qt算法函数的应用程序可以很容易地移植到使用等效的STL函数。你需要
    添加#include <algorithm> 头文件,并将Qt函数替换为STL对应函数,
    如下表所示。

    Qt function STL function
    qBinaryFind std::binary_search | std::lower_bound
    qCopy std::copy
    qCopyBackward std::copy_backward
    qEqual std::equal
    qFill std::fill
    qFind std::find
    qCount std::count
    qSort std::sort
    qStableSort std::stable_sort
    qLowerBound std::lower_bound
    qUpperBound std::upper_bound
    qLess std::less
    qGreater std::greater


    下面对其中几个常用的算法进行演示,可以在帮助索引中査看Generic Algorithms关键字来了解其他的算法。

    (1)排序

    排序算法的示例如下:

    //使用快速排序算法对list进行升序排序,排序后两个12的位置不确定
    QList<int> list1;
    list1 << 33 << 12 << 68 << 6 << 12;
    std::sort(list1.begin(), list1.end());
    qDebug() << list1; // 输出:(6, 12, 12, 33, 68)
    
    //使用一种稳定排序算法对list2进行升序排序,排序前在前面的12,排序后依然在前面
    QList<int> list2;
    list2 << 33 << 12 << 68 << 6 << 12;
    std::stable_sort(list2.begin(), list2.end());
    qDebug() << list2; // 输出:(6, 12, 12, 33, 68)
    
    //使用qSort()函数和std::greater算法中使list3反向排序
    QList<int> list3;
    list3 << 33 << 12 << 68 << 6 << 12;
    qSort(list3.begin(), list3.end(), std::greater<int>()); //Qt5已弃用
    qDebug() << list3; // 输出:(68, 33, 12, 12, 6)
    

    默认情況下,qSort()将使用 < 运算符进行元素的比较,即升序。如果需要改为降序,需要将qGreater<T>()当作第三个参数传給qSort()函数。


    (2)查找

    查找算法的示例如下:

    //使用std::find()从list中查找"two",返回第一个对应的值的迭代器,如果没有找到则返回end()
    QStringList list1;
    list1 << "one" << "two" << "three";
    QList<QString>::iterator i = std::find(list1.begin(), list1.end(), "two");
    qDebug() << *i; //输出:"two"
    
    //使用qFind()从list中查找"two",返回第一个对应的值的迭代器,如果没有找到则返回end()
    QStringList list2;
    list2 << "one" << "two" << "three";
    QStringList::iterator j = qFind(list2.begin(), list2.end(), "two");
    qDebug() << *j; //输出:"two"
    

    另外还有个 qBinaryFind() 函数,其行为很像qFind(),所不同的是,qBinaryFind()是二分查找算法,它只适用于查找排序之后的集合,而qFind()则是标准的线性查找。通常,二分查找法使用条件更为苛刻,但是效率也会更高。


    (3)复制

    复制算法的示例如下:

    //将list1中所有项目复制到vect1中
    QStringList list1;
    list1 << "one" << "two" << "three";
    QVector<QString> vect1(list1.count());
    std::copy(list1.begin(), list1.end(), vect1.begin());
    qDebug() << vect1; //输出:QVector("one", "two", "three")
    
    //将list2中所有项目复制到vect2中
    QStringList list2;
    list2 << "one" << "two" << "three";
    QVector<QString> vect2(list2.count());
    qCopy(list2.begin(), list2.end(), vect2.begin());
    qDebug() << vect2; //输出:QVector("one", "two", "three")
    


    参考:

    QT容器中的通用算法


  • 相关阅读:
    PostgreSQL在Update时使用Substring函数截取字符串并且加上CASE WHEN THEN条件判断
    清理Visual Studio 2017的项目历史记录或手工修改Visual Studio 2017的注册表设置
    基于ABP模块组件与依赖注入组件的项目插件开发
    jenkins git can't work ERROR: Timeout after 10 minutes ERROR: Error fetching remote repo 'origin'
    SV randomize
    SV class
    SV coverage
    uvm设计分析——reg
    shell bash-shell
    scrapy的安装
  • 原文地址:https://www.cnblogs.com/linuxAndMcu/p/11027942.html
Copyright © 2011-2022 走看看