我正在尝试重构以下依赖于经典 C 样式数组的代码,使其更像 C++:
fb::Block *blocks[Panel::X][Panel::Y];
void Panel::mechanics(int64_t tick) {
for (int32_t y = 1; y < Panel::Y; y++) {
for (int32_t x = 0; x < Panel::X; x++) {
fb::Block* current = blocks[x][y];
if (current -- nullptr) {
continue;
}
// Switching the 2 pointers
blocks[x][y] = blocks[x + 1][y];
blocks[x + 1][y] = current;
}
}
}
以下代码依赖于std::array
and std::unique_ptr
。我需要像在前面的代码中那样交换 2 个值,但我不确定这是否是正确的方法:
std::array<std::array<std::unique_ptr<fb::Block>, Panel::Y>, Panel::X> blocks;
void Panel::mechanics(int64_t tick) {
for (int32_t y = 1; y < Panel::Y; y++) {
for (int32_t x = 0; x < Panel::X; x++) {
std::unique_ptr<fb::Block>& current = blocks[x][y];
if (!current) {
continue;
}
// Swapping the 2 pointers
blocks[x][y].swap(blocks[x + 1][y]);
}
}
}
会员是swap
实现这一目标的正确方法吗?