Search code examples
c++boostcgns

boost::numeric::ublas::vector<double> and double[]


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?


Solution

  • 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;
    }