我有一个二维 double 组来解决使用动态编程的问题。我想做的如下(f 是对变量“size”进行操作并返回一个 int 的任何函数):
void myFunction(int size){
double myArr[size][f(size)];
helperFunction(size, myArr);
}
void helperFunction(int size, double[][] myArr){
//do something
}
但是这段代码显然是 java 和 c++ 的混搭。我将如何在 C++ 中完成此操作?
最佳答案
强烈推荐使用 vector 替代动态数组。 vector 在 Java 中名声不好,但在 C++ 中它们是正确的选择。演示使用"new"分配和 vector 实现此目的的“一种”c++ 方法。警告:与 Java 不同,C++ 中的原始数组需要您跟踪数组大小;所有内部数组也是如此。
#include <iostream>
#include <vector>
class Test{
private:
double **myArray;//Primitive 2d array; suggest using 2d vector.
std::vector<std::vector<double>> myVector;//Alternative vector.
std::vector<int> col;//Using vector for myArray sizes; where sizes = inner array size.
int size;//Required with primitive dynamic array; size of array.
public:
Test(){
myArray = 0;//c++ initialization before reading; safety.
size = 0;//c++ "" "" ""
}
~Test(){
flushArray();//Flush all data on destructor.
}
void flushArray(){//Empties 2d array and set it to 0.
//Flush of primitive 2d array.
for(int i = 0; i < size; ++i) {//Needs to delete every new data created
delete []myArray[i];//Freeing memory for inner node.
}
delete []myArray;//Freeing memory for outer node.
myArray = 0;//Setting pointer to 0;
size = 0;//Setting size to 0;
col.clear();//Flush for column vector: easy.
//vector is self maintained and will clear itself on destructor; exception: "new" DATA.
}
void myFunction(int size) {
if(this->size != 0) {//If there is already data...
flushArray();//Flush Array;
}
this->size = size;//Require size to free during next call.
myArray = new double*[size];//Create new array of nothings with a size of "size".
for(int i = 0; i < size; ++i) {
//Traversing through array and adding an array of doubles.
myArray[i] = new double[f(size,false)];//New DATA can be implicit
}
}
void otherFunction(int size) {
myVector.clear();//Flush Vector;
myVector.resize(size);//Automated dynamic sizing
for(auto it = myVector.begin(); it != myVector.end(); ++it) {
it->resize(f(size,true));
}
}
int f(int size, bool isVector) {
//.., do something.
if(isVector) {
return size;//Whatever int you were meant to return.
}
//Keep track of cols, maybe they'll vary
col.push_back(size);//it might be (size+i)... Required for dynamic array.
return col.back();//Return the intended size.
}
void printArraySize() {
for(int i = 0; i < size; ++i) {
std::cout<<"myArray["<<i<<"] has "<<col[i]<<" elements."<<std::endl;
}
}
void printVectorSize() {
//Using a counter, chose to use primitive for-loop.
for(int i = 0; i < myVector.size(); ++i) {
std::cout<<"myVector["<<i<<"] has "<<myVector[i].size()<<" elements. "<<std::endl;
}
}
};
int main()
{
Test test;
test.myFuntion(10);
test.otherFunction(10);
test.printArraySize();
test.printVectorSize();
return 0;
}
打印结果显示尺寸:
myArray[0] has 10 elements.
myArray[1] has 10 elements.
myArray[2] has 10 elements.
myArray[3] has 10 elements.
myArray[4] has 10 elements.
myArray[5] has 10 elements.
myArray[6] has 10 elements.
myArray[7] has 10 elements.
myArray[8] has 10 elements.
myArray[9] has 10 elements.
myVector[0] has 10 elements.
myVector[1] has 10 elements.
myVector[2] has 10 elements.
myVector[3] has 10 elements.
myVector[4] has 10 elements.
myVector[5] has 10 elements.
myVector[6] has 10 elements.
myVector[7] has 10 elements.
myVector[8] has 10 elements.
myVector[9] has 10 elements.
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.
基本上对于 vector :您的数据、它们的大小和生命周期都是为您管理的;指向"new"数据的指针的异常 vector 。而在原始动态数组中,您必须跟踪数组大小(所有数组)并在完成后删除数据/数组。
编辑:次要拼写检查。
关于c++ - C++ 中的动态二维数组作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52434688/