|
From: | Gábor Csárdi |
Subject: | Re: [igraph] Using std::vector with Igraph |
Date: | Wed, 25 Apr 2012 11:38:49 -0400 |
I've been trying to find a really function that let me convert from std::vector of vector to igraph_vector_ptr_t and the opposite way.
The reason is, there are different classes like a std::sort, std::unique and others, that let us manipulate vectors, this function do not exist in Igraph libraries, because the library is a C library.
The next code only show the code to convert from std::vector to igraph_vector_ptr_t, the problem is that it doesn't work, please help me to help me to improve it. This code it doesn't work :(#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;
long int l=igraph_vector_size(v);
for (i=0; i<l; i++) {
fprintf(f, " %li", (long int) VECTOR(*v)[i]);
}
fprintf(f, "\n");
}
void Stl_To_Igraph_vector_ptr_t(std::vector< std::vector< long int > > & vectR, igraph_vector_ptr_t * vl) {
long int i,j,n;
igraph_vector_t vt;
igraph_vector_init(&vt,4);
for (i = 0; i< 2; i++) {
for (j = 0; j< 4; j++) {
//cout << vectR.at(i).at(j) << " ";
VECTOR(vt)[j]=vectR.at(i).at(j);
}
//cout<<endl;
igraph_vector_ptr_set(vl, i,&vt);
}
}
int main() {
// Set STL Vectors
std::vector< std::vector< long int > > vectR;
std::vector<long int> vectRi1,vectRi2;
long int myarray1 [] = { 1,2,3,4 };
long int myarray2 [] = { 5,6,7,8 };
vectRi1.insert(vectRi1.begin(), myarray1, myarray1+4);
vectRi2.insert(vectRi2.begin(), myarray2, myarray2+4);
vectR.push_back(vectRi1);
vectR.push_back(vectRi2);
// Convert From Stl vector to Igraph_vector_ptr_t
igraph_vector_ptr_t vIgraph;
igraph_vector_ptr_init(&vIgraph, 2);
Stl_To_Igraph_vector_ptr_t(vectR, &vIgraph);
// Print and distroyed
long int i;
for (i=0; i<igraph_vector_ptr_size(&vIgraph); i++) {
print_vector((igraph_vector_t *)VECTOR(vIgraph)[i],stdout);
igraph_vector_destroy((igraph_vector_t *)VECTOR(vIgraph)[i]);
free((igraph_vector_t *)VECTOR(vIgraph)[i]);
}
return 0;
}
_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help
[Prev in Thread] | Current Thread | [Next in Thread] |