保存命名坐标集的最佳容器组合

问题描述 投票:1回答:2

我需要保存一组多达50个坐标(x,y)的集合,每个多达4个,因此看起来像:

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

x和y都不会大于9也不小于0。我最终得到unordered_map<uint8_t, vector<vector<uint8_t>>>,但几乎可以确定其效率不高。同样,它将在一个循环中生成,并带有一堆if,并在几次读取后被删除。

c++ optimization containers
2个回答
1
投票

如果您知道x和y(0 <= x <= 9),则可以使用bit field

而且我会像这样存储数据

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

unordered_map<uint8_t, vector<Pair>> container;

1
投票

您可以仅使用多维矢量,例如:

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

此声明的作用是声明array为50 vectors,其中包含pairs of integers。在您的情况下,成对的整数将表示x和y坐标。

© www.soinside.com 2019 - 2024. All rights reserved.