我有以下课程
class Node
{
int key;
Node**Nptr;
public:
Node(int maxsize,int k);
};
Node::Node(int maxsize,int k)
{
//here i want to dynamically allocate the array of pointers of maxsize
key=k;
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何在构造函数中动态分配指针数组 - 此数组的大小为maxsize.
小智 11
Node::Node(int maxsize,int k)
{
NPtr = new Node*[maxsize];
}
Run Code Online (Sandbox Code Playgroud)
但像往常一样,你可能最好使用std ::指针向量.
假设您要创建 3 行 4 列的矩阵,然后,
int **arr = new int * [3]; //first allocate array of row pointers
for(int i=0 ; i<rows ; ++i)
{
arr[i] = new int[4]; // allocate memory for columns in each row
}
Run Code Online (Sandbox Code Playgroud)