Con*_*ser 5 c++ boost boost-multi-array
说我有
#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]
给变量很容易,但我如何具体找出底层类型?
最重要的是,
extents
你也不必使用只要满足记录的标准,许多事情都是可以接受的:
集合概念记录在该链接中
#include <boost/multi_array.hpp>
#include <iostream>
using intArray3D = boost::multi_array<int, 3>;
void dump_shape(intArray3D const& arr) {
for (unsigned dim = 0; dim < arr.dimensionality; ++dim)
std::cout << arr.shape()[dim] << " ";
std::cout << "\n";
}
int main() {
{
auto my_shape = boost::extents[3][4][5];
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
dump_shape(xs); dump_shape(ys); dump_shape(zs);
}
{
std::array<int, 3> my_shape { 3, 4, 5 };
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
dump_shape(xs); dump_shape(ys); dump_shape(zs);
}
{
std::vector<int> my_shape { 3, 4, 5 };
intArray3D xs(my_shape), ys(my_shape), zs(my_shape);
dump_shape(xs); dump_shape(ys); dump_shape(zs);
}
}
Run Code Online (Sandbox Code Playgroud)
印刷
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5
3 4 5
Run Code Online (Sandbox Code Playgroud)
文档提到:
template gen_type<Ranges>::type
- 该类型生成器用于指定
Ranges
对 的链式调用的结果extent_gen::operator[]
。
其中gen_type
是 的成员boost::multi_array_types::extent_gen
(也是boost::multi_array_types::extent_gen
全局辅助对象 的类型boost::extents
)。
您还可以看到接受一组范围的构造函数是以这种方式指定的(至少出于公共文档的目的)。 例如,
namespace boost {
template <typename ValueType,
std::size_t NumDims,
typename Allocator = std::allocator<ValueType> >
class multi_array {
Run Code Online (Sandbox Code Playgroud)
...
typedef multi_array_types::extent_gen extent_gen;
Run Code Online (Sandbox Code Playgroud)
...
explicit multi_array(extent_gen::gen_type<NumDims>::type ranges,
const storage_order_type& store = c_storage_order(),
const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)
因此,您可以重写该行代码,而无需使用auto
as:
boost::multi_array_types::extent_gen::gen_type<3>::type my_shape =
boost::extents[3][4][5];
Run Code Online (Sandbox Code Playgroud)
对于局部变量来说,这有点愚蠢,但也许您想在类或类似的东西中存储一组范围。如果是的话,这是根据官方文档接口进行的方法。
(如注释中所述,此 typedef 解析为的实际类型涉及boost::internal::
,但您永远不应该在代码中使用“内部”命名空间中的任何内容,因为这可能会在未来版本中发生更改。)
归档时间: |
|
查看次数: |
610 次 |
最近记录: |