1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <vector>
int main()
{
size_t row_size = 4;
size_t col_size = 3;
// or let the user supply row and column size
// a multidimensional vector of a set size
std::vector<std::vector<short>> aVector(row_size, std::vector<short>(col_size));
aVector = { { -501, 206, 2011 }, { 989, 101, 206 }, { 303, 456, 596 }, { 123, 543, 108 } };
for (size_t row = 0; row < row_size; row++)
{
std::cout << "Row " << row << ": ";
for (size_t col = 0; col < col_size; col++)
{
std::cout << aVector[row][col] << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
// with an initializer list creating a multidimensional vector is easy
std::vector<std::vector<short>> myVector = { { -501, 206, 2011 }, { 989, 101, 206 }, { 303, 456, 596 }, { 123, 543, 108 } };
for (size_t row = 0; row < row_size; row++)
{
std::cout << "Row " << row << ": ";
for (size_t col = 0; col < col_size; col++)
{
std::cout << myVector[row][col] << ' ';
}
std::cout << '\n';
}
}
|