简体   繁体   English

如何在C ++类中初始化可变大小的2D数组?

[英]How do I initialize a 2D array of variable size in a C++ class?

I am working on a tic tac toe program and I need to create a 2D array of variable size in a class. 我正在研究一个tic tac toe程序,我需要在一个类中创建一个可变大小的2D数组。 This is how I have it written now: 这就是我现在写的方式:

class ticTacToe
{
     public:
         ticTacToe();
         void display();
         bool moveIsValid();
     private:
         int rows;
         int cols;
         int board[rows][col];
}

I have the board being read in from a file in the constructor but I am not sure how to make it of variable size so that I can read in a board of any size and then access it outside of the class. 我从构造函数中的文件中读取了板,但我不知道如何使它变为可变大小,以便我可以在任何大小的板中读取,然后在类之外访问它。

"I have the board being read in from a file in the constructor but I am not sure how to make it of variable size so that I can read in a board of any size" “我从构造函数中的文件读入了电路板,但我不知道如何使它具有可变大小,以便我可以在任何大小的电路板中读取”

In c++ you use a std::vector instead a raw array like follows 在c ++中,您使用std::vector而不是像以下那样的原始数组

class ticTacToe {
     public:
         ticTacToe();
         void display();
         bool moveIsValid();
     private:
         int rows;
         int cols;
         std::vector<std::vector<int>> board; // <<<<
};

The dynamic allocation can be applied as follows in a constructor: 可以在构造函数中按如下方式应用动态分配

ticTacToe(int rows_, int cols_) : rows(rows_), cols(cols_) {
    board.resize(rows,std::vector<int>(cols));
}

and then access it outside of the class 然后在课外访问它

Well, I'm not sure that this is really a good idea, but you can simply add an accessor function for that member variable 好吧,我不确定这是一个好主意,但你可以简单地为该成员变量添加一个访问器函数

 std::vector<std::vector<int>>& accBoard() { return board; }

The better design approach would be IMHO, to provide something like a separate function to read from a std::istream : 更好的设计方法是恕我直言,提供类似于从std::istream读取的单独函数:

 void ticTacToe::readFromStream(std::istream& is) {
     // supposed the first two numbers in the file contain rows and cols
     is >> rows >> cols;
     board.resize(rows,std::vector<int>(cols));
     for(int r = 0; r < rows; ++r) {
         for(int c = 0; c < cols; ++c) {
             cin >> board[r][c];
         }
     }
 }

For real code you would check for input errors of course like 对于实际代码,您当然会检查输入错误

 if(!(is >> rows >> cols)) {
    // handle errors from input
 }

In case this is homework and you cannot use the standard library: 如果这是家庭作业,你不能使用标准库:

// Declaration
int rows;
int columns;
int **board;

// Construction

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

// Destruction


for (int i = 0; i < rows; i++) {
    delete[] board[i];
}
delete[] board;

Update: You could perform a single allocation but it will be easier for you to work it this way. 更新:您可以执行单个分配,但您可以更轻松地以这种方式工作。

You either need to have a dynamically sized array 您需要具有动态大小的数组

int* board;

Then your constructor would be 然后你的构造函数将是

ticTacToe::ticTacToe(int _rows, int _cols)
: rows{_rows}, cols{_cols}
{
    board = new int[rows * cols];
}

And your destructor 而你的析构函数

ticTacToe::~ticTacToe()
{
    delete[] board;
}

Or better yet use a std::vector 或者更好的是使用std::vector

std::vector<int> board;

Then your constructor would be 然后你的构造函数将是

ticTacToe::ticTacToe(int _rows, int _cols)
: rows{_rows}, cols{_cols}
{
    board.resize(_rows * _cols);
}

I suggest you to use pointer to pointer. 我建议你使用指针指针。

#include <iostream>
#include <cstdlib>

using namespace std;

class ticTacToe{
private:
    int rows;
    int cols;
    int **board; // POINTER TO POINTER
public:
    ticTacToe(int a,int b){
        rows=a;
        cols=b;
        board=new int*[rows];

        for (int k = 0; k < rows; ++k) {
            board[k]=new int[cols];
        }

        /*LET'S INITIALIZE CELL VALUES TO= 0*/
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                board[i][j]=0;
            }
        }
    }
    void display();
    bool moveIsValid();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM