【发布时间】:2020-08-20 23:52:24
【问题描述】:
当我尝试运行我的代码时,它编译得很好,但是在运行时它给出了一个超出范围的向量错误。谁能帮帮我?
我已经在 Xcode 中编写了我的代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numOfRows = 0;
cout << "Enter number of rows: ";
cin >> numOfRows;
vector<vector<int>> vec;
int sizeOfAnotherArray = 0;
int value = 0;
for (int i = 0; i < numOfRows; i++) {
cout << "Enter size of another array: ";
cin >> sizeOfAnotherArray;
vec.resize(numOfRows,vector<int>(sizeOfAnotherArray));
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << "Store Value: ";
cin >> value;
vec.at(i).at(j) = value;
}
}
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << vec.at(i).at(j) << " ";
}
cout << "\n";
}
return 0;
}
【问题讨论】:
标签: c++ vector compiler-errors indexoutofboundsexception