Varibale sized arrays in c++

 Here is a simple code to create variable sized arrays in c++ :

(the code is from hackerrank problems)

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n, q;
    vector<vector<int>> vectors;
    
    cin>>n;
    cin>>q;
    for(int i=0;i<n;i++){
        int size;
        cin>>size;
        vector<int> arr;
        for(int j=0;j<size;j++){
            int el;
            cin>>el;
            arr.push_back(el);
        }
        vectors.push_back(arr);
    }
    for(int i=0; i<q; i++){
        int p1,p2;
        cin>>p1>>p2;
        cout<<vectors[p1][p2]<<endl;
    }
    return 0;
}

Comments