QList class for Qt learning

Definition of QList

1. Introduction

QList<T> is a commonly used container class, which is a list that stores values ​​of a given type, and these values ​​can be accessed through an index.

2. Definition

QList<QString> list; //定义存储数据类型为QString的列表
list<<"aa"<<"bb"<<"cc";//添加元素,结果为("aa","bb","cc")
list[0]="cc";//输出的结果为("cc","bb","cc")

Three, the operation of QList

1.获取大小
int len=list.size();
2.获取元素
qDebug()<<list.at(i);
qDebug()<<list[i];
3.替换操作
list.replace(i,"bc");//将list里面第i位置的元素换位bc
4.在列表尾部添加数据
list.append("bb");
5.在列表头部添加
list.prepend("mm");
6.在指定位置插入元素
list.insert(2,"qq");
7.删除指定位置元素并获取
QString str=list.taskAt(2);
8.交换元素
list.swap(1,2);//将位置1跟位置2交换
9.列表是否包含某值
list.contains("bb");//有包含为true,无包含为false
10.列表包含某值的个数
list.count("bb");
11.查找元素
list.indexof("mm");//从0开始查找元素为mm
list.indexof("zz",2);//从指定位置查找元素

Four, QList traversal

  1. The first
QList<int>list;
QList<int>::iterator it1=list.begin();
for(;it1!=list.end();++it1)
{
    
    
   qDebug()<<*it1;
}
  1. the second
for(int i=0;i<list.size();i++) qDebug()<<list[i];
  1. third
QListIterator<int>it2(list);
for(it2.toFront();it2.hasNext();)
{
    
    
	qDebug()<<it2.next();
}

For more details, please refer to

Guess you like

Origin blog.csdn.net/m0_56626019/article/details/129816664