QStrings的2D矩阵 [英] 2D matrix of QStrings
问题描述
我想提出一个可以预订机票的应用程序.实际上,我正在为航空公司设计系统.当我想创建一个数据库(一个保存飞机座位数的2D矩阵)时,它给了我错误.
I want to make an application with which you can reserve ticket for your travel. In fact, I'm designing the system for an airline. When I want to create a database (a 2D matrix that saves the number of seats in flights), it gives me errors.
在不同位置设置的航班数量,该数量正在更改,这是我的代码:
The number of flights set in different place and the number is changing this is my code:
QString** matrix = new QString*[numberofFlights];
for (int i = 0; i < numberofFlight; i++)
{
matrix[i] = new QString[numberofSeats];
}
我应该在Qt中使用哪个班级?
What class in Qt should i use?
推荐答案
必读: Qt容器类.
您可以使用QVector
s或QList
s或其他容器类.例如,要构建向量的向量:
You could use QVector
s or QList
s or another container class. For example, to build a vector of vectors:
QVector< QVector<QString> > matrix(numberOfFlights);
for (int i=0; i<numberOfFlights; i++)
matrix[i].fill("", numberOfSeats);
这将创建numberOfFlights
个向量,每个向量包含numberOfSeats
个空字符串.
This will create numberOfFlights
vectors, that each contain numberOfSeats
empty strings.
要设置特定的座位,请执行以下操作:
To set a specific seat:
matrix[flight][seat] = "whatever";
您可以使用普通的Qt foreach
或迭代器或普通的for
遍历向量.
You can iterate over the vectors with the usual Qt foreach
, or iterators, or plain for
.
这篇关于QStrings的2D矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!