我想知道一个简单的方法来找到一个 boost 多数组的最大/最小元素,一个包含 3 个索引的对象,如下所示:
int iDepth=10,iWidth=10,iHeight=10;
boost::multi_array<GLfloat, 3> image(boost::extents[iDepth][iWidth][iHeight]);
Run Code Online (Sandbox Code Playgroud) 似乎没有multi_array移动构造函数 - 这是正确的吗?这是否有原因,或者它只是从未实现,因为该类似乎是在移动语义可用之前编写的?在用户领域有什么可以做的吗?
我对这段代码有问题:
#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <vector>
#include <iostream>
template <typename Vec>
void foo(Vec& x, size_t N)
{
for (size_t i = 0; i < N; ++i) {
x[i] = i;
}
}
int main()
{
std::vector<double> v1(10);
foo(v1, 5);
std::cout << v1[4] << std::endl;
boost::multi_array<double, 2> m1;
boost::array<double, 2> shape;
shape[0] = 10;
shape[1] = 10;
m1.resize(shape);
foo(m1[0], 5);
std::cout << m1[0][4] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试用gcc编译它,我收到错误:
boost_multi_array.cpp: In function 'int main()':
boost_multi_array.cpp:26: error: invalid initialization of non-const …Run Code Online (Sandbox Code Playgroud) 范围可用于切片Boost多维数组(multi_array).根据文档,有几种方法可以定义范围,但并非所有方法都可以编译.我在Ubuntu 11.04上使用GCC 4.5.2.
#include <boost/multi_array.hpp>
int main() {
typedef boost::multi_array_types::index_range range;
range a_range;
// indices i where 3 <= i
// Does compile
a_range = range().start(3);
// Does not compile
a_range = 3 <= range();
a_range = 2 < range();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出是:
ma.cpp: In function ‘int main()’:
ma.cpp:9:26: error: no match for ‘operator<=’ in ‘3 <= boost::detail::multi_array::index_range<long int, long unsigned int>()’
ma.cpp:10:25: error: no match for ‘operator<’ in ‘2 < boost::detail::multi_array::index_range<long int, long unsigned int>()’
Run Code Online (Sandbox Code Playgroud)
知道如何编译这个,或者缺少什么?
我用G ++ 4.7组建了一个前沿设置(虽然目前我还在使用sudo apt-get boost-all-devDebian Wheezy 附带的boost 1.48 ).
我的代码设置在使用的逻辑数据结构是unique_ptr的多维数组.但是multi_array,如果其中有unique_ptr,似乎也无法构造一个空的单元素数组.这样可行:
boost::multi_array<int, 1> arrWorks (boost::extents[1]);
Run Code Online (Sandbox Code Playgroud)
但这不是:
boost::multi_array< unique_ptr<int>, 1> arrFails (boost::extents[1]);
Run Code Online (Sandbox Code Playgroud)
我想编译器的相关抱怨是:
/usr/include/c++/4.7/bits/stl_uninitialized.h:225:从'void std :: uninitialized_fill_n(_ForwardIterator,_Size,const _Tp&)[with _ForwardIterator = std :: unique_ptr*; _Size = unsigned int; _Tp = std :: unique_ptr]'
optional< unique_ptr<...> >即使我应用了这里提供的补丁,我也遇到了一些问题:
https://svn.boost.org/trac/boost/ticket/1841
(注意:找到通过是否可以移动boost :: optional?)
例如:
boost::optional< unique_ptr<int> > optWorks (new int);
// Fails
boost::optional< unique_ptr<int> > optFails (std::move(optWorks));
Run Code Online (Sandbox Code Playgroud)
我觉得我正在做的事情是合法的.事实上,我已经通过将unique_ptr合并到这个项目中,在所有权转移语义方面发现了一些错误.所以我不想说"哦,这太复杂了,只需使用原始指针".
这是支持推动议程吗?它有时间表吗?我可以在此期间使用任何简单的解决方法吗?
之间的关系const_multi_array_ref,multi_array_ref并且multi_array是如下:
multi_array_ref 源于 const_multi_array_refmulti_array 源于 multi_arry_ref然而,的析构函数const_multi_array_ref和multi_array_ref有非虚.实际上,它们没有明确实现的析构函数.只有multi_array一个.这是否意味着不推荐以下用法?
multi_array_ref<float, 2> * = new multi_array<float, 2>(extents[3][3]);
Run Code Online (Sandbox Code Playgroud)
如果是这样,为什么?
我惊讶地发现boost::multi_array似乎其初始元素的分配方式与std::vector. 它似乎没有用唯一的元素填充每个元素(使用其默认值或默认构造函数)。我无法找到有关此的更多信息。
有没有办法multi_array在每个元素上使用唯一的对象来填充本身?
例如,请考虑以下情况:
static int num = 0;
struct A {
int n;
A() : n((::num)++) {
std::cout << "A()" << std::endl;
}
virtual ~A() {}
void print() {
std::cout << "n=" << n << std::endl;
}
};
int main() {
std::cout << "vector:" << std::endl;
std::vector<A> v(3);
for (auto x : v) {
x.print();
}
std::cout << "multi:" << std::endl;
boost::multi_array<A, 2> m(boost::extents[2][2]);
for (auto x : m) {
for (auto y …Run Code Online (Sandbox Code Playgroud) 说我有
#include <boost/multi_array.hpp>
using intArray3D = boost::multi_array<int, 3>;
Run Code Online (Sandbox Code Playgroud)
我想创建一堆intArray3D形状相同的s:
auto my_shape = boost::extents[3][4][5];
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
Run Code Online (Sandbox Code Playgroud)
使用auto赋值boost::extents[3][4][5]给变量很容易,但我如何具体找出底层类型?
我看过这篇文章,讲述了如何使用该boost::multi_array::origin()函数循环非基于零的数组,但这只创建了一个循环.
如何遍历a的每个维度multi_array,例如:
for(index i = <origin of dim 1>; ...) {
for(index j = <origin of dim 2>; ...) {
for(index k = <origin of dim 3>; ...) {
myArray[i][j][k] = <something>;
}
}
}
Run Code Online (Sandbox Code Playgroud)
给定一个上下界都未知的数组?
我有一个 3 维的 boost::multi_array
boost::multi_array<Struct, 3>* newArr = new boost::multi_array<Struct, 3>(boost::extents[x][y][z], boost::fortran_storage_order())
Run Code Online (Sandbox Code Playgroud)
有没有方法来计算 newArr 的大小或者我应该使用
sizeof(Struct)*x*y*z ?
Run Code Online (Sandbox Code Playgroud)
它们会是一样的吗?(我希望 multi_array 有一点控制器数据)
什么是更快 - 使用元素选择运算符访问多阵列的元素,或使用迭代器遍历多阵列?
就我而言,我需要每次都对多阵列的所有元素进行完整的传递.