简体   繁体   English

改进 C++ 中的二维数组

[英]improve 2D array in C++

This is may code for 2D array.这是二维数组的可能代码。 I will using it for Battleship game.我将它用于战舰游戏。 I want to ask, if its necessary to use a dynamic memory because I have two 2D array.我想问一下,是否有必要使用动态内存,因为我有两个二维数组。 Also I want to ask if anyone can tell my how to improve or fix my code另外我想问一下是否有人可以告诉我如何改进或修复我的代码

I'm not sure that I used the Class correct, but its my first time我不确定我是否正确使用了 Class,但这是我第一次使用

enter code here在此处输入代码

#include <iostream>
#include<array>
#include <string>
#include <stdlib.h>
#include <vector>

using namespace std;

class Board {

public:

int grid[15][15];





int initBoard(int A, int B) 
{   

int grid[A][B];
for(int col=0; col<A; col++) //Outer column loop
{
    for(int row=0; row<B; row++) //Inner row loop
    {
        grid[col][row]=0;
    }
}
}

void printBoard(int A, int B)  
{

    if (B == 10){
        cout<<endl;
     cout<<"       ---{WELCOME TO THE BATTLESHIP}---\n"<<endl;
 cout << "    0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |" << endl << endl;
    }
    else if (B == 15){
        cout<<endl;
        cout<<"                ---{WELCOME TO THE BATTLESHIP}---\n"<<endl;
        cout << "    0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14|" << endl << endl;

    }
for(int i=0; i<A; i++)  //column loop
{

    for(int j=0; j<B; j++)  //row loop
    {
        if(j==0)
        {
                        if(i<=9){
                                cout << i << "   " ; //print row number and spaces before new row
                        }
                        else if(i>=10){
                                cout << i << "  " ;
                        }

                    }

                    grid[i][j]=0;
        cout << grid[i][j] ;
        if(j <= B)
        {
            cout << " | ";
        }

    }
cout << endl; //new line at end of column
}
cout << endl;
}



}:
int main(){



system("clear");
cout<<endl;
cout<<"***------GAME MENU------***"<<endl<<endl;
int b;
string name;
string surname;
cout<<"    Choose Difficulty\n\n"<<"        1 for Easy\n"<<"        2 for 
Hard\n"<<endl;
cout<<"***---------------------***"<<endl<<endl;
cout<<"Enter your name: ";
cin>>name;
cout<<"Enter your surname: ";
cin>>surname;
cout<<"Enter your choice -->";
cin>>b;
   system("clear");

if (b==1){

    Board easy;
easy.initBoard(10,10);
easy.printBoard(10,10);
  cout<<endl;
 }

else if(b==2){
  Board hard;
hard.initBoard(15,15);
hard.printBoard(15,15);
  cout<<endl;
}



return 0;
}

if its necessary to use a dynamic memory because I have two 2D array.如果有必要使用动态内存,因为我有两个二维数组。

On my system, Ubuntu 17.10, each thread stack size is 8 MB (default).在我的系统 Ubuntu 17.10 上,每个线程堆栈大小为 8 MB(默认)。 You need to figure out your per thread auto-var size, and if your grid is smaller, and enough size is left for max depth function calls, you will not need to use dynamic memory.您需要确定每个线程的自动变量大小,如果您的网格更小,并且为最大深度函数调用保留了足够的大小,则不需要使用动态内存。

Generally, I would not resist using dynamic memory.一般来说,我不会抗拒使用动态内存。

Because I have 2D array因为我有二维数组

The dimensionality has no impact.维度没有影响。 The grid (made with std::array) takes a fixed number of bytes, and the elements are back to back.网格(用 std::array 制作)采用固定数量的字节,元素是背靠背的。

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

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