Qt学习笔记常用容器

简介: 主要说Qt的以下几种容器 1.QList 2.QLinkedList 3.Map 和一些常用的容器方法的使用 qSort qCopy qFind 1.QList泛型集合是最常用的一种容器 看一下它的常用 操作 添加删除和两个迭代器 QListIterator和QMutableL...

主要说Qt的以下几种容器

1.QList<T>

2.QLinkedList<T>

3.Map<T>

和一些常用的容器方法的使用

qSort

qCopy

qFind

1.QList<T>泛型集合是最常用的一种容器

看一下它的常用 操作

添加删除和两个迭代器

QListIterator和QMutableListIterator
#include <QCoreApplication>
#include<QList>
#include<QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<int> listInt;

    //添加
    for(int i =0;i<10;i++)
    {
        listInt.append(i);
        //也可以这样添加元素
        //listInt<<i;
    }
    //删除
    qDebug()<<"删除3";
    listInt.removeAt(3);
    //查询
    foreach (int item, listInt) {
        qDebug()<<item;
    }


    qDebug()<<"Iterator";

    //迭代器
    QListIterator<int> iterator(listInt);
    while(iterator.hasNext())
    {

        qDebug()<<iterator.next();
        if(iterator.hasNext())
        qDebug()<<"the Next is :"<<iterator.peekNext();
    }
    //返转
    iterator.toBack();
    while(iterator.hasPrevious())
    {
        qDebug()<<iterator.previous();
    }
    qDebug()<<"可变迭代器QMutableListIterator";
    //可变的迭代器
    QMutableListIterator<int> mutableiterator(listInt);
    mutableiterator.insert(13);
    mutableiterator.insert(14);
    mutableiterator.insert(15);
    while(mutableiterator.hasNext())
    {
       int i=  mutableiterator.next();
       if(i==2||i==6)
       {
           mutableiterator.remove();
       }
    }

    //查询
    foreach (int item, listInt) {
        qDebug()<<item;
    }
    return a.exec();
}

 

2.QLinkedList<T> 

QLinkedList<T>和QList<T>差不多,不同的一点是它是用迭代器做的访问项

也就是说QList<int> list只以通过这样访问它的内容list[i]而QLinkedList不可以只能用Iterator

性能上它要高于QList<T>

#include <QCoreApplication>
#include<QLinkedList>
#include<QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QLinkedList<int> link;
    link<<1<<2<<2<<3<<4;
    qDebug()<<"迭代器访问QLinkedListIterator";
    QLinkedListIterator<int> iterator(link);
    while(iterator.hasNext())
    {
        qDebug()<< iterator.next();
    }
    //删除第一个2
    link.removeOne(2);
    //添加两个3这两种方式一样
    link.push_back(3);
    link.append(3);
    //删除所有的3
    link.removeAll(3);
    qDebug()<<"普通访问foreach";
    foreach (int item, link) {
        qDebug()<< item;
    }

    qDebug()<<"迭代器QMutableLinkedListIterator";
    QMutableLinkedListIterator<int> mutableIter(link);

    while(mutableIter.hasNext())
    {
        int i= mutableIter.next();
        if(i==1)
        {
            mutableIter.insert(90);
        }
        if(i==4)
        {
            mutableIter.remove();
        }
        qDebug()<<i;
    }
    qDebug()<<"迭代器QMutableLinkedListIterator重新访问";
    mutableIter.toFront();
    while(mutableIter.hasNext())
    {
        int i= mutableIter.next();
        qDebug()<<i;
    }
    //mutable
    return a.exec();
}
a

3Map<T>

map类型是一个键值对 key/value组成 其它的和上边的两个集合没什么区别 

#include <QCoreApplication>
#include<QMap>
#include<QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QMap<int,QString> map;
    map.insert(1,"a");
    map.insert(2,"b");
    map.insert(3,"c");
    QMutableMapIterator<int,QString> mutableIte(map);
    while(mutableIte.hasNext())
    {
        mutableIte.next();
        qDebug()<<mutableIte.key()<<" "<<mutableIte.value();
    }
    return a.exec();
}

 

下边说一下常用的集合操作方法

qSort

qCopy

qFind

#include <QCoreApplication>
#include<QList>
#include<QDebug>
#include<QVector>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<int> listStrs;
    listStrs<<10<<5<<8<<2<<7;
    qSort(listStrs);
    foreach (int i, listStrs) {
        qDebug()<<i;
    }
    qDebug()<<"____________________________";
    listStrs.clear();
    listStrs<<10<<5<<8<<2<<7;
    qSort(listStrs.begin()+1,listStrs.end()-1);
    foreach (int i, listStrs) {
        qDebug()<<i;
    }


    qDebug()<<"______________qCopy______________";
    QVector<int> newVec(5);
    qCopy(listStrs.begin(),listStrs.end(),newVec.begin());
    foreach (int i, newVec) {
        qDebug()<<i;
    }
    qDebug()<<"______________qFind______________";
    listStrs.clear();
    listStrs<<2<<5<<8<<2<<7;
    QList<int>::const_iterator iterFin=qFind(listStrs,2);
    if(iterFin!=listStrs.end())
    {
        qDebug()<<*iterFin;
    }
    else
    {
        qDebug()<<"notFound!";
    }
    return a.exec();
}

 

 

目录
相关文章
|
4天前
|
JavaScript API iOS开发
DeepSeek Harness下载、安装、配置一篇搞定(2026整理,送常用插件)
DeepSeek Harness(DSH)是DeepSeek于2026年开源的MIT协议AI Agent框架,支持插件化组装模型、工具、沙箱与UI,兼容主流大模型。本文提供免Node.js的桌面端安装包(Win/macOS),含详细图文教程及热门插件推荐。
|
云安全 安全 网络安全
|
5天前
|
iOS开发 Docker MacOS
【2026实测】Docker Desktop下载、安装、汉化、配置教程(附官网安装包)
Docker Desktop 是 Docker 官方推出的桌面容器管理工具,集成引擎、CLI、图形界面与 Kubernetes,支持 Win/macOS/Linux。2026 最新版 4.88.1 强化 ARM 原生运行、镜像加速及资源可视化,附中文汉化与国内镜像配置指南。
853 1
|
17天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
2962 49
|
11天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
2099 2
|
5天前
|
安全 算法 定位技术
《高性价比人生指南》完整原版:普通人用最少的资源,过好这一生的底层逻辑~How To Live Better
📖《高性价比人生指南》是一份基于实证、拒绝焦虑的生存手册,倡导以“人生时薪”为视角,重新计算时间、金钱与精力的成本与收益。它不承诺逆袭,只教人算清账:省下的钱若耗掉时间便亏;买体验比买物品更值;睡眠是最高杠杆的健康投资;关系贵在情绪价值而非表面热络;学习重在改变行为而非自我安慰。核心理念:用低成本、高回报的微小行动,换取长期的选择权——不是成为谁,而是掌控自己人生的主动权。
|
10天前
|
人工智能 JSON Linux
【保姆级】ComfyUI部署教程(下载+安装+配置+工作流,2026最新)
ComfyUI 是一款开源节点式AI生成工具,支持图像、视频、音频及3D内容创作。通过拖拽连线组合模型加载、提示词、采样等节点,构建可复用、易修改的工作流,兼容SD1.5、SDXL、Flux、Qwen Image等主流模型。
|
4天前
|
Linux 开发工具 iOS开发
Codex保姆级教程(下载+安装+登录+使用,含所有版本,2026最新)
Codex是OpenAI推出的开源AI编程智能体,本地运行,可理解整个代码库,支持自然语言指令改代码、执行命令、跑测试、修Bug,兼容Win/macOS/Linux。(239字)