如何返回指针的二维数组在C ++ [英] How to return a 2d array of pointers in c++
问题描述
我创建了一个功能,希望这个函数返回指针的二维数组。
不过,我已经尝试了很多方法,编译器只是给了我的错误。
这里是我的功能,细胞
是我定义的类。现在我只是给函数void类型。
无效CreatePuzzle(INT NROWS,诠释nColumns,诠释MINVAL,MAXVAL INT)
{
//初始化拼图
细胞*拼图[NROWS] [nColumns]; 的for(int i = 0; I< NROWS;我++)
{
对于(INT J = 0; J< nColumns; J ++)
{
益智[I] [J] =新的细胞(I,J); }
}
}
这是不是直接回答你的问题,但它可能会有所帮助:考虑使用现代C ++
考虑以下code:
的#include<&iostream的GT;
#包括LT&;矢量>类细胞{
上市:
细胞(int值= 0)
:m_value(值){}
int值()const的{
返回m_value;
}
空值(int值){
m_value =价值;
}
私人的:
INT m_value;
};益智类{
上市:
拼图(INT行,诠释COLS)
:m_cells(行* COLS)
m_rows(行),
m_cols(COLS){
//现在假设我们只给他们一个连续的值
int值= 0;
为(自动&安培;电池:m_cells){
cell.value(价值++);
}
} 细胞和放大器;细胞(INT行,诠释山口){
返回m_cells [*行+ m_cols COL];
} 私人的: 的std ::矢量<电池> m_cells;
INT m_rows;
INT m_cols;
};INT主(INT ARGC,CHAR *的argv []){
如果(argc个!= 3){
的std :: CERR<< 用法:<<的argv [0]<< 行COLS<<的std :: ENDL;
返回1;
} INT =行的std :: Stoi旅馆(的argv [1]);
INT COLS =的std :: Stoi旅馆(的argv [2]); 益智解谜(行,COLS); 对于(INT行= 0;&行LT;行++行){
对于(INT COL = 0;&山坳下,COLS ++ COL){
性病::法院LT&;< puzzle.cell(行,列).value的()<< ;
}
性病::法院LT&;<的std :: ENDL;
}
}
这是一个过于简单化,但你(希望)的想法:我有一个Cell类,其唯一目的是保持一个值(在这种情况下一个整数)。然后,我创建了一个游戏,由N-由-M细胞。
游戏的构造函数声明此明确:*创建一个游戏,我需要你为我提供的行和列数在内部,它把所有的细胞在的std ::载体
,并提供一个公共的方法来访问的(行,列)方式线性排列。您只需跨越数组自己。
希望它用来向您展示的怎么可能是更地道的C ++看起来像一个一瞥。这是不以任何方式完美code,但它是一个开始。
我编译在OS X 10.7.4使用GCC 4.8.1的code:
G ++ game.cpp -std = C ++ 11
一个简单的会话:
./ a.out的3 5
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
另一个会话:
./ a.out的2 10
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
请注意,我从来没有担心分配/释放或内存泄漏:它是所有由的std ::矢量管理
I've created a function and want this function to return a 2d array of pointers.
However I've tried a lot of methods and the compiler just gave me errors.
Here's my function, cell
is a class I defined. For now I just give the function void type.
void CreatePuzzle (int nRows, int nColumns, int minVal, int maxVal)
{
//initialize the puzzle
cell *Puzzle[nRows][nColumns];
for (int i = 0; i < nRows; i++)
{
for(int j=0; j < nColumns; j++)
{
Puzzle[i][j] = new cell(i,j);
}
}
}
This is not a direct answer to your question, but it may be helpful: consider using modern C++.
Consider the following code:
#include <iostream>
#include <vector>
class Cell {
public:
Cell(int value = 0)
: m_value(value) { }
int value() const {
return m_value;
}
void value(int value) {
m_value = value;
}
private:
int m_value;
};
class Puzzle {
public:
Puzzle(int rows, int cols)
: m_cells(rows * cols),
m_rows(rows),
m_cols(cols) {
// for now let's assume we just give them a sequential value
int value = 0;
for(auto & cell : m_cells) {
cell.value(value++);
}
}
Cell& cell(int row, int col) {
return m_cells[row * m_cols + col];
}
private:
std::vector<Cell> m_cells;
int m_rows;
int m_cols;
};
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cerr << "usage: " << argv[0] << " rows cols" << std::endl;
return 1;
}
int rows = std::stoi(argv[1]);
int cols = std::stoi(argv[2]);
Puzzle puzzle(rows, cols);
for(int row = 0; row < rows; ++row) {
for(int col = 0; col < cols; ++col) {
std::cout << puzzle.cell(row, col).value() << " ";
}
std::cout << std::endl;
}
}
It is an over-simplification, but you (hopefully) get the idea: I have a Cell class whose only purpose is to hold a value (in this case an integer). Then I create a game which consists of N-by-M cells.
The constructor of the game declares this explicitly: *to create a game, I need you to provide me the number of rows and columns". Internally, it places all the cells in a std::vector
, and offers a public method to access that linear arrangement in a (row, column) manner. You simply "stride" the array yourself.
Hopefully it serves to show you a glimpse of how could a more idiomatic C++ looks like. It is not perfect code by any means, but it is a start.
I compiled the code using GCC 4.8.1 on OS X 10.7.4:
g++ game.cpp -std=c++11
A sample session:
./a.out 3 5
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
another session:
./a.out 2 10
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
Notice that I never had to worry about allocation/deallocation or memory leaks: it is all managed by the std::vector
.
这篇关于如何返回指针的二维数组在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!