How to print a vector

Hi, I am new to both igraph an C/C++. Recently, I found there’s a problem with my code that I cannot print out the vector I want.

int main() {
    igraph_t graph;
    igraph_vector_int_t neis;

    // create a graph
    igraph_empty(&graph, 6, IGRAPH_UNDIRECTED);
    igraph_add_edge(&graph, 0, 1);
    igraph_add_edge(&graph, 0, 2);
    igraph_add_edge(&graph, 0, 3);
    igraph_add_edge(&graph, 1, 4);
    igraph_add_edge(&graph, 1, 5);


    // get the neighbors of vertex 0
    igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL);

    // print out the vector
    for (int i = 0; i < igraph_vector_size(&neis); i++)
    {
        printf(" %d ", (int)igraph_vector_e(&neis, i));
    }
    printf("\n");
  
    
    igraph_destroy(&graph);

    return 0;
}

Terminal showed error:
*error C2664: 'igraph_real_t igraph_vector_e(const igraph_vector_t ,igraph_integer_t)’

I don’t know what I did wrong. How to print out all of the neighbors of vertex 0?

I found another problem. If I type igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL); in the code, the terminal would not show errors, but any outcome will disappear.

Like this,

int main() {
    igraph_t graph;
    igraph_vector_int_t neis;

    // create a graph
    igraph_empty(&graph, 6, IGRAPH_UNDIRECTED);
    igraph_add_edge(&graph, 0, 1);
    igraph_add_edge(&graph, 0, 2);
    igraph_add_edge(&graph, 0, 3);
    igraph_add_edge(&graph, 1, 4);
    igraph_add_edge(&graph, 1, 5);


    // get the neighbors of vertex 0
    igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL);

    cout<<"Hello"<<endl;
  
    
    igraph_destroy(&graph);

    return 0;
}

It will not print Hello in the terminal. However, if I delete igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL); there will be Hello in terminal.

In C, every data structure will need to be explicitly initialised and freed.

In this case, you need to make sure to first initialise the vector neis by doing

    igraph_vector_int_init(&neis, 0);

The second argument is the length of the vector that is allocated. The actual length of the vector does not matter much, since the igraph_neighbours function will properly resize the vector as needed. However, it does require the vector data structure to be initialised before use.

After use, you can destroy the vector using igraph_vector_int_destroy().

You may want to take a closer look at the tutorial.

1 Like

Thank you! My second problem was solved. But the first problem still exists.

I cannot print out all of the neighbors of vertex 0 with the code below

    for (int i = 0; i < igraph_vector_size(&neis); i++)
    {
        printf(" %d ", (int)igraph_vector_e(&neis, i));
    }
    printf("\n");

What’s wrong with my code?

Note that the type is igraph_vector_int_t not igraph_vector_t (note the int). Hence, you should use igraph_vector_int_size and igraph_vector_int_e.

Note that igraph_vector_int_e is deprecated, and that it is replaced by igraph_vector_int_get. However, in most cases, you should use VECTOR(neis)[i] instead of igraph_vector_int_get(&neis, i). Note that you use neis when using VECTOR, not &neis. You can read more about this in the tutorial.

Thank you! That really helps a lot!