如何将2d数组对象传递给c++中的函数

how to pass 2d array object to function in c++?

本文关键字:c++ 函数 2d 数组 对象      更新时间:2023-10-16

我有一个"spot"类的2D固定大小对象数组点图[row][col]//行&col是动态更改的整数我想把它传递给一个函数bool isFilled(int row,int col,Spot[row][col]){}如何定义函数?如何删除此数组?请查看我的代码。谢谢你的帮助。

Spot.h
#ifndef SPOT_H
#define SPOT_H
class Spot
{
private:
bool isBunny;
int nextCycle;
public:
static const int UP = 0;
static const int RIGHT = 1;
static const int DOWN = 2;
static const int LEFT = 3;
static const int SLEEP = 4;
virtual void setSpot(bool newIsBunny);
Spot();
~Spot();
virtual int getNextCycle();
virtual void setNextCycle();
virtual bool getIsBunny();
virtual void makeBunny();
};
void Spot::setSpot(bool newIsBunny)
{
isBunny = newIsBunny;
nextCycle = UP;
}
Spot::Spot()
{
isBunny = false;
nextCycle = UP;
}
Spot::~Spot()
{
}
void Spot::setNextCycle()
{
if (nextCycle != SLEEP)
{
nextCycle++;
}
}
int Spot::getNextCycle()
{
return nextCycle;
}
bool Spot::getIsBunny()
{
return isBunny;
}
void Spot::makeBunny()
{
if (!isBunny)
nextCycle = UP;
isBunny = true;
}

#endif  /* SPOT_H */

Bunny.cpp

#include "Spot.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <boost/algorithm/string.hpp>

using namespace std;

static string line;
static ifstream inFile;
static ofstream outFile;
bool isFilled(int x, int y, Spot **myMap);
int main () {
int numSims = 0;
inFile.exceptions ( ifstream::failbit | ifstream::badbit );
try {
inFile.open ("/home/mike/Desktop/input.txt");
outFile.open ("/home/mike/Desktop/output.txt");
// while(!inFile.eof())
{
getline (inFile,line);
numSims= atoi(line.c_str());
//cout<<"numSims: "<<numSims<<endl;
for (int i = 0;i < numSims;i++)
{
int minPerCycle = 1;
getline (inFile,line);
minPerCycle= atoi(line.c_str());
//cout << "minPerCycle: "<<minPerCycle <<endl;
int row = 0;
int col = 0;
getline (inFile,line);
std::vector<std::string> xy;
boost::split(xy, line, boost::is_any_of(" "));
row=atoi(xy.at(0).c_str());
col=atoi(xy.at(1).c_str());
//cout <<"row: "<< row<<endl;
//cout << "col: "<<col<<endl;

Spot** myMap = new Spot* [col];
for(int i = 0; i < col; ++i)
myMap[i] = new Spot [row];
//std::vector<std::vector<Spot> > myMap(x, std::vector<Spot>(y));
for (int i = 0;i < row;i++)
{
getline (inFile,line);
//cout<<line<<endl;
for (int j = 0;j < col;j++)
{
if (line[j] == 'B')
{
myMap[i][j].setSpot(true);
}
else
{
myMap[i][j].setSpot(false);
}
}
}
int numCycles = 1;
if (isFilled(row,col,myMap))
{
numCycles = 0;
}
while (!isFilled(row,col,myMap))
{
numCycles++;
for (int j = 0;j < row;j++)
{
for (int k = 0;k < col;k++)
{
if (myMap[j][k].getIsBunny())
{       //cout<< j<<" "<<k<<" " <<"true"<<endl;
switch (myMap[j][k].getNextCycle())
{
case Spot::UP :
if (j>0)
myMap[j-1][k].makeBunny();
break;
case Spot::RIGHT :
if (k<col-1)
myMap[j][k + 1].makeBunny();
break;
case Spot::DOWN :
if (j<row-1)
myMap[j+ 1][k].makeBunny();
break;
case Spot::LEFT :
if (k>0)
myMap[j][k - 1].makeBunny();
break;
}
myMap[j][k].setNextCycle();
}
//cout<< j<<" "<<k<<" " <<"outside"<<endl;
}
}
}
int time = numCycles*minPerCycle;
outFile<<"It took " <<time <<" minutes for the bunnies to take over the world!n";
cout<<"It took " <<time <<" minutes for the bunnies to take over the world!n";
for(int i=0; i < col; i++) {
delete [] myMap[i];
}
delete myMap;
}
}
inFile.close();
outFile.close();
}
catch (ifstream::failure e) {
cout << "Exception opening/reading file";
}
return 0;
}

bool isFilled(int row, int col,Spot **myMap)
{
for (int i = 0;i < row;i++)
{
for (int j = 0;j < col;j++)
{
if (!myMap[i][j].getIsBunny())
{
//cout<<"true ";
return false;
}
//else
//  cout<<"false ";

}
//cout<<endl;
}
return true;
}

是否尝试过使用指针进行传递?

static bool isFilled(int row, int col,Spot** yourMap)

我不确定Spot myMap[row][col]是否会被编译器接受,因为rowcol似乎是在运行时设置的。

您可能需要以以下方式分配内存

Spot** myMap = new Spot* [col];
for(int i = 0; i < col; ++i)
myMap[i] = new Spot [row];

删除内存需要使用类似的for loop,但请记住调用delete []

我认为您可以将对2D数组的引用发送到要使用2D数组的方法中。

bool isFilled(int row, int col, Spot &m){  // if you want a fix position in the array
// Code
}

如果要删除数组,则必须使用delete运算符。

首先你必须删除指针数组指向的所有数组,即

for(int i=0; i < size; i++) {
delete [] spot[i];  // Depends on what you have called the array
}

然后你必须用删除指针数组

delete [] spot;   

Spot myMap[row][col];

这不是合法的C++代码。你可以在C99中做到这一点,但不能在C++中做到。

在C++中有两个基本选择:

  1. 使用一维数组,将其视为2D数组
  2. 使用不规则的二维阵列,而不是连续的二维阵列

使用一维数组的主要优点是速度快。如果你需要速度,这就是前进的道路。图形程序员经常使用这种方法,因为他们需要这种速度。缺点是您必须将2D索引转换为单个索引。例如,map[i][j]可能变成map[i*col+j](行主序)或map[i+j*row](列主序)。

使用不规则数组的主要优点是索引是自然的。缺点是要管理更多的内存,访问速度也要慢得多。对于扁平阵列,访问包括少量整数运算和单个内存查找。参差不齐的数组需要两次内存查找。

这是C++,所以我的建议是让你的地图成为一个类。提供一个具有行和列大小的非默认构造函数来分配内存,并提供一个要清理的析构函数(如果需要)。如果您使用std::vector作为底层存储机制,它将为您进行分配和清理。提供operator()的过载,作为映射的索引机制。现在,用于表示地图的机制已隐藏。您可以从一个矢量开始,如果速度太慢,请切换到扁平阵列方法。