我无法使用矢量头文件

时间:2015-06-04 11:22:58

标签: visual-studio-2010 c++11

我想使用矢量头文件,但我猜它中有错误。我附上了包含错误的图像文件。当我运行此代码时,我得到图像中显示的错误。请告诉我如何解决它。

#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;
int main()
{
vector<vector<int>> rmat;
vector<int> r = { 1, 2, 3, 4, 5 };

for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
{
    rmat[i][j] = r[j];
}

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 5; j++)
        cout << rmat[i][j] << "\t";
    cout << "\n";
}


_getch();
return 0;
}

Error on runtime

1 个答案:

答案 0 :(得分:1)

阅读错误消息:

  

向量下标超出范围

您已创建空矢量

vector<vector<int>> rmat;

然后访问其不存在的元素

rmat[i][j] = r[j];

导致这种未定义的行为,在您的情况下会导致抛出一个有用的异常。

抓住a good book on C++并首先学习基础知识。

相关问题