简体   繁体   中英

Initialize and access <QVector< QVector<int> > QT5

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM