更新:C ++指针代码段 [英] UPDATE: C++ Pointer Snippet
问题描述
再次致以问候,再次感谢所有提供第一个问题答案的人.以下代码已更新为每个分配包括两个功能.
Greetings again, and thanks once more to all of you who provided answers to the first question. The following code is updated to include the two functions per the assignment.
要查看原始问题,请点击此处.
To see the original question, click here.
我很确定 可以满足任务的要求,但是再次感谢您的协助.我是否适当地修改了删除语句?再次感谢.
I am pretty sure this fulfills the requirements of the assignment, but once again I would greatly appreciate any assistance. Did I modify the delete statements appropriately? Thanks again.
#include<iostream>
#include<string>
int** createArray(int, int);
void deleteArray(int*[], int);
using namespace std;
int main()
{
int nRows;
int nColumns;
cout<<"Number of rows: ";
cin>>nRows;
cout<<"Number of columns: ";
cin>>nColumns;
int** ppInt = createArray(nRows, nColumns);
deleteArray(ppInt, nRows);
}
int** createArray(int nRows, int nColumns)
{
int** ppInt = new int*[nRows];
for (int nCount = 0; nCount < nRows; nCount++)
{
ppInt[nCount] = new int[nColumns];
}
return ppInt;
}
void deleteArray(int** nPointer, int nRows)
{
for (int nCount = 0; nCount < nRows; nCount++)
{
delete[] nPointer[nCount];
}
delete[] nPointer;
}
P.S.如果有帮助,这是作业文档本身:
P.S. Here is the assignment documentation itself, in case it helps:
(1)设计并实现为二维整数数组分配内存的函数:该函数假定使用两个整数作为参数,一个用于行数,一个用于列数.您需要在此功能中使用"new"运算符.请记住,我们需要首先创建一个指针数组.然后,对于该数组中的每个指针,我们需要创建一个整数数组.该函数应该返回一个指向二维整数数组的指针.
(1) Design and implement a function to allocate memory for a 2-D integer array: the function is supposed to take two integers as parameters, one for number of rows and one for number of columns. You need to use "new" operator in this function. Remember that we need to first create an array of pointers. Then, for each pointer in that array, we need to create an array of integers. This function is supposed to return a pointer which points to a 2-D integer array.
(2)设计并实现一个用于为此2-D数组取消分配内存的函数:该函数应该具有两个参数(一个指向2-D整数数组的指针,另一个是数字.数组中的行数).在函数中,应该使用删除"运算符为此2-D数组取消分配内存.您应该先删除每一行(整数数组),然后再删除指针数组.
(2) Design and implement a function to de-allocate memory for this 2-D array: the function is supposed to have two parameters (a pointer which points to a 2-D integer array, and the other one is number of rows in the array). In the function, you are supposed to de-allocate memory for this 2-D array using the "delete" operator. You should delete each row (an array of integers) first, and then delete the array of pointers.
推荐答案
代码看起来不错.
但是,对于我们人类来说,您可能要解决一些问题:
However, there are some problems you may want to address, for us humans:
-
您的函数签名(声明)缺少参数名称.更合适:
Your function signatures (declarations) lack parameter names. More suitable:
int** createArray(int rows, int columns);
void deleteArray(int** array, int rows);
您的函数名称对它们真正创建/删除的内容的描述并不太多.例如,create2DArray
将是一个更明智的选择.
Your function names aren't too descriptive as to what they really create/delete. create2DArray
would be a wiser choice, for example.
一些超越自我的东西,供您个人练习:
Some things which go Above And Beyond, for your personal practice:
- 创建一个类,该类将
rows
和cols
作为其构造函数的参数.确保自动取消分配数组. - 使用
std::vector
并为您的类创建一个resize
成员函数. 请注意,这与最初的问题有所不同,后者要求提供指示. - 创建一个
copy
函数和一个clone
函数,以将数据复制到另一个2D数组(可能大小不同!)或克隆现有数组.
- Create a class which takes
rows
andcols
as arguments to its constructor. Make sure to deallocate the array automatically. - Use
std::vector
and create aresize
member function for your class. Note that this deviates from the original question, which asked for pointers. - Create a
copy
function and aclone
function to copy data to another 2D array (possibly of a different size!) or clone an existing array.
这篇关于更新:C ++指针代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!