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

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

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

目录

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

cpp 复制代码
QList()
QList(const QList<T> &other)
QList(QList<T> &&other)
QList(std::initializer_list<T> args)

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

cpp 复制代码
// 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。

cpp 复制代码
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)

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

cpp 复制代码
// 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。

cpp 复制代码
void prepend(const T &value)
void push_front(const T &value)

🌰举例子:

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

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

✨2.4 在指定位置添加数据

cpp 复制代码
void insert(int i, const T &value)
QList::iterator insert(QList::iterator before, const T &value)

🌰举例子:

cpp 复制代码
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 。

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

🌰举例子:

cpp 复制代码
// 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种策略:

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

函数原型如下:

cpp 复制代码
// 获取指定位置元素
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

🌰举例子:

cpp 复制代码
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:从尾部开始查询第一个匹配到的元素的下标;

cpp 复制代码
int indexOf(const T &value, int from = ...) const
int lastIndexOf(const T &value, int from = ...) const

🌰举例子:

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

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

cpp 复制代码
bool contains(const T &value) const
bool startsWith(const T &value) const
bool endsWith(const T &value) const

🌰举例子:

cpp 复制代码
// 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 的迭代器函数原型:

cpp 复制代码
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()

🌰举例子:

cpp 复制代码
// 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 清空数据

cpp 复制代码
void clear()

🌰举例子:

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

运行结果:


✨4.2 删除某个元素

cpp 复制代码
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)

🌰举例子:

cpp 复制代码
// 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 对象转换成其他容器类对象。具体函数原型如下:

cpp 复制代码
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 对象

🌰举例子:

cpp 复制代码
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

相关推荐
fxshy2 分钟前
01-Cesium添加泛光线
开发语言·前端·javascript
qq_393060474 分钟前
在wsl 上运行window的R代码
开发语言·python·r语言
@小码农6 分钟前
速查!2024 CSP-J/S第一轮认证成绩查询及晋级分数线
开发语言·c++·python·职场和发展·蓝桥杯·noi
Invulnerabl_DL9 分钟前
C++的智能指针
开发语言·c++
Pandaconda20 分钟前
【计算机网络 - 基础问题】每日 3 题(二十六)
开发语言·经验分享·笔记·后端·计算机网络·面试·职场和发展
虽千万人 吾往矣21 分钟前
golang strings api接口
开发语言·后端·golang
景天科技苑24 分钟前
【Go语言】深入解读Go语言中的指针,助你拨开迷雾见月明
开发语言·后端·golang·指针·go指针·go语言指针
虽千万人 吾往矣24 分钟前
golang格式化输入输出
开发语言·后端·golang
MavenTalk26 分钟前
Python在进行LLM应用相关开发常用的技术框架
开发语言·python·大模型·llm·大语言模型
吾爱星辰2 小时前
【解密 Kotlin 扩展函数】扩展函数的底层原理(十八)
java·开发语言·jvm·kotlin