print igraph_vector_ptr_t(矢量列表igraph_vector_t)

时间:2012-04-19 09:52:33

标签: c igraph

我一直在尝试在c ++(On eclipse)中包含C Igraph库,如果你发表评论print_vectorList(&vn1, stdout),这段代码会很好用

cout<<VECTOR(v2)[0]<<endl------------>3        
cout<<VECTOR(v2)[1]<<endl------------>7

如果你没有评论print_vectorList(&amp; vn1,stdout)你得到

 0 1 4 9
 1 0 7 9
 2 3 5 9
 3 2 6 9
 4 0 5 8
 5 2 4 8
 6 3 7 8
 7 1 6 8
 8 4 5 6 7
 9 0 1 2 3

cout<<VECTOR(v2)[0]<<endl------------>0        
cout<<VECTOR(v2)[1]<<endl------------>0

怎么了?

#include <igraph.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>





using namespace std;


void print_vector(igraph_vector_t *v, FILE *f) {
  long int i;
  for (i=0; i<igraph_vector_size(v); i++) {
    fprintf(f, " %li", (long int) VECTOR(*v)[i]);
  }
  fprintf(f, "\n");
}

void print_vectorList(igraph_vector_ptr_t * vl, FILE *f) {
      long int i,n;
      n = igraph_vector_ptr_size(vl);
      for (i=0; i<n; i++) {
        igraph_vector_t * vt;
        vt=(igraph_vector_t*)igraph_vector_ptr_e(vl,i);
        print_vector((igraph_vector_t*)vt,f);
        igraph_vector_destroy(vt);
        free(vt);
      }
}

long int GetElementVectorList(igraph_vector_ptr_t vl,long int i,long int j) {

         igraph_vector_t * vt;
         vt=(igraph_vector_t*)igraph_vector_ptr_e(&vl,i);
         return(VECTOR(*(igraph_vector_t*)vt)[j]);

}

int main() {

  igraph_t g;
  igraph_vector_t v;

  igraph_vector_init(&v,32);
  VECTOR(v)[0]=0; VECTOR(v)[1]=1;
  VECTOR(v)[2]=0; VECTOR(v)[3]=4;
  VECTOR(v)[4]=0; VECTOR(v)[5]=9;
  VECTOR(v)[6]=1; VECTOR(v)[7]=7;
  VECTOR(v)[8]=1; VECTOR(v)[9]=9;
  VECTOR(v)[10]=2; VECTOR(v)[11]=9;
  VECTOR(v)[12]=2; VECTOR(v)[13]=3;
  VECTOR(v)[14]=2; VECTOR(v)[15]=5;
  VECTOR(v)[16]=3; VECTOR(v)[17]=6;
  VECTOR(v)[18]=3; VECTOR(v)[19]=9;
  VECTOR(v)[20]=4; VECTOR(v)[21]=5;
  VECTOR(v)[22]=4; VECTOR(v)[23]=8;
  VECTOR(v)[24]=5; VECTOR(v)[25]=8;
  VECTOR(v)[26]=6; VECTOR(v)[27]=7;
  VECTOR(v)[28]=6; VECTOR(v)[29]=8;
  VECTOR(v)[30]=7; VECTOR(v)[31]=8;

  igraph_create(&g, &v, 0,0);


  // Neighbors

    igraph_vector_ptr_t vn1;
    igraph_vs_t vs;

   igraph_vector_ptr_init(&vn1, 0);
   igraph_vs_vector_small(&vs, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
   igraph_neighborhood(&g, &vn1,vs, 1, IGRAPH_OUT);

   print_vectorList(&vn1, stdout);  // **comment here and.....**


   igraph_vector_t v2;
   igraph_vector_init(&v2,2);
   VECTOR(v2)[0]=GetElementVectorList(vn1,6,1); VECTOR(v2)[1]=GetElementVectorList(vn1,6,2);

   cout<<VECTOR(v2)[0]<<endl;        // **you can get a good result here**
   cout<<VECTOR(v2)[1]<<endl;


  igraph_destroy(&g);
  igraph_vector_destroy(&v);
  igraph_vector_ptr_destroy_all(&vn1);
  igraph_vs_destroy(&vs);
  igraph_vector_destroy(&v2);

  return 0;
}

似乎功能打印出现问题,但我不知道发生了什么。

提前致谢

出发

1 个答案:

答案 0 :(得分:0)

你正在摧毁print_vectorList中的向量 - 当你尝试从main打印时,难怪你之后看不到它的内容。