为什么我在执行代码时遇到此错误?在其中我试图为图形实现邻接列表。
#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n,k,i,m=0;
cin >> n;
vector<int> mv[n];
for(i=0;i<n;i++)
{
for(m=0;m<n;m++)
{
scanf("%d",&k);
if(k>0)
mv[i].push_back(k);
}
}
cout << mv[0].at(0) << " ";
cout << mv[0].at(1) << " ";
cout << mv[0].at(2) << " ";
cout << mv[1].at(1) << " ";
cout << mv[2].at(1) << " ";
cout << mv[3].at(0) << " ";
}
return 0;
}
答案 0 :(得分:0)
您可以在不确定自己处于向量范围内的情况下迭代元素...
替换
cout << mv[0].at(0) << " ";
cout << mv[0].at(1) << " ";
cout << mv[0].at(2) << " ";
cout << mv[1].at(1) << " ";
cout << mv[2].at(1) << " ";
cout << mv[3].at(0) << " ";
人:
for(i=0;i<n;i++)
{
for ( size_t pos = 0; pos != mv[i].size(); ++pos )
cout << mv[i].at(pos) << " ";
}
它应该比......更好......