C++ 矢量超出范围,但为什么?

C++ 矢量超出范围,但为什么?,c++,arrays,matrix,multidimensional-array,vector,C++,Arrays,Matrix,Multidimensional Array,Vector,上面说我的向量超出了ArrayD的范围。但我不明白为什么。其他一切都可以。表示向量在双deez后超出范围。因此,它不会完全提升 使用名称空间std 你好 空隙填充 双x,y,z 双p,B int E=0 双V[16] 双C[16] 双D[16] int m,n int main { } 空隙填充 { 这里,q从零开始,所以您试图访问索引[-1] ArrayD[q - 1] 在ArrayP上迭代时,您可以正确地执行以下操作: 对于int i=0;INOTE:当使用标准的库容器(如St::vect

上面说我的向量超出了ArrayD的范围。但我不明白为什么。其他一切都可以。表示向量在双deez后超出范围。因此,它不会完全提升

使用名称空间std

你好

空隙填充

双x,y,z

双p,B

int E=0

双V[16]

双C[16]

双D[16]

int m,n

int main

{

}

空隙填充

{


这里,q从零开始,所以您试图访问索引[-1]

ArrayD[q - 1]
在ArrayP上迭代时,您可以正确地执行以下操作:


对于int i=0;INOTE:当使用标准的库容器(如St::vector或STD::Vector)和避免C样式数组时,使用C++来解决方案。STD::vector是动态的,您推送数据行,这意味着您永远不会溢出它。您定义行和列常量,然后完全忽略其他的Pro和Pro常量。gram,而不是到处使用硬编码的4。为了保持一致,这是值得修正的。记住int[4]有4个位置,编号为0..3和P[4]当您离开数组末尾时,是未定义的行为。您还将所有内容读取到每个数组中不存在的相同元素中。还要记住argv!您可以使用它指定要打开的文件,这意味着很容易重新运行测试:例如,myprogram qap.txt。您的while循环缺少{和}大括号。在任何像样的IDE中自动格式化都会揭示这一点。您正在读取数据,但从未对数据做任何处理,实际上跳过了一行。也许这是有意的?ifstream in_fileqap.txt;ifstream in_file.is_open{while getlinein_file,data,所以我命名了我想要使用的txt文件并将{}对于while循环,但它仍然没有拾取矩阵。那些有效,不起作用的是ArrayD。V[m]很好
ifstream fileA("P_matrix.txt");
ifstream fileB("b_matrix.txt");
ifstream fileC("d_matrix.txt");
vector<double>ArrayP;
vector<double>ArrayB;
vector<double>ArrayD;


cout << "P Matrix Values \n";
while (fileA.good())
{
    fileA >> P;
    ArrayP.push_back(P);
}
for (int i = 0; i<ArrayP.size(); i++)
{
    cout << ArrayP[i] << ",";
}
system("pause");



cout << "B Matrix Values \n";
while (fileB.good())
{
    fileB >> B;
    ArrayB.push_back(B);
}
for (int j = 0; j < 16; j++)
{
    cout << ArrayB[j] << ",";
}
system("pause");
while (fileC.good())
{

    fileC >> D;
    ArrayD.push_back(D);


}
for (int k = 0; k <16; k++)
{
    cout << ArrayD[k] << ",";

}

system("pause");


for (int m = 0; m < 16; m++)
{
    V[m] = ArrayP[m] * ArrayB[m];
    cout << V[m] << ",";
}
system("pause");

for (int n = 0; n < 16; n++)
{
    C[n] = V[n] * ArrayD[n];
    cout << C[n] << ",";
}

//outfile.close();
system("pause");

double deez;
for (int d = 0; d < 16; d++) //acscending
{
    for (int q = 0; q < 16; q++)
    {
        if (ArrayD[q] < ArrayD[q - 1])
        {
            deez = ArrayD[q];
            ArrayD[q] = ArrayD[q - 1];
            ArrayD[q - 1] = deez;
        }
    }
}
for (int q = 0; q < 16; q++)
    cout << C[q] << ",";
system("pause");



double nutz;
for (int d = 0; d < 16; d++) //descending
{
    for (int q = 0; q < 16; q++)
    {
        if (V[q] < V[q + 1])
        {
            nutz = V[q];
            V[q] = V[q + 1];
            V[q + 1] = nutz;
        }
    }
}
for (int q = 0; q < 16; q++)
    cout << V[q] << ",";
system("pause");
return;

ArrayD[q - 1]