Reputation: 551
I have already declared this:
//myclass.h
QVector<int> aux;
QVector< QVector<int> > tests;
//myclass.cpp
aux = (QVector<int>(2));
and it works ok, but now i want to initialize each test QVector dimension into 2 lengths: 20 and 5. Something similar to:
tests = (QVector< QVector<int> >(20)(5));
but this is not Ok. How can initialize it?.
The second question is how to access tests positions using [ ] Is it something similar to this?:
tests[1][4]
Thanks.
Upvotes: 1
Views: 2429
Reputation: 42796
You can initialize it with a size and an object to copy on each one:
//Your .h
QVector<int> aux;
QVector<QVector<int>> tests;
//your .cpp
aux = QVector<int>(2);
tests = QVector<QVector<int>>(20, aux);
Notice that using the aux vector on the tests initialization will not reference(so it will not be modified in the future by accesing tests) it or modify it. This way tests will have 20 QVectors of size 2 inside.
Relating the second question, it should work with tests[x][y] but if not you can just use .at() method
tests.at(x).at(y);
Upvotes: 2