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.
精彩评论