2D matrix of QStrings [closed]

2019-09-20 00:35发布

问题:

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.



标签: qt list qt4