How to use Qvector<Qvector <double> as matrix?

Asked

Viewed 196 times

3

In the mainwindow.h have:

private:
QVector<QVector<double> > numbers; //Variável que será minha matriz
public slot:
void realizar_calcs(QVector<QVector<double> > &numbers);

In the mainwindow.cpp

void MainWindow::realizar_calcs(QVector<QVector<double> > &numbers)
{
   int n1 = 10;
   numbers.resize(n1);
   for(int i = 0;i < n1;i++)
   {
      numbers[i].resize(n2);
   }
}

My difficulty is in its initialization and the passage to function!

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

5

An array is not meant to behave like an array. Of course there are some solutions.

I found a solution that seems to approach what you want, see if it helps. It shows how to initialize the vector as if it were a matrix.

There is a class in Qt for this but it was considered obsolete and as far as I know nothing was created to replace it. but it has a more modern class in version 4.6 onwards.

Another obvious option is to look for a class that serves you in another library. It might not be as simple to integrate with Qt.

There is still the possibility of creating a class of your own that works as a matrix. This can bring some advantages of integration with Qt depending on how you do it.

I found a gambit in that reply in the OS:

int index(int x, int y) {
    return x + width * y;
}

So you simulate a matrix within the vector:

QVector<...> vector(width * height);
vector[index(5, 3)] = ...;

I put in the Github for future reference.

  • It might help, but for my algorithm it would be very confusing to apply a matrix this way. I saw right here that doing so Qvector applies as matrix, my difficulty is in its initialization and as input arguments.

  • Your gambiarra would look good if they were static, but it is uses here allocations. But still thank you very much

  • @carlosgoes2008 did you see the first solution? Ali does the boot more or less the way you need it (I think).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.