C++ 为什么我总是得到;矢量下标超出范围“;2D向量的错误?

C++ 为什么我总是得到;矢量下标超出范围“;2D向量的错误?,c++,adjacency-list,indexoutofrangeexception,2d-vector,C++,Adjacency List,Indexoutofrangeexception,2d Vector,我正在尝试使用向量(V)中的向量(V)创建邻接列表图形表示。我的目标是,对于输入的每个新顶点值,我增加V*的大小,并将新值插入到打开的点中,这将是V**的起始值 然后,我想在输入的新值和邻接矩阵中已经存在的另一个值之间创建一条边 然而,在我的代码中,一旦我输入secIndexVal,我就会不断得到“向量下标超出范围错误”。我已经尝试过将numberIndices的初始值变大(最多10个)以及我在代码中提到的内容。多谢各位 *向量的向量,我记为“V” **V中的向量,我记为“V” int inde

我正在尝试使用向量(V)中的向量(V)创建邻接列表图形表示。我的目标是,对于输入的每个新顶点值,我增加V*的大小,并将新值插入到打开的点中,这将是V**的起始值

然后,我想在输入的新值和邻接矩阵中已经存在的另一个值之间创建一条边

然而,在我的代码中,一旦我输入secIndexVal,我就会不断得到“向量下标超出范围错误”。我已经尝试过将numberIndices的初始值变大(最多10个)以及我在代码中提到的内容。多谢各位

*向量的向量,我记为“V” **V中的向量,我记为“V”

int indexVal; 
int secIndexVal; 
int numberIndices = 0; 
bool valIsFound = false; 
vector <vector <int>> vecOfVectors(numberIndices);

int main()
{
    cout << "\nEnter first value: ";
    cin >> indexVal;

    cout << "\nEnter second value: ";
    cin >> secIndexVal;

    numberIndices++;
    vecOfVectors.resize(numberIndices);
    vecOfVectors[numberIndices - 1].push_back(indexVal);
    vecOfVectors[numberIndices - 1].push_back(secIndexVal);
    //pushes back the values of indexVal then secIndexVal to index 0 
    //I tried changing the value within the brackets to [numberIndices]

    numberIndices++;
    vecOfVectors.resize(numberIndices);
    vecOfVectors[numberIndices].push_back(secIndexVal); 
    vecOfVectors[numberIndices].push_back(indexVal);
    //pushes back the values of secIndexVal then indexVal to index 1
    //I tried changing the value within the brackets to [numberIndices + 1]         

    cout << "\nExit? (y/n)";
    cin >> userPrompt;

    while (userPrompt != 'y')
    {

        cout << "\nEnter a new value: ";
        cin >> indexVal; //new value

        numberIndices++;
        vecOfVectors.resize(numberIndices); 

        cout << "\n enter a value to connect it to: ";
        cin >> secIndexVal; //old value

        vecOfVectors[numberIndices - 1].push_back(indexVal); //new value given newly opened spot on vecOfVectors


        for (int j = 0; j < numberIndices; ++j)
        {
            if (vecOfVectors[j].at(0) == secIndexVal)
            {
                valIsFound = true; 
                vecOfVectors[j].push_back(indexVal); //the new value is attached to the adjacency list at the index of the old value
            }
        }

        if (valIsFound == true)
        {
            vecOfVectors[numberIndices - 1].push_back(secIndexVal); //the old value is attached to the adjacency list at the index of the new value
        }

        cout << "\nExit? (y/n)";
        cin >> userPrompt;
    }
}
int indexVal;
int secIndexVal;
int numberIndices=0;
bool valIsFound=false;
向量向量向量(数字骰子);
int main()
{
cout>indexVal;
cout>secIndexVal;
numberIndices++;
VectofVectors.调整大小(numberIndices);
vecOfVectors[numberIndices-1]。向后推(indexVal);
vecOfVectors[numberIndices-1]。向后推(secIndexVal);
//将indexVal的值向后推,然后将secIndexVal推到索引0
//我尝试将括号内的值更改为[numberIndices]
numberIndices++;
VectofVectors.调整大小(numberIndices);
vecOfVectors[numberIndices]。向后推(secIndexVal);
vecOfVectors[numberIndices]。向后推(indexVal);
//将secIndexVal的值向后推,然后将indexVal推到索引1
//我尝试将括号内的值更改为[numberIndices+1]
cout>userPrompt;
while(userPrompt!=“y”)
{
cout>indexVal;//新值
numberIndices++;
VectofVectors.调整大小(numberIndices);
cout>secIndexVal;//旧值
vecOfVectors[numberIndices-1]。向后推(indexVal);//给定vecOfVectors上新打开的点的新值
对于(int j=0;juserPrompt;
}
}

vecOfVectors.resize(numberIndices);vecOfVectors[numberIndices]。向后推(secIndexVal)这里缺少-1吗?使用调试器。当您遇到错误时,沿着调用堆栈走到您的代码,您可以确切地看到出现错误的位置和原因。如果您总是希望向量中的最后一个项目考虑使用该函数,那么触发此错误的索引的值是多少?