Initializing a Ublas Vector from a C Array

Initializing a ublas vector from a C array

Both std::vector and ublas::vector are containers. The whole point of containers is to manage the storage and lifetimes of their contained objects. This is why when you initialize them they must copy values into storage that they own.

C arrays are areas of memory fixed in size and location so by their nature you can only get their values into a container by copying.

You can use C arrays as the input to many algorithm functions so perhaps you can do that to avoid the initial copy?

Initializing boost matrix with a std::vector or array

According to the boost matrix documentation, there are 3 constructors for the matrix class: empty, copy, and one taking two size_types for the number of rows and columns. Since boost doesn't define it (probably because there are many ways to do it and not every class is gong to define a conversion into every other class) you are going to need to define the conversion.

Here's an approach that I would use, but since there are multiple ways to do this and the question doesn't specify how you want this done you may find a different approach more applicable to your situation.

void Foo(const std::vector<double> & v) {
size_t m = ... // you need to specify
size_t n = ... // you need to specify

if(v.size() < m * n) { // the vector size has to be bigger or equal than m * n
// handle this situation
}

matrix<double> mat(m, n);
for(size_t i=0; i<mat.size1(); i++) {
for(size_t j=0; j<mat.size2(); j++) {
mat(i,j) = v[i+j*mat.size1()];
}
}
}

A couple of notes about your provided code: std::vector needs a templated argument and you are declaring m as a matrix and an input argument to it's constructor.

Read ublas vectors from binary file or init it with array[]

You can use something like this

   double array[] = {1., 2., 3.};
boost::numeric::ublas::vector<double> v(sizeof(array) / sizeof(*array));
std::copy(array, array + sizeof(array) / sizeof(*array), v.data().begin());

How to create a nonempty boost ublas::vector inside an initializer list?

You may change the default storage type of an ublas/vector from unbounded_array to std::vector to get initializer_list support or introduce a helper function to initialize the member:

#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;

template <typename T>
unbounded_array<T> make_unbounded_array(std::initializer_list<T> list) {
unbounded_array<T> result(list.size());
for(unsigned i = 0; i < list.size(); ++ i)
result[i] = *(list.begin() + i);
return result;
}

class Example
{
public:
vector<double, std::vector<double>> v0;
vector<double> v1;

public:
Example()
: v0({ 1.25, 2.75, 3.34 }),
v1(make_unbounded_array({ 1.25, 2.75, 3.34 }))
{}
};

int main(int argc, char **argv) {
Example example;
std::cout
<< example.v0[0] << " == " << example.v1[0] << '\n'
<< example.v0[1] << " == " << example.v1[1] << '\n'
<< example.v0[2] << " == " << example.v1[2] << '\n'
;
}

What is the easiest way to initialize a std::vector with hardcoded elements?

One method would be to use the array to initialize the vector

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );

how to initialize a vector of array

I don't know what data type you used a BYTE. Here how it works with int array.

#include <iostream>
#include <vector>

int main()
{
std::vector<int*> cKey;
int keyTemp[8] = { 1,2,3,4,5,6,7,8 };
cKey.push_back(keyTemp);
std::cout << cKey[0][6] << std::endl;
system("pause");
return 0;
}

How to initialize all members of an array to the same value?

Unless that value is 0 (in which case you can omit some part of the initializer
and the corresponding elements will be initialized to 0), there's no easy way.

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...

So this will initialize all elements to 0:

int myArray[10] = { 0 }; // all elements 0

In C++, an empty initialization list will also initialize every element to 0.
This is not allowed with C until C23:

int myArray[10] = {}; // all elements 0 in C++ and C23

Remember that objects with static storage duration will initialize to 0 if no
initializer is specified:

static int myArray[10]; // all elements 0

And that "0" doesn't necessarily mean "all-bits-zero", so using the above is
better and more portable than memset(). (Floating point values will be
initialized to +0, pointers to null value, etc.)

how-to initialize 'const std::vectorT' like a c array

For C++11:

vector<int> luggage_combo = { 1, 2, 3, 4, 5 };

Original answer:

You would either have to wait for C++0x or use something like Boost.Assign to do that.

e.g.:

#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope

vector<int> v;
v += 1,2,3,4,5;


Related Topics



Leave a reply



Submit