Create c_vector

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

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

c_vector(vector_expression<AE> const&)

which accepts VectorExpression, which appear to be all other types of vectors, such as zero_vectorand scalar_vector, that are dynamically allocated.

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

+4
source share
1 answer

I came across this in the header assignment.hpp:

v <<= 1,2,3;

I suggest that there might be more useful methods if you look carefully. For instance.

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

: 1 2 0 0 0 3

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

:

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

1 2 3 
+2

All Articles