繁体   English   中英

向量向量的 C++ OOP 类

[英]C++ OOP Class for vector of vectors

我对 OOP 的概念很陌生,我想创建一个通用类Board ,它可以创建多个不同的矩阵对象。

这是我当前的代码:

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int> >board;

void setup(int board_size) {
    // fill inner vector
    for (int i = 0; i < board_size; i++) {
        vector<int>temp;
        for (int j = 0; j < board_size; j++) {
            temp.push_back(1);
        }
        board.push_back(temp);
    }
}

void display() {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[i].size(); j++) {
            cout << board[i][j];
        }
    cout << endl;
    }
}
int main()
{
    setup(3);
    setup(5);
    display();

    return 0;
}

我想以 OOP 方式改进我的代码,以便我有一个类Board ,它可以使用setup函数创建多个不同的矩阵。

然后我可以使用display函数显示每个矩阵。

例如:

Board board_1;
Board board_2;

board_1.display();
board_2.display();

我不太确定如何实现这一点,任何帮助将不胜感激。 先感谢您。

你快到了,你所要做的就是在你已经拥有的代码周围包装一个类:

#include <iostream>
#include <vector>
using namespace std;
class Board {
vector<vector<int> >board;
public:
Board(int board_size) {//constructor as per suggestion
  setup(board_size);
}

void setup(int board_size) {//setup now also allows you to change the board-size later
    board.clear();//removing existing elements
    // fill inner vector
    for (int i = 0; i < board_size; i++) {
        vector<int>temp;
        for (int j = 0; j < board_size; j++) {
            temp.push_back(1);
        }
        board.push_back(temp);
    }
}

void display() {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[i].size(); j++) {
            cout << board[i][j];
        }
    cout << endl;
    }
}
};

就是这样。 现在你可以这样称呼它:

Board board_1 = Board(3);
Board board_2 = Board(5);


board_1.display();
board_2.display();

//change board-size and display them again
board_1.setup(6);
board_2.setup(10);


board_1.display();
board_2.display();

此外,可能将声明(在 .hpp 文件中)与实现(在 .cpp 文件中)分开

我对您的代码有一些建议:

  1. 使用std::vector意味着棋盘的大小可以在运行时改变,这不应该是真的(在大多数游戏中......)。 为了创建一个固定大小的板,但可以以不同的大小初始化(意思是,你选择一次大小,它不能改变),你可以使用std::array和类模板,这是在编译时确定的,且无法更改!
  2. 您的设置函数实际上是类 Board 的构造函数,因此,与其使用它创建一个板,不如将其设为默认构造函数。

看下面的例子,以及使用示例:

#include <array>
#include <iostream>
#include <stdint.h>

template <uint64_t SIZE>
class Board
{
public:
    Board()
    {
        // Here you initialize the board
        for (uint64_t i = 0; i < SIZE; ++i)
        {
            for (uint64_t j = 0; j < SIZE; ++j)
            {
                board_rep[i][j] = 1;
            }
        }
    };

    void display() const
    {
        for (uint64_t i = 0; i < SIZE; ++i)
        {
            std::cout << "| ";
            for (uint64_t j = 0; j < SIZE; ++j)
            {
                std::cout << board_rep[i][j] << " |";
            }

            std::cout << std::endl;
        }
    }

private:
    std::array<std::array<int, SIZE>, SIZE> board_rep;
};


int main()
{
    Board<3> small_board{};
    Board<5> big_board{};

    std::cout << "small board:\n\n" << std::endl;
    small_board.display();
    std::cout << "big board:\n\n" << std::endl;
    big_board.display();
}

暂无
暂无

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

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