1

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].

msmilkshake
  • 214
  • 1
  • 13
  • Hi msmilkshake, welcome to SO. What do you mean by `make the console print them correctly?`. What is the expected correct output? – Bhavin May 25 '19 at 15:03
  • I am just going to remove that part. – msmilkshake May 25 '19 at 15:04
  • You could add a bool to your class that indicates the indices should be swapped then test it and swap the order in the class methods. A better approach, with much higher performance is to use a single vector in the class but that's another discussion. There are examples on SO of that. – doug May 25 '19 at 15:14
  • @doug Can I make a 2d array with a single vector? What do you mean by a single vector in the class? – msmilkshake May 25 '19 at 15:16
  • The problem with vectors of vectors is that each vector stores it's elements on an allocated memory block so you go through multiple indirections accessing it. See this example of a 2D matrix with a single vector. https://stackoverflow.com/questions/36123452/statically-declared-2-d-array-c-as-data-member-of-a-class/36123944#36123944 However, a better approach is to use function arguments instead of `[][]` – doug May 25 '19 at 15:20
  • @doug Ohh, I see... – msmilkshake May 25 '19 at 15:28
  • @doug so one vector stores all the values and then we get the row / col by doing some math. Clever that! – msmilkshake May 25 '19 at 15:36
  • It's pretty much the idiomatic way to avoid layered vector overhead. However, indexing using function arguments is more flexible in C++ and it lets you re-order easily as well as facilitating adding bounds error checks and such. This is usually done by adding `operator()` to your class with arguments for however many dimensions you have. – doug May 25 '19 at 15:49

1 Answers1

0

Why don't just swap places "x" and "y" in constructor? Nothing else is needed.

Or I misunderstood the problem?

    VectorTest(int x, int y)
    {
        m_v.resize(x /* was "y" */);
        for (auto &element : m_v)
            element.resize(y /* was "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;
            }
        }
    }
Livers47
  • 1
  • 2