We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int n,q;
cin >> n >> q;
vector< vector<int> > a(n);
// input each array
for (int i=0;i<n;i++) {
int k;
cin >> k;
for (int j=0;j<k;j++) {
int data;
cin >> data;
a[i].push_back(data);
}
}
// do the queries
for (int i=0;i<q;i++) {
int x,y;
cin >> x >> y;
cout << a[x][y] << endl;
}
Yes, you can take 'int k' and 'int data' out of the for loops and declare them outside. This is for scoping purpose (that is, to be 'defensive' in programming). This is just in case there is a 'k' somewhere else in the code too. The 'k' or 'data' inside the for loops will be different from those elsewhere.
He is declaring a matrix of vector (in this case, vector filled with vectors), a is the var name and (n) is a resize of the "parent" vectors, which is, the one who contains other vectors.
This is very close to my implementation except i loaded the data all at once into a temporary vector created in the loop and pushed the data vector into the top level vector directly.
Variable Sized Arrays
You are viewing a single comment's thread. Return to all comments →
Thanks a lot rwan7727, i was wondering how to input each array. It helped me.
Why you don't take the int k & int data out of the for loop?
Yes, you can take 'int k' and 'int data' out of the for loops and declare them outside. This is for scoping purpose (that is, to be 'defensive' in programming). This is just in case there is a 'k' somewhere else in the code too. The 'k' or 'data' inside the for loops will be different from those elsewhere.
why should we use "a[i].push_back(data)"?Is there anything wrong in using "a[i,j]" instead of that?I didn't get the output.
can u convert this in c.because i dont know c++
Here u can find C code
segmentation fault
Can you explain it please?
Thank you bro! it's perfect solution...
Can anyone explain line 3 ?
vector< vector > a(n)
He is declaring a matrix of vector (in this case, vector filled with vectors), a is the var name and (n) is a resize of the "parent" vectors, which is, the one who contains other vectors.
which languague is it?
C++ it is..
Filling of vector also can be done in the following way:
That reserve() was exectly what I was lacking. Thanks!
No one use dynamic array?
thankyou soo much; my frnd it s really halpfull for me :)
This is very close to my implementation except i loaded the data all at once into a temporary vector created in the loop and pushed the data vector into the top level vector directly.
less lines and simple. Good.
vector< vector > a(n);
you reallly made it simple by seperating the data from the k value, this one really helped me thanks now I understand :)
helped alot for taking the input
I'm still failing 9/11 when they're doing vectors of size 10,000 and 100,000.