【发布时间】:2016-10-21 20:41:01
【问题描述】:
我有一个更新 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_array 或array 的大小。请注意,我使用 C++14。使用 std::move() 没有帮助。
【问题讨论】: