开发者

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

开发者 https://www.devze.com 2023-01-19 19:05 出处:网络
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 argum开发者_C百科ent.How do I copy the vector into double[] in a boost \'way\'

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 argum开发者_C百科ent. 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消