如何在C ++中的Heap上创建二维数组?

时间:2014-01-15 13:16:25

标签: c++ arrays

我正在尝试用C ++进行国际象棋模拟。我创建了一个类Pieces,我想在堆上创建一个包含所有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]’

如何在堆上成功分配二维数组? 还有什么比创建一个Pointers to Pieces或创建一个Pieces Objects数组更好?

5 个答案:

答案 0 :(得分:3)

为什么需要所有动态分配?只需要一个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.
}

Live demo

更简单,更安全。 : - )

答案 1 :(得分:1)

您只需要进行两项简单的更改。董事会声明应为:

  Pieces (*board)[8]  = new Pieces[8][8];

换句话说,board是指向(一个数组)8元素数组的指针。然后分配应该是:

  board[i][j] = *startup[k];

请注意,该板是一个二维的Pieces数组 - 不是一个指针数组,这可能是你真正想要的。

答案 2 :(得分:0)

像这样创建你的电路板数组

Pieces** board=new Pieces*[8];
for (int i=0; i<8; i++)
{
   board[i] = new Pieces[8];
}

答案 3 :(得分:0)

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为类型**时可能发生的额外的间接层。语法当然是相同的,但内存布局明显不同。

示例:

#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 

答案 4 :(得分:0)

以下作品:

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];

内存管理减少了。