c++vectorgraphoutofrangeexception

Out of range error when reading from vector


After reading integers from a txt file and push_back them to a vector for different txt files (all of same structure) i dont get the right result every time.

the first line (first number of the vector) show how many graphs are to be implemented from this file. Then the first number of the second line (second number of the vector) gives the nodes and the second number of the second line (third number of the vector) the edges. After there are the edges number pairs, then again number of nodes and number of edges and so on.

For the first vector my code works. This is what i have written:

int main(int argc, char** argv)  { 
vector<int> read_ints = {2,9,9,6,3,7,1,8,9,6,4,5,1,1,9,2,5,1,4,5,4,9,9,4,6,8,1,3,4,2,8,6,7,9,3,4,7,1,5,4,9};
vector<int> read_ints2 = {10,25, 18,5 ,19,4 ,23,13, 25,4 ,7,2 ,18,11, 4,15, 25,16, 12,2 ,23,5 ,23,24, 19,14, 11,2 ,24,19, 2,14, 16,20, 24,19, 8,18, 3,15, 15,14, 10,15, 4,8 ,10,2 ,3,6 ,11,13, 9,8 ,1,7 ,13,5 ,12,3 ,9,12, 7,1 ,5,15, 2,14, 11,4 ,6,17, 17,9 ,1,10, 17,5 ,11,5 ,14,14, 13,7 ,4,14, 4,10, 8,17, 4,9 ,3,5 ,16,6 ,16,10, 3,2 ,14,1 ,13,15, 17,16, 13,16, 16,6 ,1,13, 5,5 ,8,16, 1,7 ,13,13, 10,10, 3,8 ,14,3 ,2,7 ,5,11, 12,7 ,12,7 ,10,15, 4,13, 15,11, 16,22, 22,11, 13,17, 6,10, 7,8 ,10,10, 21,20, 5,2 ,20,14, 4,3 ,22,7 ,1,4 ,19,2 ,16,11, 8,15, 1,1 ,4,16, 19,9 ,16,11, 3,12, 5,6 ,22,9 ,2,17, 5,18, 18,10, 4,10, 13,3 ,10,17, 12,15, 6,11, 2,6 ,17,7 ,5,14, 16,8 ,2,7 ,8,3 ,1,16, 9,16, 1,14, 12,9 ,5,14, 9,18, 11,20, 20,11, 8,9 ,11,1 ,19,7 ,15,5 ,12,1 ,6,10, 4,2 ,19,20, 7,3 ,8,11, 13,5 ,9,20, 18,10, 16,4 ,6,17, 14,18, 19,17, 5,8 ,10,17, 15,12, 12,6 ,4,12, 2,12, 8,7 ,4,7 ,5,10, 8,1 ,5,1 ,2,10, 6,10, 3,8 ,9,9 ,11,8 ,8,5 ,3,7 ,8,6 ,4,7 ,1,2 ,6,8 ,2,3 ,4,5 ,1,20, 20,6 ,15,14, 7,4 ,17,13, 3,8 ,15,17, 8,12, 18,14, 18,18, 7,9 ,15,11, 5,9 ,8,2 ,20,16, 20,4 ,1,3 ,2,20, 3,14, 10,1 ,5,19, 13};
vector<int>::iterator ints_iter;
int counter = 0; // to count the edges, for testing purposes.
int num_of_graphs = read_ints.at(0);
int num_vertices = read_ints.at(1) + 1; // + 1 because of the code i wrote for implementing the graph's vertices
int num_edges = read_ints.at(2);
read_ints.erase(read_ints.begin(), read_ints.begin() + 3); // deleting the first line (number of graphs) and the first graph's info, so it is easier to add edge's pairs.
for (int i = 0; i < num_of_graphs; i++){
    Graph g(num_vertices); // Graph is the class i made
    for (int i = 0; i < num_edges * 2; i += 2) {
        cout << read_ints.at(i) << " " << read_ints.at(i + 1) << endl; // printing the pairs line by line
        counter++;
    }
    read_ints.erase(read_ints.begin(), read_ints.begin() + (num_edges * 2)); // delete the pairs of the i'th graph every time from the vector to add the new edges on the next graph
    if (read_ints.empty()) // error handling for the last graph (which doesnt work)
        break;
    else 
        int num_vertices = read_ints.at(0) + 1; // after deleting the previous graph info and the previous graphs edges from the vector, i change the i values for next graph's nodes and edges
        int num_edges = read_ints.at(1);
        read_ints.erase(read_ints.begin(), read_ints.begin() + 2); // since the vector is not empty i erase the last elements
}
cout << counter;

return 0; 

For the first vector it works but for a bigger one like the second it says "terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 8) >= this->size() (which is 8)"

after printing out all the edges.


Solution

  • This

        else 
            int num_vertices = read_ints.at(0) + 1;
        int num_edges = read_ints.at(1);
        read_ints.erase(read_ints.begin(), read_ints.begin() + 2);
    

    declares two local variables named num_vertices and num_edges. These are different from the local variables of the same name declared earlier in main, so the values used on the next iteration of the loop are from the first block of data.

    Note that I've also adjusted the indentation to show the how the statements interact with the else. In this case it is harmless because the other branch of the if is a break but you need to be careful that you don't misread your code due to misleading indentation.