无效向量<T>下标

问题描述 投票:0回答:2

我正在尝试创建一个二维向量,其中 0 和 1 坐标都是复数,定义向量似乎工作正常,但是当我尝试访问它时,我收到一个错误,该错误在新选项卡中弹出有一堆乱码,但在某处它说无效的向量下标。 (缩写)代码是

#include <iostream>     
#include <vector>
#include <complex>
using namespace std;
int main() 
{
vector<vector<complex<double>>> rho;
for(int g = 0; g < 4; ++g){
    for(int h = 0; h < 4; ++h){
        rho.push_back(vector<complex<double>>(2));
        rho.at(g).at(h) = 0;
        cout << rho.at(g).at(h)<<endl;
    }
}
}

任何帮助将不胜感激:) xx

c++ visual-studio-2010 vector complex-numbers subscript
2个回答
3
投票

存在无效的下标,因为您有一个包含内部向量的外部向量。您的外部向量在内循环上添加一个新向量,因此您将推回许多大小为 2 的向量,h 的范围从 0 到 3,并且 2 和 3 对于所有内部向量来说都是无效的下标。

不幸的是,这是“无用的无上下文”错误之一。如果它至少说“大小为 2 的向量的下标 2 无效”会更有用


-1
投票

我认为对于C++,尖括号需要用空格分隔。 当我这样做时,代码按原样工作。看看这个

#include <iostream>
#include <vector>
#include <complex>
using namespace std;

int main()
{
    int g = 0;
    int h = 0;
    vector<vector<complex<double > > > rho;
    rho.push_back(vector<complex<double > >(2));
    rho.at(g).at(h) = 0;
    cout << rho.at(g).at(h)<<endl;
}
© www.soinside.com 2019 - 2024. All rights reserved.