【Qt | QList 】QList<T> 容器详细介绍和例子代码

news2024/9/27 15:34:03

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
⏰发布时间⏰: 2024-09-26 09:08:26

本文未经允许,不得转发!!!

目录

  • 🎄一、概述
  • 🎄二、QList 新增数据
    • ✨2.1 构造函数
    • ✨2.2 在尾部添加数据
    • ✨2.3 在首部添加数据
    • ✨2.4 在指定位置添加数据
  • 🎄三、QList 查询数据
    • ✨3.1 元素个数相关查询
    • ✨3.2 获取元素
    • ✨3.3 查询元素的下标
    • ✨3.4 判断是否包含某个元素
    • ✨3.5 迭代器
  • 🎄四、QList 删除数据
    • ✨4.1 清空数据
    • ✨4.2 删除某个元素
  • 🎄五、QList 修改数据
  • 🎄六、总结


在这里插入图片描述
在这里插入图片描述

🎄一、概述

Qt 提供了一组通用的基千模板的容器类。对比 C丑一的标准模板库中的容器类, Qt 的这些容器更轻量、更安全并且更容易使用。

存储在 Qt 容器中的数据必须是可赋值的数据类型,包括大多数数据类型,如:基本数据类型(如 int 和 double等)和 Qt 的一些数据类型(如 QString 、 QDate 和 QTime 等)。不过, Qt 的 QObject 及其他的子类(如 QWidget 和 Qdialog 等)是不能够存储在容器中的,例如QList<QLineEdit> list;是不允许的。

QList<T>是迄今为止最常用的容器类,它存储给定数据类型 T 的一列数值。下面将从增删改查四个方面来介绍怎么使用QList<T>


在这里插入图片描述

🎄二、QList 新增数据

✨2.1 构造函数

QList()
QList(const QList<T> &other)
QList(QList<T> &&other)
QList(std::initializer_list<T> args)

🌰构造一个QList对象时,需要指定元素类型<T>,下面是一些构造QList的例子:

// 2.1 构造
QList<int> intList;
intList << 1 << 2 << 3 << 4 << 5;
qDebug() << intList;

QList<int> intListAdd(intList);
qDebug() << intListAdd;

✨2.2 在尾部添加数据

push_back 是为了兼容C++的STL,它等价于append。

void append(const T &value)
void append(const QList<T> &value)
void push_back(const T &value)
QList<T> operator+(const QList<T> &other) const
QList<T> &operator+=(const QList<T> &other)
QList<T> &operator+=(const T &value)
QList<T> &operator<<(const QList<T> &other)
QList<T> &operator<<(const T &value)

🌰在尾部添加数据时,使用 << 运算符最直观,下面是一些例子:

// 2.2 在尾部添加数据
QList<int> listAddTail;
listAddTail << 5 << 4 << 3 << 2 << 1;
qDebug() << listAddTail;    // (5, 4, 3, 2, 1)

listAddTail.append(0);
qDebug() << listAddTail;    // (5, 4, 3, 2, 1, 0)

qDebug() << listAddTail + intList;  // (5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5)

listAddTail += 6;
qDebug() << listAddTail;    // (5, 4, 3, 2, 1, 0, 6)

✨2.3 在首部添加数据

push_front 是为了兼容C++的STL,它等价于prepend。

void prepend(const T &value)
void push_front(const T &value)

🌰举例子:

// 2.3 在首部添加数据
QList<int> listAddHead;
listAddHead << 1 << 2 << 3 << 4 << 5;

listAddHead.prepend(0);
qDebug() << listAddHead;

✨2.4 在指定位置添加数据

void insert(int i, const T &value)
QList::iterator insert(QList::iterator before, const T &value)

🌰举例子:

QList<int> listInsert;
listInsert << 1 << 2 << 3 << 4 << 5;
listInsert.insert(3,3);
qDebug() << listInsert; // (1, 2, 3, 3, 4, 5)

QList<int>::iterator itorInsert = listInsert.begin();   // 迭代器指向首个元素
itorInsert++;   // 迭代器移动到第二个元素
listInsert.insert(itorInsert, 2);
qDebug() << listInsert; // (1, 2, 2, 3, 3, 4, 5)

在这里插入图片描述

🎄三、QList 查询数据

✨3.1 元素个数相关查询

empty 函数是为了兼容 C++ 的STL,等价于 isEmpty 。

int count(const T &value) const	// 计算某个元素的个数
int count() const
int length() const
int size() const
bool isEmpty() const
bool empty() const

🌰举例子:

// 3.1 元素个数相关查询
QList<QString> listSize;
listSize << "1" << "2" << "3" << "4" << "5";
qDebug() << "count=" << listSize.count() << ", len=" << listSize.length() << ", size=" << listSize.size();
qDebug() << "count of 3=" << listSize.count("3");
qDebug() << listSize.isEmpty();
listSize.clear();
qDebug() << listSize.empty();

运行结果:
在这里插入图片描述


✨3.2 获取元素

获取单个元素,QList提供了4种策略:

  • 获取指定位置元素
  • 获取第一个元素
  • 获取最后一个元素
  • 从指定位置开始获取指定长度的元素

函数原型如下:

// 获取指定位置元素
const T &at(int i) const
T value(int i) const
T value(int i, const T &defaultValue) const
T &operator[](int i)
const T &operator[](int i) const

// 获取第一个元素
T &first()
T &front()
const T &first() const
const T &front() const
const T &constFirst() const

// 获取最后一个元素
T &back()
T &last()
const T &last() const
const T &back() const
const T &constLast() const

// 从指定位置开始获取指定长度的元素
QList<T> mid(int pos, int length = -1) const

🌰举例子:

QList<QString> listGet;
listGet << "1" << "2" << "3" << "4" << "5";
qDebug() << "at_1=" << listGet.at(1) << ", value_2=" << listGet.value(2) << ", [3]=" << listGet[3];
qDebug() << "fist=" << listGet.first() << ", front=" << listGet.front();
qDebug() << "back=" << listGet.back() << ", last=" << listGet.last();
qDebug() << "mid_2=" << listGet.mid(2) << ", last=" << listGet.mid(3,2);

运行结果:
在这里插入图片描述


✨3.3 查询元素的下标

indexOf:从首部开始查询第一个匹配到的元素的下标;
lastIndexOf:从尾部开始查询第一个匹配到的元素的下标;

int indexOf(const T &value, int from = ...) const
int lastIndexOf(const T &value, int from = ...) const

🌰举例子:

QList<QString> listGetIndex;
listGetIndex << "1" << "2" << "3" << "4" << "1" << "5";
qDebug() << listGetIndex.indexOf("1");		// 0
qDebug() << listGetIndex.lastIndexOf("1");	// 4

✨3.4 判断是否包含某个元素

bool contains(const T &value) const
bool startsWith(const T &value) const
bool endsWith(const T &value) const

🌰举例子:

// 3.4 判断是否包含某个元素
QList<QString> listContains;
listContains << "1" << "2" << "3" << "4" << "5";
qDebug() << listContains.contains("1");     // true
qDebug() << listContains.startsWith("1");   // true
qDebug() << listContains.endsWith("1");     // false

✨3.5 迭代器

查询的时候,很多时候需要用到迭代器,下面是 QList 的迭代器函数原型:

QList::const_iterator begin() const
QList::const_iterator end() const
QList::const_iterator cbegin() const
QList::const_iterator cend() const
QList::const_iterator constBegin() const
QList::const_iterator constEnd() const
QList::const_reverse_iterator crbegin() const
QList::const_reverse_iterator crend() const
QList::const_reverse_iterator rbegin() const
QList::const_reverse_iterator rend() const

QList::iterator begin()
QList::iterator end()
QList::reverse_iterator rbegin()
QList::reverse_iterator rend()

🌰举例子:

// 3.5 迭代器
QList<QString> listItorSample;
listItorSample << "1" << "2" << "3" << "4" << "5";
for(QList<QString>::const_iterator itor = listItorSample.constBegin(); itor!=listItorSample.constEnd(); itor++)
{
	qDebug() << *itor;
}
qDebug(" ");
for(QList<QString>::const_iterator itor = listItorSample.cbegin(); itor!=listItorSample.cend(); itor++)
{
	qDebug() << *itor;
}
qDebug(" ");
for(QList<QString>::reverse_iterator  itor = listItorSample.rbegin(); itor!=listItorSample.rend(); itor++)
{
	qDebug() << *itor;
}

运行结果:
在这里插入图片描述


在这里插入图片描述

🎄四、QList 删除数据

✨4.1 清空数据

void clear()

🌰举例子:

// 4.1 清空数据
QList<QString> listCLear;
listCLear << "1" << "2" << "3" << "4" << "5";
qDebug() << listCLear;
listCLear.clear();
qDebug() << listCLear;

运行结果:
在这里插入图片描述


✨4.2 删除某个元素

void removeAt(int i)// 删除指定下标的元素
void removeFirst()	// 删除首个的元素
void removeLast()	// 删除最后一个的元素
int removeAll(const T &value)	// 删除 QList对象中 与指定元素相同的所有元素
bool removeOne(const T &value)	// 删除 QList对象中 与指定元素相同的第一元素
void pop_back()		// 兼容 STL,等价于removeLast
void pop_front()	// 兼容 STL,等价于removeFirst

// 删除并获取值,如果不需要获取值,removeAt会更高效
T takeAt(int i)
T takeFirst()
T takeLast()

QList::iterator erase(QList::iterator pos)
QList::iterator erase(QList::iterator begin, QList::iterator end)

🌰举例子:

// 4.2 删除元素
QList<QString> listRemove;
listRemove << "1" << "2" << "3" << "4" << "5" << "4" << "3" << "2" << "1";
listRemove.removeAt(1);
qDebug() << listRemove; // ("1", "3", "4", "5", "4", "3", "2", "1")
listRemove.removeFirst();
qDebug() << listRemove; // ("3", "4", "5", "4", "3", "2", "1")
listRemove.removeLast();
qDebug() << listRemove; // ("3", "4", "5", "4", "3", "2")
listRemove.removeAll("4");
qDebug() << listRemove; // ("3", "5", "3", "2")
listRemove.removeOne("3");
qDebug() << listRemove; // ("5", "3", "2")
QList<QString>::iterator itor = listRemove.begin();
listRemove.erase(++itor);
qDebug() << listRemove; // ("5", "2")
qDebug(" ");

在这里插入图片描述

🎄五、QList 修改数据

修改数据的函数不多,一个是移动元素的move,另一个是替换元素的replace,还有就是将 QList 对象转换成其他容器类对象。具体函数原型如下:

void move(int from, int to)			// 移动元素
void replace(int i, const T &value)	// 替换元素
QSet<T> toSet() const			// 转换成 QSet 对象
std::list<T> toStdList() const	// 转换成 std::list 对象
QVector<T> toVector() const		// 转换成 QVector 对象

🌰举例子:

QList<QString> listChange;
listChange << "1" << "2" << "3" << "4" << "5";
listChange.move(2,listChange.size()-1);
qDebug() << listChange;
listChange.replace(2,"3");
qDebug() << listChange;
QVector<QString> vectorChange = listChange.toVector();
qDebug() << vectorChange;

在这里插入图片描述

🎄六、总结

👉本文介绍 Qt 的 QList 是使用总结,从 增、查、删、改 四个方面去翻译Qt文档。

在这里插入图片描述
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁

参考:
Qt文档
https://blog.csdn.net/u014779536/article/details/111029600

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2170354.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

python面向对象三大特性

面向对象 面向对象编程&#xff0c;是许多编程语言都支持的一种编程思想。 基于模板(类)去创建实体(对象)&#xff0c;使用对象去完成功能开发 面向对象的三大特性 封装继承多态 封装 封装表示&#xff1a;将现实世界事物的属性和行为&#xff0c;封装到类中&#xff0c;描…

打造高质量软件架构 - 9大质量属性

关注TechLead&#xff0c;复旦博士&#xff0c;分享云服务领域全维度开发技术。拥有10年互联网服务架构、AI产品研发经验、团队管理经验&#xff0c;复旦机器人智能实验室成员&#xff0c;国家级大学生赛事评审专家&#xff0c;发表多篇SCI核心期刊学术论文&#xff0c;阿里云认…

球体RCS计算 - 金属球的单站RCS【CST软件分析】

用金属球算RCS雷达散射截面可谓RCS的入门案例&#xff0c;本期用T和I两个求解器算单站RCS进行比较。 Step 1. RCS模板&#xff0c;T-solver&#xff0c; 频率0-5GHz&#xff0c;然后建模&#xff0c;半径10.16cm&#xff0c;可以算出来电尺寸在5GHz大概为三个波长&#xff0c;…

ROS理论与实践学习笔记——2 ROS通信机制之常用的命令

4.1 rosnode操作节点 rosnode&#xff1a;是用于获取节点信息的命令。 rosnode ping 测试到节点的连接状态 rosnode list 列出活动节点 rosnode info 打印节点信息 rosnode machine 列出指定设备上节点 rosnode kill 杀死某个节点 rosnode cleanup 清除不…

Python中的数据处理与分析:从基础到高级

在数据科学和数据分析领域&#xff0c;Python凭借其丰富的库和强大的生态系统&#xff0c;成为了最受欢迎的语言之一。本文将从基础到高级&#xff0c;详细介绍如何使用Python进行数据处理和分析&#xff0c;涵盖数据清洗、数据转换、数据可视化等多个方面。 1. 数据导入与导出…

华为 HCIP-Datacom H12-821 题库 (27)

&#x1f423;博客最下方微信公众号回复题库,领取题库和教学资源 &#x1f424;诚挚欢迎IT交流有兴趣的公众号回复交流群 &#x1f998;公众号会持续更新网络小知识&#x1f63c; 1. 如图&#xff0c;BGP 下有如下配置&#xff0c;下面哪些描述是错误的&#xff1f; A、Time…

Minderbinder:一款基于eBPF的进程安全测试工具

关于Minderbinder Minderbinder是一款基于eBPF的进程安全测试工具&#xff0c;在该工具的帮助下&#xff0c;广大研究人员可以通过注入噪声来测试目标进程的安全性。 Minderbinder 是一款使用 eBPF 将故障注入正在运行的进程的工具。当前版本的Minderbinder 可以通过将 kprobe…

动手学LLM(ch2)

2.1 理解词嵌入 深度神经网络模型&#xff0c;包括大型语言模型&#xff08;LLMs&#xff09;&#xff0c;无法直接处理原始文本&#xff0c;因为文本是分类数据&#xff0c;与神经网络的数学运算不兼容。为了达到这个目的&#xff0c;需要将单词转换为连续值向量。记住一句话…

“Y模型”—我在3年实操后的个人总结

一直想写一篇关于【需求分析】及【产品设计】方面个人最常用的一些方式方法&#xff0c;对于一些刚入行以及埋头苦干的同学来说&#xff0c;大多数情况都是粗放式凭感觉的分析产品。 因为自己也有过这样的阶段&#xff0c;深知这种思考方式的弊端。从用户场景/反馈到具象化的产…

Linux标准IO(四)-格式化I/O输入

C 库函数提供了 3 个格式化输入函数&#xff0c;包括&#xff1a;scanf()、fscanf()、sscanf()&#xff0c;其函数定义如下所示&#xff1a; #include <stdio.h> int scanf(const char *format, ...); int fscanf(FILE *stream, const char *format, ...); int sscanf(c…

2023年金融科技建模大赛(初赛)开箱点评,多分类模型实战

原创作者Toby&#xff0c;文章来源公众号&#xff1a;python风控模型&#xff0c;2023年金融科技建模大赛&#xff08;初赛&#xff09;开箱点评 各位同学大家好&#xff0c;我是Toby老师。2023年金融科技建模大赛&#xff08;初赛&#xff09;从今年10月14日开始&#xff0c;…

最强反推更新!Joy Caption Alpha One详细测评、在线免费使用

免费教程网站&#xff1a;AI教程_深度学习入门指南 - 站长素材 (chinaz.com) 原文链接&#xff1a;最强反推更新&#xff01;Joy Caption Alpha One详细测评、在线免费使用 (chinaz.com) JoyCaption在一周前悄悄上线了最新版本Joycaption alpha one Joycaption alpha one免费在…

Python进阶:利用NotImplemented优化你的对象交互逻辑,让Python对象间的操作更加智能与灵活

推荐阅读&#xff1a;从混乱到清晰&#xff1a;用NotImplementedError重构你的Python代码&#xff0c;NotImplementedError如何助你打造更健壮的API NotImplemented 在Python中&#xff0c;NotImplemented并不是一个异常类&#xff0c;而是一个特殊的值&#xff0c;用于在二元…

linux桌面软件(wps)内嵌到其他窗口

程序测试环境是&#xff1a;slackware系统&#xff0c;属于linux系统&#xff0c;有桌面&#xff08;Xface Session&#xff09;。系统镜像是&#xff1a;slackware64-15.0-install-dvd.iso。qt、c代码实现。 程序功能&#xff1a;将已经打开的wps&#xff08;word、pdf等都可…

【优选算法】(第五篇)

目录 ⻓度最⼩的⼦数组&#xff08;medium&#xff09; 题目解析 讲解算法原理 编写代码 ⽆重复字符的最⻓⼦串&#xff08;medium&#xff09; 题目解析 讲解算法原理 编写代码 ⻓度最⼩的⼦数组&#xff08;medium&#xff09; 题目解析 1.题目链接&#xff1a;. - …

分割数组的最大值

题目链接 分割数组的最大值 题目描述 注意点 0 < nums[i] < 10^61 < nums.length < 10001 < k < min(50, nums.length) 解答思路 首先需要理解题意&#xff0c;需要将这个数组分成 k 个非空的连续子数组&#xff0c;找到划分组合中子数组和的最大值最小…

el-table+el-form实现表单校验和解决不垂直居中导致的问题

el-tableel-form实现表单校验 1.实现el-table的表单校验 关键点123 2.解决不垂直居中导致的问题 问题效果图 解决方案 .item-align-center {display: inline-flex; }

OJ在线评测系统 原生Java代码沙箱核心实现流程三 整理封装输出结果 拿到程序执行时间(stopwatch类) 和 运行内存

我们在之前的操作中已经拿到程序进行了编译和运行 接下来我们要将我们的结果输出 整理输出 // 4.收集整理输出结果 ExecuteCodeResponse executeCodeResponse new ExecuteCodeResponse(); ArrayList<String> outputList new ArrayList<>();for (ExecuteMessage…

Library介绍(一)

之前和大家介绍过cell delay是如何计算的。那么&#xff0c;本文将着重和大家介绍一些timing lib中的各个参数定义是什么意思。会分以下几个部分介绍&#xff1a;库属性描述、时序弧介绍、环境描述、单元描述。之前介绍的cell delay template就是单元描述中的一部分。本文主要介…

网络安全入门必备:这四点你做到了吗?

数据的鸿沟无疑是显而易见的&#xff0c;网络安全领域亟需熟练的专业人员。 组织在这方面投入巨大资金&#xff0c;但挑战依旧存在。 根据最新的研究&#xff0c;有64%的违规行为是导致机构过去一年收入损失及/或罚款的主要原因。 60%的组织在努力招聘网络安全人才&#xff…