从向量读取时超出范围错误

时间:2020-04-24 00:56:40

标签: c++ vector graph outofrangeexception

从txt文件中读取整数并将它们push_back到用于不同txt文件(均具有相同结构)的向量后,我每次都不会得到正确的结果。

第一行(向量的第一个数字)显示了要从该文件中实现多少个图形。 然后,第二行的第一个数字(向量的第二个数字)给出节点,第二行的第二个数字(向量的第三个数)给出边缘。之后是边数对,然后是节点数和边数等等。

对于第一个向量,我的代码有效。这是我写的:

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; 

对于第一个向量,它起作用,但对于第二个向量,它说“终止,在抛出'std :: out_of_range'实例后被调用” what():vector :: _ M_range_check:__n(8)> = this-> size()(8)“

打印完所有边缘后。

1 个答案:

答案 0 :(得分:0)

    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);

声明两个名为num_verticesnum_edges的局部变量。这些与先前在main中声明的同名局部变量不同,因此在循环的下一次迭代中使用的值来自数据的第一块。

请注意,我还调整了缩进以显示语句如何与else交互。在这种情况下,它是无害的,因为if的另一个分支是一个中断点,但是您需要注意不要因为误导缩进而误读代码。