Я только что прогнал свой код через терминал и "Отладка подтверждения не удалась!" выскочила ошибка, которая говорит "векторный индекс вне диапазона". Это первый раз, когда я сталкивался с такой ошибкой, поэтому я не уверен, как найти, где находится ошибка в моем коде. Возможно, это что-то очевидное, так как я довольно новичок в C ++ и не очень хорошо умею находить ошибки. Ниже приведен код, который у меня есть, поэтому, пожалуйста, дайте мне знать, если вы найдете, что нужно исправить. Спасибо!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Node {
int data;
Node* right, * down;
};
Node* construct(vector<vector<int>> arr, size_t i, size_t j, size_t m, size_t n)
{
if (i > n - 1 || j > m - 1)
return NULL;
Node * temp = new Node();
temp->data = arr[i][j];
temp->right = construct(arr, i, j + 1, m, n);
temp->down = construct(arr, i + 1, j, m, n);
return temp;
}
void display(Node * head)
{
Node* Rp;
Node* Dp = head;
// loop till node->down is not NULL
while (Dp) {
Rp = Dp;
// loop till node->right is not NULL
while (Rp) {
cout << Rp->data << " ";
Rp = Rp->right;
}
cout << "\n";
Dp = Dp->down;
}
}
int main(int argc, char* argv[])
{
if ((argc == 2) && (string(argv[1]) == "-Stack"))
{
int K;
cin >> K; //getting the number of rooms from the text file
for (int i = 0; i < K; ++i) //a loop for each room
{
int M = 0; // initializing rows variable
int N = 0; // initializing columns variable
cin >> M >> N;
vector<vector<int> > matrix(M); //give a matrix with a dimension M*N with all elements set to 0
for (int i = 0; i < M; i++)
matrix[i].resize(N);
for (int i = 0; i < M; i++) //adding each row to the matrix
{
for (int j = 0; j < N; j++) //adding each column to the matrix
{
cin >> matrix[i][j]; //putting all the elements in the matrix
}
}
size_t m = M, n = N;
Node* head = construct(matrix, 0, 0, m, n);
display(head);
return 0;
}
}
else if ((argc == 2) && (string(argv[1]) == "-Queue"))
{
int K;
cin >> K; //this grabs the number of rooms in the dungeon
cout << K;
}
}