tags:

views:

50

answers:

2

I'm using boost for matrix and vector operations in a code and one of the libraries I am using (CGNS) has an array as an argument. How do I copy the vector into double[] in a boost 'way', or better yet, can I pass the data without creating a copy?

I'm a bit new to c++ and am just getting going with boost. Is there a guide I should read with this info?

+2  A: 

Contents between any two input iterators can be copied to an output iterator using the copy algorithm. Since both ublas::vector and arrays have iterator interfaces, we could use:

#include <boost/numeric/ublas/vector.hpp>
#include <algorithm>
#include <cstdio>

int main () {
    boost::numeric::ublas::vector<double> v (3);
    v(0) = 2;
    v(1) = 4.5;
    v(2) = 3.15;

    double p[3];

    std::copy(v.begin(), v.end(), p);   // <--

    printf("%g %g %g\n", p[0], p[1], p[2]); 

    return 0;
}
KennyTM
Can it be done without a copy? Ty for the copy method!
ccook
@ccook: It is an implementation detail, but you could use `v.data().begin()` to get a `double*` from the `ublas::vector<double>`.
KennyTM
Exactly what I needed, thank you!
ccook
+1  A: 

Depends on the types involved. For std::vector you just make sure that it's non-empty and then you can pass &v[0]. Most likely the same holds for the Boost types you're using.

Alf P. Steinbach
Does this work for a boost vector?
ccook