gpt4 book ai didi

c++ - 调整 boost::multi_array 的大小以匹配另一个

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:25 27 4
gpt4 key购买 nike

我需要将一个 multi_array 的大小调整为另一个的大小。

在 Blitz++ 中我可以做到

arr1.resize(arr2.shape());

是否有类似长度的多数组解决方案?因为

arr1.resize(boost::extents[arr2.shape()[0]][arr2.shape()[1]]);

似乎有点长和费力。

最佳答案

您可以使用 shape() 成员。遗憾的是,它不能直接用作 ExtentList(它不模拟 Collection 概念),但很容易将其合二为一:

using MA = multi_array<double, 2>;
MA ma(extents[12][34]);
auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());

这样

// demo
std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]\n";

打印[12][34]

现在,ma_shape 可以直接用于重新整形/调整另一个数组的大小:

Live On Coliru

#include <boost/multi_array.hpp>
#include <iostream>

int main() {
using namespace boost;
using MA = multi_array<double, 2>;

MA ma(extents[12][34]);
auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());

// demo
std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]\n";

// resize
MA other;
assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

other.resize(ma_shape);
assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

// reshape
other.resize(extents[1][12*34]);
assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

other.reshape(ma_shape);
assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
}

关于c++ - 调整 boost::multi_array 的大小以匹配另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com