如何使用std :: valarray来存储/操纵连续的2D数组? [英] How can I use a std::valarray to store/manipulate a contiguous 2D array?

查看:200
本文介绍了如何使用std :: valarray来存储/操纵连续的2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 std :: valarray 来存储/操作2D数组?

How can I use a std::valarray to store/manipulate a 2D array?

d喜欢看具有由行/列索引访问的元素的2D数组的示例。类似这样的伪代码:

I'd like to see an example of a 2D array with elements accessed by row/column indices. Something like this pseudo code:

matrix(i,j) = 42;

如何初始化这样的数组的例子也很不错。

An example of how to initialize such an array would also be nice.

我已经知道Boost.MultiArray,Boost.uBlas和Blitz ++。

I'm already aware of Boost.MultiArray, Boost.uBlas, and Blitz++.

随意回答为什么我不应该使用valarray为我的用例。但是,我想要多维数组的内存是一个连续(列x行)块。没有Java样式的嵌套数组。

Feel free to answer why I shouldn't use valarray for my use case. However, I want the memory for the multidimensional array to be a contiguous (columns x rows) block. No Java-style nested arrays.

推荐答案

我的头顶:

template <class element_type>
class matrix
{
public:
    matrix(size_t width, size_t height): m_stride(width), m_height(height), m_storage(width*height) {  }

    element_type &operator()(size_t row, size_t column)
    {
        // column major
        return m_storage[std::slice(column, m_height, m_stride)][row];

        // row major
        return m_storage[std::slice(row, m_stride, m_height)][column];
    }

private:
    std::valarray<element_type> m_storage;
    size_t m_stride;
    size_t m_height;
};

std :: valarray 经由片,掩码,多维片或间接表来访问元素。请参阅 std :: slice_array std :: gslice_array std :: mask_array std :: indirect_array 了解详情。

std::valarray provides many interesting ways to access elements, via slices, masks, multidimentional slices, or an indirection table. See std::slice_array, std::gslice_array, std::mask_array, and std::indirect_array for more details.

这篇关于如何使用std :: valarray来存储/操纵连续的2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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