Out of Range Vector / Range Check

new user here. I am writing this code that creates a network with nodes and uses a random number to create edges between them. I track the entire graph as a vector, each entry is a vector representing a node whose elements are its neighbors. He then uses the depth search to find the number of components that are divided parts of the graph (my account variable). Then I output the node and the number of neighbors to which it is connected to a txt file. The code compiles, but an error message appears on the command line:

ending a call after calling an instance of 'std :: out_of_range' what (): vector :: _ M_range_check

This application asked Runtime to terminate it in an unusual way. Please contact support ...

So ... what does it mean and how can I fix it?

Also, do I need to keep track of how many nodes are in each component, any ideas?

Thanks in advance, here is my code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;

void gengraph(int v, float p, vector <vector <int> >& G);
void DFS(vector <vector <int> > G, int v, vector<int>& M);

int main()
{
    int a = 1000;
    float b = 0.004;
    vector <vector <int> > G;
    gengraph(a,b,G);
    vector <int> M (1000);
    int count = 0;
    int i;
    for (i = 0; i < a; i++)
    {
        if (M[i]==0)
        {
            DFS(G, i, M);
            count += 1;
        }
    }
    ofstream myfile;
    myfile.open ("data.txt");
    for (int l=0; l<1000; l++)
    {
        myfile << "v   len(G[v])\n";
    }
    myfile.close();
}
void gengraph(int v, float p, vector <vector <int> >& G)
{
    for (int i = 0; i<1000; i++)
    {  
        for (int j = 0; j<1000; j++)
        {
            int y = rand();
            bool Prob = (y <= p);
            if (i == j)
                continue;
            else
            {
                if(Prob == true)
                {
                    G.at(i).push_back (j);
                    G.at(j).push_back (i);
                }
            }
        }
    }
}
void DFS(vector <vector <int> >& G, int v, vector<int>& M)
{
    M[v]=1;
    for(unsigned int j = 0; j < G[v].size(); j++)
    {
        if (M[j]==0)
        {
            DFS(G, j, M);
        }
    }
}
+5
source share
4 answers

You have created a vector> but it has an initial size of 0.

Now, when you access it using the M.at () command, it checks to see if this index is out of bounds and throws an exception if this is the case.

defining vector as:

vector<vector<int> > M(1000);

should solve your problem.

You should also use gdb or another debugger. it will make your life much easier.

+1
source
vector <vector <int> > G;

-of-int, -of-int. , G.at(i)..., - i, .

, rand() 32- , 0,004 float. - (rand() % 1000000 / 1000000.0). ala srand(time(NULL));

std::cerr << "x is now " << x << '\n'; - , .. - , , , . , , .

+1

, .

vector::at() . , , - , G.

0
source

First mistake: in the gengraph () function, you use an empty vector:

G.at(i) ...
G.at(j) ...

Yes, you call push_back, but you call it which method returned on (). But at () cannot return anything that matters i and j, since your vector is empty. One solution puts this line at the beginning of gengraph ()

G.resize( 1000 );

Second tip: Avoid using magic numbers as much as possible. Put something like:

const int size = 1000; 

At the beginning of your file and use size instead of magic number 1000. Especially this:

int a = 1000;
float b = 0.004;
vector <vector <int> > G;
gengraph(a,b,G);
vector <int> M (1000); // use a here and better call it something more meaningfulness than a
int count = 0;
int i;
for (i = 0; i < a; i++) { // or use M.size() here or both
}
0
source

All Articles