# Qt/C++ - Lesson 037. The two-dimensional array using QVector

Class QVector relates to a container class, and provides access to the items on the index, as well as a number of additional methods for ease of operation.

QVector instance of the class is essentially a one-dimensional array of objects. If you want to set as a vector of two-dimensional array, you can create an instance of a QVector , which will contain other instances QVector .

## One-Dimensional array using QVector

For starters keep in Vector-dimensional array of type int:

```QVector <int> myVector;

int massive[4] = {1, 2, 3, 4};

for(int i = 0; i < 4; i++)
{
myVector.push_back(massive[i]);
qDebug() << "Value " << i << ": " << myVector.value(i);
}```

And look at the output qDebug() :

```Value  0 :  1
Value  1 :  2
Value  2 :  3
Value  3 :  4```

## The two-dimensional array in QVector

Now put in a two-dimensional vector array of int:

```QVector <QVector <int> > myVector;

int massive[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };

for(int i = 0; i < 4; i++)
{
QVector<int> tempVector;

for(int j = 0; j < 4; j++)
{
tempVector.push_back(massive[i][j]);
qDebug() << "Value " << j << ": " << tempVector.value(j);
}
myVector.push_back(tempVector);
qDebug() << "myVector " << i << ": " << myVector.value(i);
}```

And look at the output qDebug() :

```Value  0 :  1
Value  1 :  2
Value  2 :  3
Value  3 :  4
myVector  0 :  QVector(1, 2, 3, 4)
Value  0 :  5
Value  1 :  6
Value  2 :  7
Value  3 :  8
myVector  1 :  QVector(5, 6, 7, 8)
Value  0 :  9
Value  1 :  10
Value  2 :  11
Value  3 :  12
myVector  2 :  QVector(9, 10, 11, 12)
Value  0 :  13
Value  1 :  14
Value  2 :  15
Value  3 :  16
myVector  3 :  QVector(13, 14, 15, 16)```

## An array of two-dimensional arrays using QVector

And if you want to keep all of two-dimensional arrays again in the vector, it can be done as follows:

```QVector <QVector <QVector <int> > > myVector;

int massive[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };

QVector <QVector <int> > matrix;

for(int i = 0; i < 4; i++)
{
QVector<int> tempVector;

for(int j = 0; j < 4; j++)
{
tempVector.push_back(massive[i][j]);
qDebug() << "Value " << j << ": " << tempVector.value(j);
}
matrix.push_back(tempVector);
qDebug() << "matrix row " << i << ": " << matrix.value(i);
}

myVector.push_back(matrix);

qDebug() << "myVector: " << myVector.value(0);```

And look at the output qDebug() :

```Value  0 :  1
Value  1 :  2
Value  2 :  3
Value  3 :  4
matrix row  0 :  QVector(1, 2, 3, 4)
Value  0 :  5
Value  1 :  6
Value  2 :  7
Value  3 :  8
matrix row  1 :  QVector(5, 6, 7, 8)
Value  0 :  9
Value  1 :  10
Value  2 :  11
Value  3 :  12
matrix row  2 :  QVector(9, 10, 11, 12)
Value  0 :  13
Value  1 :  14
Value  2 :  15
Value  3 :  16
matrix row  3 :  QVector(13, 14, 15, 16)
myVector:  QVector(QVector(1, 2, 3, 4), QVector(5, 6, 7, 8), QVector(9, 10, 11, 12), QVector(13, 14, 15, 16))```

## Conclusion

And finally, another way to work with vectors and arrays on the example of two matrices. which is somewhat different from the above the given methods. In this case, a matrix or two-dimensional array will contain one QVector <int *> . In turn, vector QVector <QVector <int *> > will contain a list of all matrices.

```QVector <QVector <int *> > matrixList;
QVector <int *> matrix1;
QVector <int *> matrix2;

int massive1[2][4] = { {1,2,3,4}, {5,6,7,8} };
int massive2[2][4] = { {9,10,11,12}, {13,14,15,16} };

qDebug() << "Matrix 1";
for(int i = 0; i < 2; i++)
{
matrix1.push_back(massive1[i]);
for(int j = 0; j < 4; j++)
{
qDebug() << "[" << i << "]" << "[" << j << "]" << matrix1.value(i)[j];
}
}

qDebug() << "Matrix 2";
for(int i = 0; i < 2; i++)
{
matrix2.push_back(massive2[i]);
for(int j = 0; j < 4; j++)
{
qDebug() << "[" << i << "]" << "[" << j << "]" << matrix2.value(i)[j];
}
}

matrixList.push_back(matrix1);
matrixList.push_back(matrix2);

qDebug() << "Matrix 1 from matrixList";
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 4; j++)
{
qDebug() << "[" << i << "]" << "[" << j << "]" << matrixList.value(0).value(i)[j];
}
}

qDebug() << "Matrix 2 from matrixList";
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 4; j++)
{
qDebug() << "[" << i << "]" << "[" << j << "]" << matrixList.value(1).value(i)[j];
}
}```

And look at the output qDebug() :

```Matrix 1
[ 0 ] [ 0 ] 1
[ 0 ] [ 1 ] 2
[ 0 ] [ 2 ] 3
[ 0 ] [ 3 ] 4
[ 1 ] [ 0 ] 5
[ 1 ] [ 1 ] 6
[ 1 ] [ 2 ] 7
[ 1 ] [ 3 ] 8
Matrix 2
[ 0 ] [ 0 ] 9
[ 0 ] [ 1 ] 10
[ 0 ] [ 2 ] 11
[ 0 ] [ 3 ] 12
[ 1 ] [ 0 ] 13
[ 1 ] [ 1 ] 14
[ 1 ] [ 2 ] 15
[ 1 ] [ 3 ] 16
Matrix 1 from matrixList
[ 0 ] [ 0 ] 1
[ 0 ] [ 1 ] 2
[ 0 ] [ 2 ] 3
[ 0 ] [ 3 ] 4
[ 1 ] [ 0 ] 5
[ 1 ] [ 1 ] 6
[ 1 ] [ 2 ] 7
[ 1 ] [ 3 ] 8
Matrix 2 from matrixList
[ 0 ] [ 0 ] 9
[ 0 ] [ 1 ] 10
[ 0 ] [ 2 ] 11
[ 0 ] [ 3 ] 12
[ 1 ] [ 0 ] 13
[ 1 ] [ 1 ] 14
[ 1 ] [ 2 ] 15
[ 1 ] [ 3 ] 16```

##### We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author
M

Помогите пожалуйста с вектором, не имеющим ограничений по количеству элементов. Создаю и добавляю элементы, пока не ругается компилятор.

``` QVector<double>*Open1=   new QVector<double>;
Open1->append(5); Open1->append(6);```
При этом не работает так Open1[0]=7; Также при попытке вывода Open1[0] выдает ошибку. Скажите пожалуйста, где у меня ошибка?

Выделили память в куче. Обращаться к элементам нужно так.

```Open1->at(0);
Open1->at(1);```
M
`Open1->at(5); Open1->at(6); Сделал так, при запуске вырубается скрипт.`

Only authorized users can post comments.
##### How to become an author?

Contribute to the evolution of the EVILEG community.

Learn how to become a site author.

##### Donate

Good day, Dear Users!!!

I am Evgenii Legotckoi, developer of EVILEG. And it is my hobby project, which helps to learn programming another programmers and developers

If the site helped you, and you want also support the development of the site, than you can donate by following ways

##### Timeweb

Let me recommend you the excellent hosting on which EVILEG is located.

For many years, Timeweb has been proving his stability.

For projects on Django I recommend VDS hosting

n
June 5, 2020, 2:28 a.m.
###### n1k0m1

Qt - Test 001. Signals and slots

• Result:0points,
• Rating points-10
s
June 3, 2020, 1:56 a.m.
###### silo1995

C++ - Тест 003. Условия и циклы

• Result:35points,
• Rating points-10
AP
June 2, 2020, 9:11 p.m.
###### Aleksej Pikenin

C++ - Test 005. Structures and Classes

• Result:75points,
• Rating points2
June 5, 2020, 10:52 a.m.
###### progammist

Распознавание изображений на Python с помощью TensorFlow и Keras

Огромное спасибо за метериал, по-больше бы подобных статей (с подробным описанием работы и примерами применения). Вопрос поразмышлять. На текущий момент реализовано немало технологий в осно…
June 5, 2020, 1:39 a.m.
###### Evgenij Legotskoj

Qt/C++ - Tutorial 091. How to write a custom delegate controlling the highlighting of a row in a table

По-моему, смысла в этом нет особого. Если делегат будет игнорировать настройки таблицы, то это приведёт ещё к большему непониманию, что вообще происходит, для программиста, который после вас буд…
June 5, 2020, 1:34 a.m.
###### IscanderChe

Qt/C++ - Tutorial 091. How to write a custom delegate controlling the highlighting of a row in a table

Сижу, размышляю: можно ли переписать делегата так, чтобы независимо от настроек строк выделялись строки?
June 5, 2020, 1:31 a.m.
###### Evgenij Legotskoj

Qt/C++ - Tutorial 091. How to write a custom delegate controlling the highlighting of a row in a table

Понятно. Я не обратил внимания на то, что там было в старом коде по настройкам строк :)
June 5, 2020, 1:27 a.m.
###### IscanderChe

Qt/C++ - Tutorial 091. How to write a custom delegate controlling the highlighting of a row in a table

Разобрался. У вас изначально в проекте были вот эти настройки: ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);ui->tableView->setSelectionMode(QAbstractItemVie…
Now discuss on the forum
June 5, 2020, 6:13 a.m.
###### IscanderChe

Фильтр для QtableView sql

Добрый день. Для такой фильтрации необходимо использовать QSortFilterProxyModel. В оффдоках есть хороший пример.
MA
June 4, 2020, 2:46 a.m.
###### Mihail A

Qt- C++ QTableView подсветить строку

Спасибо.
f
June 3, 2020, 1:49 a.m.
###### fryn3

Можно ли сделать в QML таблицу как в Excel?

edi-tableview - нашел пока такое выглядит коряво, посмотрим что можно сделать
June 2, 2020, 2:46 a.m.