I’m struggling hard with this. Basically, I have to read a number first:
int n;
scanf("%d", &n);
Then I have to read N intervals to a vector:
vector< vector<int> > intervals;
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
intervals.at(i).at(0) = a;
intervals.at(i).at(1) = b;
}
Using GDB debugger, I get this:
3
1 4
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Program received signal SIGABRT, Aborted.
0x00007ffff7260c35 in raise () from /lib/libc.so.6
(gdb)
Any idea of what I’m doing wrong? Thank you!
Is illegal, since your vector is initially empty. You can either use
push_backor pre-allocate the vector.I would pre-allocate the vector, since this would require no further re-allocation on
push_back: