Multidimensional Array and Storing Data

Someone told me that multidimensional in C++ is something that shouldnt be touched, is it true?

Plus I have a question. Im making an RPG, 2d RPG which tile engine. I had been thinking and still have no idea how to implement a map

What I have in mind is that a class map:

class Map
{
private:
int maxX, maxY;

//code that goes here use to store data
public Map(int x, int y)
}

the code that use to store data will generate maxX, maxY tile and set it to default tile which is 0. I believe this will require some sort of array, but I really have no idea how to do this
Someone told me that multidimensional in C++ is something that shouldnt be touched, is it true?
I don't see why that would be true.

I had been thinking and still have no idea how to implement a map ... I believe this will require some sort of array.
A dictionary/map is a standard container in the C++ standard library.
http://www.cplusplus.com/reference/map/map/
I allocate memory using an allocator, which gives me a linear address space. Then I write a macro which indexes into that allowing (x, y, z) type subscript notation. So far, in my C++ code, I've never needed to go beyond three dimensional arrays, and so I have code I could help you with to index into two and three dimensional arrays.

In my work I occasionally need four dimensional arrays, but I haven't yet taken the time to work that macro out in C/C++ syntax. I also code in PowerBASIC, and I believe that language allows up to 8 dimensions, dynamically allocated. In the app that needs four dimensional arrays, I used PowerBASIC.

If you want help or examples wit two or three dimensional arrays, let me know.

As far as I know, the issue with C and C++ is that all of the dimensions can't be allocated at run time. If I'm wrong about that, I hope somebody will tell me too. I know I did spend some time researching this quite a few years ago, and gave up on it with C++. It's my understnding C++ coders use various capabilities from STL and such to deal with this issue, as kbw intimated.
Last edited on
probably due to my misunderstanding (sorry my english is really bad) but let me make sure we are on the same page. The map im talking about is literally, a map. If you had ever played any 2d rpg, you will see your character walk on a bunch of tiles putted together. Those "bunch" of tiles putted together. That is called a map. Plainly speaking, a map is simply a data (sort of like array) that store x and y (coordinate) number that represent a tile.


For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Map

{

private:

int MaxX, MaxY;  // this represent the maximum x and maximum y of a map;

(whatever im trying to do right here)

//the code goes here have to do with data storing, which i absolutely have no idea how to do this

public:

Map(int maxx, int maxy);

void draw();

//other function etc

} 


i want to use multidimensional array as something like this:

int myTiles[MaxX][MaxY]

then have a function that be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

for (int y = 0; y < MaxY; y++)

{

for (int x = 0; x < MaxX; x++)

{

myTiles[x][y] = 0;

}

}


If you can please teach me to use multidimensional array. I only need to use 2 variables x and y.
Last edited on
I didn't think this would work, but it does. Maybe it shows what you are asking ...

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
#include <cstdio>

int main()
{
 int iCols=3,iRows=4;
 int arNums[iRows][iCols];
 arNums[0][0]=2 ,  arNums[0][1]=4,  arNums[0][2]=6;
 arNums[1][0]=8 ,  arNums[1][1]=10, arNums[1][2]=12;
 arNums[2][0]=14 , arNums[2][1]=16, arNums[2][2]=18;
 arNums[3][0]=20 , arNums[3][1]=22, arNums[3][2]=24;
 for(int i=0; i<4; i++)
     printf("%d\t%d\t%d\n", arNums[i][0], arNums[i][1], arNums[i][2]);
 getchar();

 return 0;
}

/*
Output
===================
2       4       6
8       10      12
14      16      18
20      22      24
*/
Topic archived. No new replies allowed.