在更改其大小的循环中更新boost multi_array变量

时间:2016-10-21 20:41:02

标签: c++ c++11 boost c++14

我有一个更新boost::multi_array更改大小的函数。有什么方法可以在循环中更新数组(即在初始化后更新它),当它改变大小时?如何在初始化后更新(分配)boost::multi_array

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

typedef boost::multi_array<int, 2> A;

A update(const A& a) {
    auto s = a.shape()[0];
    A b{boost::extents[s+s][3]};
    auto b_mid = std::copy(a.begin(), a.end(), b.begin());
    std::copy(a.begin(), a.end(), b_mid);
    return b;
}

int main() {
    A a {boost::extents[5][3]};
    int r = 5;
    while (r--) {
        a =             // causes error because the size has changed the sizes of RHS and LHS don't match.
            update(a);  // Alternatively: a = std::move(update(a));
    }
}

问题是A类型的赋值运算符无法更改multi_arrayarray的大小。请注意,我使用的是C ++ 14。使用std::move()没有帮助。

1 个答案:

答案 0 :(得分:1)

.reshape呼吁成为你想要的。

相关问题