Creating a c_vector

380 views Asked by At

Is it possible to create a c_vector<int, 3> with the values [12 398 -34] (as an example), in a single line?

As far as I can see the only viable constructor is:

c_vector(vector_expression<AE> const&)

which takes a VectorExpression, which seem to be all the other kind of vectors, like zero_vector and scalar_vector that are dynamically allocated.

Is there something like a std::initializer_list<T> constructor that I can use? Or what VectorExpression should I use for this simple task?

1

There are 1 answers

1
sehe On BEST ANSWER

I stumbled upon this in the assignment.hpp header:

v <<= 1,2,3;

I suppose there might be more useful methods in there, if you look closely. E.g.

vector<double> a(6, 0);
a <<= 1, 2, move_to(5), 3;

will result in: 1 2 0 0 0 3 and

matrix<double> A(3, 3, 0);
A <<= 1, 2, next_row(),
3, 4, begin1(), 1;

will result in:

1 2 1
3 4 0
0 0 0

Live On Coliru

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/traits/c_array.hpp>
#include <boost/numeric/ublas/assignment.hpp>

namespace ublas = boost::numeric::ublas;

int main() {
    using V = ublas::c_vector<int, 3>;

    V v;
    v <<= 1,2,3;

    for (auto i : v)
        std::cout << i << " ";
}

Prints

1 2 3