-3

I am trying to execute this code but it gives a runtime error everytime on Ideone. What is the reason for this error?

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() 
{
    signed int t;
    cin >> t;
    while(t--)
    {
        int p, q;
        cin >> p >> q;
        vector<pair<pair<int,int>,int >> v;
        int i = 0;
        while(q--)
        {
            cin >> v[i].first.first >> v[i].first.second >> v[i].second;
            i++;
        }
        for(int j=i; j>=0; j--)
        {
            cout << v[i].first.first << v[i].first.second << v[i].second;
        }

    }

    return 0;
}

How to determine which runtime error is occuring?

Anselmo GPP
  • 425
  • 2
  • 21
darth_vader
  • 149
  • 10
  • 3
    Unrelated to your problem, but please read [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jun 19 '18 at 04:32
  • 12
    As for your problem, if you read [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) they would probably tell you that vectors start out *empty* and all indexing in them will be invalid. In fact, it leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). – Some programmer dude Jun 19 '18 at 04:33

2 Answers2

1

Your runtime error is because you are attempting to access v[i] prior to initializing any elements in the vector. I would recommend you pass in the number of elements q into the constructor to initialize the vector with q elements of pair<pair<int,int>,int> to their default state.

int p,q;
cin>> p >> q;
vector<pair<pair<int,int>,int>> v(q);
int i=0;
while(q--)
{
    cin>>v[i].first.first>>v[i].first.second>>v[i].second;
    i++;
}

I would also recomend looking at Choice between vector::resize() and vector::reserve() which explains 2 other ways to initialize a vector - (resize / reserve).

In short, resizing is similar to that of the constructor mentioned in the code above, except it can be called anytime after construction to suddenly change entire vector length.

Reserve on the other hand does not actually change the vector's length, but changes the vector's underlying capacity (max number of elements that can be stored in the vector prior to the vector having to change it's underlying size). Once a vector has been reserved, push_back() is a very useful function to effectively grow the vector by 1 without increasing the capacity (assuming that initial reserved capacity was large enough).

So for the code above, you could also write it like this:

int p,q;
cin>> p >> q;
vector<pair<pair<int,int>,int>> v;
v.reserve(q) ///reserve large enough here, max capacity here is just q elements
int i=0;
pair<pair<int,int>,int> input;
while(q--)
{
    cin>>input.first.first>>input.first.second>>input.second;
    v.push_back(input);
    i++;
}

In code above your vector's underlying capacity will be q elements, and after while loop finishes it will have a length of q elements as well. The next push_back() operation will be an expensive operation.

Ilan Keshet
  • 514
  • 5
  • 19
1

Try changing vector<pair<pair<int,int>,int>> vto vector<pair<pair<int,int>,int>> v(q) to avoid runtime error.

Or you can also use

vector<pair<pair<int,int>,int>> v; v.push_back({{1,2},3}); // v.push_back( make_pair (make_pair(1,2)) , 3 )

And also change cout<<v[i].first.first<<v[i].first.second<<v[i].second to cout<<v[j].first.first<<v[j].first.second<<v[j].second to see expected result.

Hope this helps!