[英]input 2D vector in main() c++
I am novice in vectors, I am tryind to input this 2D vector in main() function, but unable to do so. 我是向量的新手,我尝试在main()函数中输入此2D向量,但无法这样做。
int main()
{
int t, x, n;
cin>>n;
vector< vector <int> > jail(n);
for(int i=0; i<n; i++){
jail[i].reserve(n);
for(int j=0; j<n; j++){
cin>>jail[i][j];
}
}
cout<< jailBreak(jail,n-1,0,0)<<endl;
}
Runtime error is that I need to input an garbage input in the beginning of the program. 运行时错误是我需要在程序的开头输入垃圾输入。 This ambiguous input has been bothering me for a long time now, thanx in advance for any advice on this.! 这种模糊的输入已经困扰我很长时间了,事先感谢thanx的任何建议。
this line: 这行:
jail[i].reserve(n);
just tells vector
to pre-allocate memory (it's just a hint to optimize further reallocs on push_back
operations but does not guarantee allocation). 只是告诉vector
预分配内存(这只是在push_back
操作上优化进一步重新分配的提示,但不能保证分配)。 You have to use resize
instead which really allocates memory. 您必须改用resize
来真正分配内存。
In your code: 在您的代码中:
for(int i=0; i<n; i++){
jail[i].reserve(n);
for(int j=0; j<n; j++){
cin>>jail[i][j];
}
}
jail[i].reserve(n);
should be jail[i].resize(n)
应该是jail[i].resize(n)
cin>>jail[i][j]
Never seen that work before. 从未见过这项工作。 cin
in to a temporary and then push. cin
进入临时位置,然后推动。
int temp;
std::cin >> temp;
jail[i].emplace_back(temp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.