hiteshn97 hiteshn97 - 3 months ago 23
C++ Question

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.

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.!

Answer

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)

cin>>jail[i][j]

Never seen that work before. cin in to a temporary and then push.

int temp;
std::cin >> temp;
jail[i].emplace_back(temp);