Skip to content

Instantly share code, notes, and snippets.

@victorholt
Created March 14, 2021 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorholt/78e3019a2e6d7a6c3d07d155c951caee to your computer and use it in GitHub Desktop.
Save victorholt/78e3019a2e6d7a6c3d07d155c951caee to your computer and use it in GitHub Desktop.
Simple Array 1D to Array 2D and back
#include <iostream>
#include <string>
#include <vector>
static std::vector<int> map;
static int mapSize = 3;
int initMap()
{
for (int i = 0; i < (mapSize * mapSize); i++) {
map.push_back(0);
}
}
int setMapValue(int index, int value)
{
auto nx = index % mapSize;
auto ny = index / mapSize;
auto mIndex = nx + ny * mapSize;
map[mIndex] = value;
std::cout << "Setting Map Data at (" << nx << ", " << ny << ")" << std::endl;
}
void printMap()
{
for (int y = 0; y < mapSize; y++) {
std::cout << "[ ";
for (int x = 0; x < mapSize; x++) {
std::cout << map[x + y * mapSize] << " ";
}
std::cout << "]" << std::endl;
}
}
int main()
{
initMap();
setMapValue(0, 5);
printMap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment