2D matrix of QStrings [closed]

跟風遠走 提交于 2019-12-13 09:36:56

问题


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];
}

What class in Qt should i use?


回答1:


A must-read: Qt container classes.

You could use QVectors or QLists 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);

This will create numberOfFlights vectors, that each contain numberOfSeats empty strings.

To set a specific seat:

matrix[flight][seat] = "whatever";

You can iterate over the vectors with the usual Qt foreach, or iterators, or plain for.



来源:https://stackoverflow.com/questions/6333783/2d-matrix-of-qstrings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!