Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的二维复数数组+; 我在C++编程方面是新手,所以我需要一个关于2D数组的帮助。有可能从两个带两个for循环的真实数组创建复杂数组吗?我试图在代码中做到这一点,但…我不知道如何做到这一点。 谢谢你的帮助! 这是我的代码:: #include <iostream> #include <fstream> #include <complex> #include <cmath> using namespace std; int const BrGr = 15, BrCv = BrGr + 1, BrSat = 24; //(BrCv=number of nodes,BrSat=number of hours) int main() { // Every array must be dynamic array.It is a task.Is this correct way? auto *Ipot = new double[BrCv - 1][BrSat]; auto *cosfi = new double[BrCv - 1][BrSat]; auto *S_pot = new complex<double>[BrCv - 1][BrSat]; auto *I_inj = new complex<double>[BrCv - 1][BrSat]; auto *V_cvo = new complex<double>[BrCv][BrSat]; ifstream reader("Input.txt"); if (reader.is_open()) { for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> Ipot[i][j]; } } for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> cosfi[i][j]; } } } else cout << "Error!" << endl; reader.close(); // Here i want to create 2D array of complex numbers - Is this correct way? // Also in same proces i want to calculate a value of S_pot in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { S_pot[i][j] = complex<double>(Ipot[i][j]*cosfi[i][j],Ipot[i][j]*sqr(1-pow(cosfi[i][j],2))); } } // Here i give a value for V_cvo in nodes for every single hour for (int i = 0;i < BrCv;i++) { for (int j = 0;j < BrSat;j++) { V_cvo[i][j] = 1; } } // Here i want to calculate a value of I_inj in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { I_inj[i][j] = conj(S_pot[i][j] / V_cvo[i][j]); } } // Here i want to delete all arrays delete[] Ipot, cosfi, S_pot, I_inj, V_cvo; system("pause"); return 0; #包括 #包括 #包括 #包括 使用名称空间std; int const BrGr=15,BrCv=BrGr+1,BrSat=24; //(BrCv=节点数,BrSat=小时数) int main() { //每个数组都必须是动态数组。这是一项任务。这种方法正确吗? 自动*Ipot=新双精度[BrCv-1][BrSat]; 自动*cosfi=新双精度[BrCv-1][BrSat]; auto*S_pot=新复合体[BrCv-1][BrSat]; auto*I_inj=新复合体[BrCv-1][BrSat]; auto*V_cvo=新复合体[BrCv][BrSat]; ifstream阅读器(“Input.txt”); if(reader.is_open()) { 对于(int i=0;iIpot[i][j]; } } 对于(int i=0;icosfi[i][j]; } } } else cout_C++_Arrays - Fatal编程技术网

C+中的二维复数数组+; 我在C++编程方面是新手,所以我需要一个关于2D数组的帮助。有可能从两个带两个for循环的真实数组创建复杂数组吗?我试图在代码中做到这一点,但…我不知道如何做到这一点。 谢谢你的帮助! 这是我的代码:: #include <iostream> #include <fstream> #include <complex> #include <cmath> using namespace std; int const BrGr = 15, BrCv = BrGr + 1, BrSat = 24; //(BrCv=number of nodes,BrSat=number of hours) int main() { // Every array must be dynamic array.It is a task.Is this correct way? auto *Ipot = new double[BrCv - 1][BrSat]; auto *cosfi = new double[BrCv - 1][BrSat]; auto *S_pot = new complex<double>[BrCv - 1][BrSat]; auto *I_inj = new complex<double>[BrCv - 1][BrSat]; auto *V_cvo = new complex<double>[BrCv][BrSat]; ifstream reader("Input.txt"); if (reader.is_open()) { for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> Ipot[i][j]; } } for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> cosfi[i][j]; } } } else cout << "Error!" << endl; reader.close(); // Here i want to create 2D array of complex numbers - Is this correct way? // Also in same proces i want to calculate a value of S_pot in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { S_pot[i][j] = complex<double>(Ipot[i][j]*cosfi[i][j],Ipot[i][j]*sqr(1-pow(cosfi[i][j],2))); } } // Here i give a value for V_cvo in nodes for every single hour for (int i = 0;i < BrCv;i++) { for (int j = 0;j < BrSat;j++) { V_cvo[i][j] = 1; } } // Here i want to calculate a value of I_inj in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { I_inj[i][j] = conj(S_pot[i][j] / V_cvo[i][j]); } } // Here i want to delete all arrays delete[] Ipot, cosfi, S_pot, I_inj, V_cvo; system("pause"); return 0; #包括 #包括 #包括 #包括 使用名称空间std; int const BrGr=15,BrCv=BrGr+1,BrSat=24; //(BrCv=节点数,BrSat=小时数) int main() { //每个数组都必须是动态数组。这是一项任务。这种方法正确吗? 自动*Ipot=新双精度[BrCv-1][BrSat]; 自动*cosfi=新双精度[BrCv-1][BrSat]; auto*S_pot=新复合体[BrCv-1][BrSat]; auto*I_inj=新复合体[BrCv-1][BrSat]; auto*V_cvo=新复合体[BrCv][BrSat]; ifstream阅读器(“Input.txt”); if(reader.is_open()) { 对于(int i=0;iIpot[i][j]; } } 对于(int i=0;icosfi[i][j]; } } } else cout

C+中的二维复数数组+; 我在C++编程方面是新手,所以我需要一个关于2D数组的帮助。有可能从两个带两个for循环的真实数组创建复杂数组吗?我试图在代码中做到这一点,但…我不知道如何做到这一点。 谢谢你的帮助! 这是我的代码:: #include <iostream> #include <fstream> #include <complex> #include <cmath> using namespace std; int const BrGr = 15, BrCv = BrGr + 1, BrSat = 24; //(BrCv=number of nodes,BrSat=number of hours) int main() { // Every array must be dynamic array.It is a task.Is this correct way? auto *Ipot = new double[BrCv - 1][BrSat]; auto *cosfi = new double[BrCv - 1][BrSat]; auto *S_pot = new complex<double>[BrCv - 1][BrSat]; auto *I_inj = new complex<double>[BrCv - 1][BrSat]; auto *V_cvo = new complex<double>[BrCv][BrSat]; ifstream reader("Input.txt"); if (reader.is_open()) { for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> Ipot[i][j]; } } for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { reader >> cosfi[i][j]; } } } else cout << "Error!" << endl; reader.close(); // Here i want to create 2D array of complex numbers - Is this correct way? // Also in same proces i want to calculate a value of S_pot in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { S_pot[i][j] = complex<double>(Ipot[i][j]*cosfi[i][j],Ipot[i][j]*sqr(1-pow(cosfi[i][j],2))); } } // Here i give a value for V_cvo in nodes for every single hour for (int i = 0;i < BrCv;i++) { for (int j = 0;j < BrSat;j++) { V_cvo[i][j] = 1; } } // Here i want to calculate a value of I_inj in every node for every hour for (int i = 0;i < BrCv - 1;i++) { for (int j = 0;j < BrSat;j++) { I_inj[i][j] = conj(S_pot[i][j] / V_cvo[i][j]); } } // Here i want to delete all arrays delete[] Ipot, cosfi, S_pot, I_inj, V_cvo; system("pause"); return 0; #包括 #包括 #包括 #包括 使用名称空间std; int const BrGr=15,BrCv=BrGr+1,BrSat=24; //(BrCv=节点数,BrSat=小时数) int main() { //每个数组都必须是动态数组。这是一项任务。这种方法正确吗? 自动*Ipot=新双精度[BrCv-1][BrSat]; 自动*cosfi=新双精度[BrCv-1][BrSat]; auto*S_pot=新复合体[BrCv-1][BrSat]; auto*I_inj=新复合体[BrCv-1][BrSat]; auto*V_cvo=新复合体[BrCv][BrSat]; ifstream阅读器(“Input.txt”); if(reader.is_open()) { 对于(int i=0;iIpot[i][j]; } } 对于(int i=0;icosfi[i][j]; } } } else cout,c++,arrays,C++,Arrays,为前置类型声明动态2D数组与std::complex的声明相同 锯齿阵列: complex<int> **ary = new complex<int>*[sizeY]; //run loop to initialize for (int i = 0; i < sizeY; ++i) { ary[i] = new complex<int>[sizeX]; } //clean up (you could wrap this in a class a

为前置类型声明动态2D数组与std::complex的声明相同

锯齿阵列:

complex<int> **ary = new complex<int>*[sizeY];
//run loop to initialize
for (int i = 0; i < sizeY; ++i) 
{
    ary[i] = new complex<int>[sizeX];
}
//clean up (you could wrap this in a class and write this in its destructor)
for (int i = 0; i < sizeY; ++i) 
{
    delete[] ary[i];
}
delete[] ary;
//access with
ary[i][j];
//assign index with
ary[i][j] = complex<int>(int,int);
complex**ary=newcomplex*[sizeY];
//运行循环进行初始化
对于(int i=0;i
它的重量比需要的重一点,并且它分配的块比需要的多。 多维数组只需要一块内存,而不需要每行一块

矩形阵列:

complex<int> *ary = new complex<int>[sizeX * sizeY];
//access with:
ary[y*sizeX + x]
//assign with
ary[y*sizeX+x] = complex<int>(int,int);
//clean up
delete[] ary;
complex*ari=newcomplex[sizeX*sizeY];
//访问:
ary[y*sizeX+x]
//指派
ary[y*sizeX+x]=复数(int,int);
//清理
删除[]项;

只分配一个连续的块是一种方法(对分配器的影响较小,局部性更好,等等,但您必须牺牲干净和良好的订阅。

为前置类型声明动态2D数组与
std::complex
相同

锯齿阵列:

complex<int> **ary = new complex<int>*[sizeY];
//run loop to initialize
for (int i = 0; i < sizeY; ++i) 
{
    ary[i] = new complex<int>[sizeX];
}
//clean up (you could wrap this in a class and write this in its destructor)
for (int i = 0; i < sizeY; ++i) 
{
    delete[] ary[i];
}
delete[] ary;
//access with
ary[i][j];
//assign index with
ary[i][j] = complex<int>(int,int);
complex**ary=newcomplex*[sizeY];
//运行循环进行初始化
对于(int i=0;i
它的重量比需要的重一点,并且它分配的块比需要的多。 多维数组只需要一块内存,而不需要每行一块

矩形阵列:

complex<int> *ary = new complex<int>[sizeX * sizeY];
//access with:
ary[y*sizeX + x]
//assign with
ary[y*sizeX+x] = complex<int>(int,int);
//clean up
delete[] ary;
complex*ari=newcomplex[sizeX*sizeY];
//访问:
ary[y*sizeX+x]
//指派
ary[y*sizeX+x]=复数(int,int);
//清理
删除[]项;

只分配一个连续的块是一种方法(对分配器的影响较小,局部性更好,等等,但是您必须牺牲干净和良好的订阅。

注意:我使用double来完成这些示例,但是您可以用任何类型替换double

老实说,您可能不想使用2D数组

<>在C++中创建二维动态数组是一个多阶段操作,不能仅仅是

double twoDArray [nrRows][nrColumns];

这有几个问题,但最重要的是行和列不是一个常量,不是在编译时定义的值。有些编译器允许第一个,但这不能保证。我不知道是否有编译器允许第二个

相反,首先创建一个行数组来保存列,然后分别创建每一行列

以下是设置:

double * arr[] = new double*[nrRows]; // create rows to point at columns
for (size_t index = 0; index < nrRows; index++)
{
    arr[index] = new double[nrColumns]; // create columns
}
<>在C++中,理智的人选择STD::向量在一个动态大小的数组上,除非给出非常强的<非常强>非常强/强的原因。
std::vector<std::vector<double>> vec(nrRows, std::vector<double>(nrColumns));
这实际上没有内存问题,但又回到了糟糕的位置,因为向量可能在任何地方

但是

还有一种更好的方法:使用一维数组并将其包装在一个简单的类中,使其看起来像二维数组

template <class TYPE>
class TwoDee
{
private:
    size_t mNrRows;
    size_t mNrColumns;
    vector<TYPE> vec;
public:
    TwoDee(size_t nrRows, size_t nrColumns): 
        mNrRows(nrRows), mNrColumns(nrColumns), vec(mNrRows*mNrColumns)
    {

    }
    TYPE & operator()(size_t row, size_t column)
    {
        return vec[row* mNrColumns + column];
    }
    TYPE operator()(size_t row, size_t column) const
    {
        return vec[row* mNrColumns + column];
    }
};
模板
二等
{
私人:
尺寸箭头;
柱体尺寸;
向量向量机;
公众:
TwoDee(大小为行,大小为列):
mNrRows(nrRows)、mNrColumns(nrColumns)、vec(mNrRows*mNrColumns)
{
}
类型和运算符()(大小行、大小列)
{
返回vec[行*mNrColumns+列];
}
类型运算符()(大小行、大小列)常量
{
返回vec[行*mNrColumns+列];
}
};
这只小野兽将完成你需要的2D向量的大部分工作。你可以复制它,你可以移动它。你可以随心所欲地咀嚼。杰伊·雷诺会做得更多

我直接跳到模板版本,因为我有一个很好的理由解释TwoDee类两次

构造器很简单。你给它数组的维数,它构建了一个漂亮、安全的1D向量。不需要混乱、混乱,也不需要Zayn

运算符()函数获取行和列索引,执行简单的算术运算,将索引转换为单个索引,然后返回对索引值的引用以允许修改,或者返回常量情况下索引值的副本

如果你觉得你需要额外的安全,加上范围检查

TYPE & operator()(size_t row, size_t column)
{
    if (row < mNrRows && column < mNrColumns)
    {
        return vec[row* mNrColumns + column];
    }
    throw std::out_of_range("Bad indices");
}
TYPE&operator()(大小行、大小列)
{
if(行vec[i][j] = somevalue;
template <class TYPE>
class TwoDee
{
private:
    size_t mNrRows;
    size_t mNrColumns;
    vector<TYPE> vec;
public:
    TwoDee(size_t nrRows, size_t nrColumns): 
        mNrRows(nrRows), mNrColumns(nrColumns), vec(mNrRows*mNrColumns)
    {

    }
    TYPE & operator()(size_t row, size_t column)
    {
        return vec[row* mNrColumns + column];
    }
    TYPE operator()(size_t row, size_t column) const
    {
        return vec[row* mNrColumns + column];
    }
};
TYPE & operator()(size_t row, size_t column)
{
    if (row < mNrRows && column < mNrColumns)
    {
        return vec[row* mNrColumns + column];
    }
    throw std::out_of_range("Bad indices");
}
TwoDee<complex<double>> spot(BrCv - 1, BrSat);
for (int i = 0;i < BrCv - 1;i++)
{
    for (int j = 0;j < BrSat;j++)
    {
        Spot(i,j) = complex<double>(7.8*Ipot(i,j),2.3*cosfi(i,j));
    }
}