Home > Blockchain >  How to input 2D vector correctly?
How to input 2D vector correctly?

Time:05-04

When I try to set and populate a 2D vector as follows, I couldn't input int numbers to them. I try to input numbers usinga range-based for loop; but it doesn't finish after the expected number of loop cycles.

#include <iostream>
#include <vector>
using namespace std;

int main(){
    int N;
    vector<vector<int>> A(N,vector<int>(6)); 
    cin>>N;
    for(auto row_vec:A) {
        for(auto x:row_vec) cin>>x;
    }
    return 0;
}

What is wrong with that code?

The following is my test input on the console:

[ec2-user@ip-10-0-1-187 atcoder]$ ./052
2
1 2
2 4
2 4

CodePudding user response:

When running your code on linux you for example, I got:

terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()

If you encountered this, it should have been part of the question; if not, you still should have reported more on the actual behavior you saw; whether you could input some numbers for example. "it didn't finish" is an unclear problem description. You probably meant that it kept on asking for numbers, despite you having entered the amount of numbers that you expected it to ask for already?

With the above hint, the first thing should be to check the vector initialization. You declare the overall size N, but where do you initialize that? It is only set after it is used! So it tries to allocate a more or less random, unknown amount of memory using whatever value happens to be in the memory location where N will be stored. You need to move the reading of N before the initialization of A:

    cin>>N;
    vector<vector<int>> A(N,vector<int>(6));

As a side note to your shown input, note that cin doesn't distinguish between space and line break as separator; so with the fixed size of 6 of the "second level" vector, with an overall size of 2, your loop would expect 12 inputs.

Additionally, the row_vec and x variables are created as copies of the respective parts of your vector - meaning that after your reading loop, the elements in A will still all be 0, since only copies of it were set. You have to explicitly say that you want to take references to the parts of A by declaring the loop variables as auto &. Full code for reading N times 6 variables (and printing them to see whether it worked):

#include <iostream>
#include <vector>

int main()
{
    int N;
    std::cin>>N;
    std::vector<std::vector<int>> A(N,std::vector<int>(6));
    for(auto & row_vec:A)
    {
        for(auto & x:row_vec)
        {
            std::cin>>x;
        }
    }
    // print A to verify that values were set from input:
    for (auto const & row: A)    // note the use of const & to avoid copying
    {
        for (auto const & cell: row)
        {
            std::cout << cell << " ";
        }
        std::cout << "\n";
    }
    return 0;
}

One last note: It's recommended to not use using namespace std;

  • Related