Best container combination to hold named set of coordinates

Viewed 83

I need to hold a set of up to 50 coordinate (x, y) sets, up to 4 each, so it would look like:

4: {7, 3}, {5, 3}
27: {1, 1}
44: {9, 9}, {9, 7}, {7, 7} and so on

Both x and y will never be higher than 9 nor lower than 0. I ended up with unordered_map<uint8_t, vector<vector<uint8_t>>>, but almost sure that its inefficient. Also, it will be generated over a cycle with a bunch of if's and deleted after few reads.

2 Answers

You can just use a multidimensional vector, as such:

vector <pair<int, int>> coordinates[50];

What this declaration does it it declares an array of 50 vectors containing pairs of integers. The pairs of integers would represent an x and a y-coordinate in your case.

If you know about x and y (0 <= x <= 9), you can use bit field.

And I would store data like this

struct Pair {
  uint8_t x:4;
  uint8_t y:4;
};

unordered_map<uint8_t, vector<Pair>> container;
Related