-3

I have in my class 2 const int variables:

const int m_width;
const int m_height;

In my constructor, I have set the variables and I want to create a 2D array with exactly this size that will be passed by value from the player. I am trying to make a TicTacToe game. I need the input of the user to determine the size of the playing field(in this case the width and height of it). How do I dynamically declare a 2D array in my situation?

Drise
  • 4,310
  • 5
  • 41
  • 66
  • 1
    Possible duplicate of [How to dynamically allocate arrays in C++](https://stackoverflow.com/questions/35532427/how-to-dynamically-allocate-arrays-in-c) – FrankS101 Apr 16 '18 at 20:43
  • Take input and pass those values as part of object initialization. – Mahesh Apr 16 '18 at 20:44
  • 1
    Welcome to SO. You should use the search box before asking a new question, and also read [tour] and [ask] and [mcve]. – Dave S Apr 16 '18 at 20:45
  • If `vector` of `vector` is off the table for some reason, consider using a matrix class like this one: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op . Note the trick they pull with a 1 dimensional array. It can come in very handy in later projects. – user4581301 Apr 16 '18 at 20:49

2 Answers2

1

It is a common misconception that 2-dimensional matrices should be supported by two-dimensional storage. People often try to use vectors of vectors or other techniques, and this comes at a cost, both performance and code maintainability.

This is not needed. In fact, perfect two-dimensional matrix is a single std::vector, where every row is packed one after each another. Such a vector has a size of of M * N, where M and N are matrix height and width. To access the element at location X, Y, you do v[K], where K is calculated as X * N + Y.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • You know, I never thought of it that way. I was initally concerned that iterating across the array would be a challenge compared to a nested for loop, but then I realized that `for row: for column: do x` is simply just iterating down the vector. Neat! Heck, you could even just write a small accessor function like `item getItem(int x, int y);` that would give you the item at `X, Y` and not have to worry about repeating the maths code everywhere. – Drise Apr 16 '18 at 20:59
0

C++ doesn't provide a standard dynamic 2D array container.

What you can do (if you don't want to write your own full implementation) is use an std::vector of std::vectors instead.

It's not exactly the same thing (provides you with an extra degree of freedom: rows can be of different length) but unless you're working in an extremely constrained environment (or need an extremely optimized solution) the extra cost is not big.

Supposing your elements needs to be integers the code to initialize a 2d array can be for example:

std::vector<std::vector<int>> board(rows, std::vector<int>(cols));

PS: A few years ago I wrote a class here to implement a simple 2D array as an answer to an SO question... you can find it here.

6502
  • 112,025
  • 15
  • 165
  • 265