如何在 C++ 中的堆上创建二维数组
How can I create a 2 dimensional array on Heap in C++?
我正在尝试用C++进行国际象棋模拟。我已经创建了一个类 Pieces,我想在堆上创建一个由所有 Pieces 组成的二维数组。这是我的代码:国王,王后和其他人都来自碎片。
king = new King();
queen = new Queen();
knight = new Knight();
bishop = new Bishop();
rook = new Rook();
pawn = new Pawn();
empty = new Pieces();
Pieces* startup[64] = {rook, knight, bishop, king, queen, bishop, knight, rook,
pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
empty, empty, empty, empty, empty, empty, empty, empty,
empty, empty, empty, empty, empty, empty, empty, empty,
empty, empty, empty, empty, empty, empty, empty, empty,
empty, empty, empty, empty, empty, empty, empty, empty,
pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
rook, knight, bishop, king, queen, bishop, knight, rook};
Pieces* board = new Pieces[8][8];
int k = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = startup[k];
k++;
}
}
但这给了我以下错误:
ChessBoard.cpp: In constructor ‘ChessBoard::ChessBoard()’:
ChessBoard.cpp:25: error: cannot convert ‘Pieces (*)[8]’ to ‘Pieces*’ in initialization
ChessBoard.cpp:29: error: no match for ‘operator[]’ in ‘board[i][j]’
如何在堆上成功分配二维数组?还有什么更好的是创建一个指向片段的指针数组或创建一个片段对象数组?
为什么需要所有动态分配?只需拥有一个 64 元素的枚举数组,并在顶部添加一些伪造的 2D 索引。
下面是一个示例:
#include <array>
#include <iostream>
enum class piece_t
{
EMPTY = 0, PAWN, ROOK, BISHOP, KNIGHT, QUEEN, KING
};
static const size_t WIDTH = 8, HEIGHT = 8;
struct board_t : std::array<piece_t, WIDTH*HEIGHT>
{
board_t()
{
for (size_t y = 0; y < HEIGHT; y++)
for (size_t x = 0; x < WIDTH; x++)
operator()(x,y) = piece_t::EMPTY;
}
piece_t& operator()(size_t x, size_t y)
{
return operator[](x + y*WIDTH);
}
const piece_t& operator()(size_t x, size_t y) const
{
return operator[](x + y*WIDTH);
}
};
std::ostream& operator<<(std::ostream& os, const piece_t& piece)
{
switch (piece) {
case piece_t::KING: return (os << 'K');
case piece_t::QUEEN: return (os << 'Q');
case piece_t::KNIGHT: return (os << 'N');
case piece_t::BISHOP: return (os << 'B');
case piece_t::ROOK: return (os << 'R');
case piece_t::PAWN: return (os << 'P');
case piece_t::EMPTY: return (os << ' ');
default: return (os << '?');
}
}
std::ostream& operator<<(std::ostream& os, const board_t& board)
{
os << '+' << std::string(WIDTH, '-') << '+' << 'n';
for (size_t y = 0; y < HEIGHT; y++) {
os << '|';
for (size_t x = 0; x < WIDTH; x++)
os << board(x, y);
os << '|' << 'n';
}
os << '+' << std::string(WIDTH, '-') << '+' << 'n';
return os;
}
int main()
{
board_t b;
b(3, 5) = piece_t::KING;
b(6, 4) = piece_t::KNIGHT;
std::cout << b << std::endl;
// etc.
}
现场演示
更简单,更安全。
你只需要两个简单的更改。董事会声明应为:
Pieces (*board)[8] = new Pieces[8][8];
换句话说,board
是指向 8 元素数组(数组)的指针。那么分配应该是:
board[i][j] = *startup[k];
请注意,该板是 Pieces 的二维数组 - 而不是指针数组,这也许是您真正想要的。
像这样创建电路板数组
Pieces** board=new Pieces*[8];
for (int i=0; i<8; i++)
{
board[i] = new Pieces[8];
}
std::array<std::array<Pieces, 8>, 8>
可能是定义电路板的最简洁方法。
如果不能使用 std::array,则可以先将二维数组分配为一个连续块,然后让二维指针指向该缓冲区,如下所示:
Pieces * buffer = new Pieces [8 * 8];
Pieces (*board)[4] = reinterpret_cast<Pieces (*)[8]>(buffer);
...
delete [] buffer;
这允许您使用正常的二维索引,而不必经历 P 类型为 Pieces ** 时会发生的额外间接层。语法当然是相同的,但内存布局明显不同。
例:
#include <iostream>
int main()
{
int * buffer = new int [7 * 4];
for( int i = 0; i < 28; ++i )
buffer[i] = i;
int (*P)[4] = reinterpret_cast<int (*)[4]>(buffer);
for( int i = 0; i < 7; ++i )
{
for( int j = 0; j < 4; ++j )
std:: cout << P[i][j] << ' ';
std::cout << std::endl;
}
delete [] buffer;
}
输出:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
20 21 22 23
24 25 26 27
以下作品:
Pieces* (*board)[8][8] = new Pieces*[1][8][8];
int k = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
(*board)[i][j] = startup[k];
k++;
}
}
但ChessBoard
可能直接拥有成员:
Pieces* board[8][8];
因此减少了内存管理。
相关文章:
- 创建 std::string 的二维数组的最佳做法
- 在创建动态二维数组时,在A[i*c+j]中使用c有什么用,即c代表什么
- 如何在C++中创建二维数组的数组
- C++基于用户输入字符串长度创建二维数组
- 如何在 C++ 中的堆上创建二维数组
- 动态创建具有默认值的二维数组C++
- 如何在C++中创建给定大小的二维数组
- 如何为二维数组创建类
- C++:创建结构的二维数组
- 如何创建一个一维数组来容纳几个二维数组
- 创建二维数组
- 如何为抽象类创建二维数组
- 创建结构的二维数组会导致崩溃
- 用C++创建二维数组,并在MATLAB中使用
- 使用输入值创建二维数组的大小
- 为什么在尝试用c++创建二维数组时会出现此错误
- 如何创建二维数组
- 创建二维数组的两种方法的区别
- 如何从模板结构创建二维数组
- 如何使用基本循环创建二维数组矩阵