我有一个四/八树数据结构。我将一个单元格的子索引/ ptrs存储在一个数组中。数组中的每个位置代表一个孩子相对于其父母的位置,例如在2D中:
// _____________
// | | |
// | 2 | 3 |
// |_____|_____|
// | | |
// | 0 | 1 |
// |_____|_____|
// for each cell, 4 children are always stored in row-major order
std::vector<std::array<Integer,4>> children;
我知道孩子的最大数量是一个值的子集 Integer
类型可以代表。因此,我可以通过使用像'魔术'这样的值来识别一个细胞是否遗漏了一个孩子 -1
对于 Integer = int
, 要么 std::numeric_limits<unsigned>::max()
对于 Integer = unsigned
。这是那样的 std::optional<Integer>
不能假设。
据我所知,这种魔法价值的使用是其中一个存在的理由 std::optional
。不过,我担心的是表现 std::vector<std::optional<int>>
在内循环中。
所以,
请问表现
std::vector<std::optional<int>>
比那更糟糕std::vector<int>
? (我已经在对“不存在的”值进行比较了)。或者,可以执行
std::optional
经过优化,可提供与原始相同的性能int
?如何?
混合 std::optional
在我的函数的返回类型和我的数据结构中的魔术值听起来是一个非常糟糕的主意。我更喜欢保持一致,要么使用其中一个(至少在同一个上下文中)。虽然我可以重载与幻数进行比较的函数:
template<T> bool is_valid(const T& t) {
return /* comparison with magic value for t */;
}
对于可选类型。