I am experimenting with vectors for the first time. So, to experiment, I created a class that constructs a 2d vector of integers and initializes them with numbers 0 through 9 in this order.
Here's the example I created:
#include <iostream>
#include <vector>
class VectorTest
{
private:
std::vector< std::vector<int> > m_v;
public:
VectorTest(int x, int y)
{
m_v.resize(y);
for (auto &element : m_v)
element.resize(x);
int count {0};
for (int i {0}; i < m_v.size(); ++i)
{
for (int j {0}; j < m_v[i].size(); ++j)
m_v[i][j] = count++ % 10;
}
}
}
void print()
{
for (int i {0}; i < m_v.size(); ++i)
{
for (int j {0}; j < m_v[i].size(); ++j)
{
std::cout << m_v[i][j];
}
std::cout << '\n';
}
}
void print(int x, int y)
{
std::cout << m_v[y][x];
}
};
int main()
{
VectorTest v(9, 9);
v.print();
std::cout << '\n';
v.print (6, 4);
return 0;
}
My question is: how can I modify this program to make m_v[x][y]
instead of m_v[y][x]
basically switch around the row and column indexes in the vector?
Considering that x is a row and y is a column, how can I make the inner vector store the columns and the outside vector store the rows? Because right now I have to access the coords as m_v[y][x]
but I wanted this to be accessed as m_v[x][y]
.