当前位置:网站首页>QT中foreach的使用
QT中foreach的使用
2022-06-30 03:03:00 【my_angle2016】
示例:
在标准C++中,并没有foreach关键字,但是在QT中,可以使用这一个关键字,其主要原因是QT自己增加了这一个关键字,就像slots和signals、emit等一样。增加的foreach关键字在编译时会进行预处理。
用法:
foreach (varItem , Items) // foreach(variable ,container)
其中,varItem(variable)是容器Items(container)中的一个项,相当于:variable=container.item 。遍历会从头遍历到尾。
如以下代码
QStringList slt = {"abc", "qwe", "upo"};
foreach(QString s , slt )
{
cout<<s<<endl;
}
// 输出结果为:
abc
qwe
upo
QStringList test;
test.append("aaaa");
test.append("bbbb");
test.append("cccc");
QString tempa;
QString tempb;
int i = 0;
foreach(tempa, test)
{
qDebug() << tempa << endl;
if(1==i)
tempb = tempa;
i++;
}
qDebug() << tempb << endl;
运行结果:
上段代码,清晰的揭示了,foreach函数的作用,即将test里面的元素,通过循环传递给tempa
边栏推荐
- 中断操作:AbortController学习笔记
- [oiclass] chess piece
- &nbsp; Difference from spaces
- On the role of database tables
- An article to get you started VIM
- 【直播笔记0629】 并发编程二:锁
- Federal learning: dividing non IID samples by Dirichlet distribution
- SQLite use
- Mysql 带上库名称跨库操作可能出现的问题
- 2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
猜你喜欢
编译一个无导入表的DLL
【微信小程序】条件渲染 列表渲染 原来这样用?
广播模块代码在autojs4.1.1版本运行正常,但在pro7.0版本上运行报错(未解决)
DC/DC变换器轻载时三种工作模式的原理及优缺点
OP-diode-限制摆幅
[live broadcast notes 0629] Concurrent Programming II: lock
Raki's notes on reading paper: named entity recognition as dependency parsing
一篇文章带你入门vim
Heavy attack -- ue5's open source digital twin solution
HOOK Native API
随机推荐
Mysql表数据比较大情况下怎么修改添加字段
华为面试题: 分糖果
Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
备忘一下es6的export/import和类继承的用法
Tri rapide, index groupé, recherche de la plus grande valeur K dans les données
Distributed file storage system fastdfs hands on how to do it
原生JS怎么生成九宫格
golang bilibili直播彈幕姬
Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
【微信小程序】条件渲染 列表渲染 原来这样用?
Auto. JS learning notes 15:ui interface basics of autojs Chapter 2
外汇交易平台哪个好?有监管的资金就安全吗?
Cmake tutorial series-03-dependency management
uniapp 地址转换经纬度
The rigorous judgment of ID number is accurate to the last place in the team
How to prevent duplicate submission under concurrent requests
New edition of diazotization process in 2022 and analysis of diazotization process
正则全匹配:密码由8位以上数字,大小写字母,特殊字符组成
Mysql 带上库名称跨库操作可能出现的问题
Mysqldump principle