0

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
codeling
  • 11,056
  • 4
  • 42
  • 71
Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • what is going wrong? Have you printed the vector, does it contain what you want? – Fantastic Mr Fox Apr 28 '22 at 05:28
  • 2
    Please don't double post the more or less the same question: https://stackoverflow.com/questions/72027359/how-to-input-to-vector-correctly-in-c It had the **exact same problem of not initializing `N` before it was used**, and answers pointing you in that direction. What made you do the same mistake a second time here again? – codeling Apr 28 '22 at 05:52
  • Does this answer your question? [How to input to vector correctly in C++](https://stackoverflow.com/questions/72027359/how-to-input-to-vector-correctly-in-c) – user1810087 Apr 28 '22 at 07:41
  • Did the answers provided here or in your other questions solve your problem? If so, please upvote the ones from which you could learn something and accept the ones that solved your problem. If not, please clarify the problem by editing the question. – codeling Apr 28 '22 at 14:19

1 Answers1

2

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;

codeling
  • 11,056
  • 4
  • 42
  • 71