ccook
ccook

Reputation: 5959

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?

Upvotes: 2

Views: 1493

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

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.

Upvotes: 0

kennytm
kennytm

Reputation: 523344

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

Upvotes: 3

Related Questions