dimanche 3 novembre 2019

reading variable length input in C++ using stream

I have following requirement which is to be read in to program.

The first line contains two space-separated integers denoting the respective values of p (the number of variable-length arrays) and q(the number of queries). Each line of the subsequent lines contains a space-separated sequence for each element in to array .

Each of the subsequent lines contains two space-separated integers describing the respective values of i (an index in array ) and j (an index in the array referenced by ) for a query.

2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

In example above i have 2 arrays and 2 queries. First array is 3,3,5, 4 and second array 5 1 2 8 9 3.

My question is how can I read this data in my container. Note: I cannot enter input from console, here some test program provides input.

I have written as below

int iNoOfVectors = 0;
    int iNoOfQueries = 0;
    cin >> iNoOfVectors >> iNoOfQueries;
    cout << iNoOfVectors ; 
    vector<vector<int>> container;
    container.reserve(iNoOfVectors);
    for(int i = 0; i < iNoOfVectors; i++ ) {
        int temp;
        std::string line;
        while (std::getline(std::cin, line))
        {
            std::cout << line << std::endl;
        }
    }

output of above

2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

How can I get variable length array elements in to my container.

Thanks

Aucun commentaire:

Enregistrer un commentaire