在 Qt 中,数组的使用方法与 C++ 中的数组类似,但 Qt 还提供了一些更高级的数据结构,例如 QList、QVector、QArray 等。这些数据结构在处理动态数组时更为方便和安全。
下面是一些 Qt 中数组的使用方法以及代码示例。
### 1. 使用 C++ 原生数组
```cpp
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 定义一个原生数组
int arr[5] = {1, 2, 3, 4, 5};
// 输出数组内容
for (int i = 0; i < 5; ++i) {
qDebug() << arr[i];
}
return a.exec();
}
```
### 2. 使用 QList
`QList` 是 Qt 提供的一个动态数组容器,适合存储各种数据类型。
```cpp
#include <QCoreApplication>
#include <QList>
#include <QDebug>
i