从C数组初始化ublas矢量 [英] Initializing a ublas vector from a C array

查看:178
本文介绍了从C数组初始化ublas矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++ ublas库编写了一个Matlab扩展,我想能够从Matlab插值器传递的C数组初始化我的ublas矢量。
如何从C数组初始化ublas矢量,而不是(为了效率)显式复制数据。我正在寻找以下代码行:

I am writing a Matlab extension using the C++ ublas library, and I would like to be able to initialize my ublas vectors from the C arrays passed by the Matlab interpeter. How can I initialize the ublas vector from a C array without (for the sake of efficiency) explicitly copying the data. I am looking for something along the following lines of code:

using namespace boost::numeric::ublas;

int pv[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
vector<int> v (pv);

一般来说,可以初始化一个C ++ std :: vector 从数组?像这样:

In general, is it possible to initialize a C++ std::vector from an array? Something like this:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int pv[4] = { 4, 4, 4, 4};
    vector<int> v (pv, pv+4);

    pv[0] = 0;
    cout << "v[0]=" << v[0] << " " << "pv[0]=" << pv[0] << endl;

    return 0;
}

但初始化不会复制数据。在这种情况下,输出为

but where the initialization would not copy the data. In this case the output is

v[0]=4 pv[0]=0

但我想要的输出是相同的,其中更新C数组更改C ++向量指向的数据

but I want the output to be the same, where updating the C array changes the data pointed to by the C++ vector

v[0]=0 pv[0]=0


推荐答案

std :: vector / code>是容器。容器的整体点是管理它们所包含的对象的存储和寿命。这是为什么当你初始化它们,他们必须将值复制到他们自己的存储。

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数组是内存的大小和位置固定的区域,

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.

您可以使用C数组作为许多算法函数的输入,所以也许你可以这样做,以避免初始副本?

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

这篇关于从C数组初始化ublas矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆