multi_array的正确(错误)用法说明

本文详细介绍了如何使用C++和Boost库中的multi_array和pool等模块来创建和操作多维数组,包括创建数组、设置维度、获取数组属性等关键步骤,并着重探讨了在使用multi_array::extent_gen进行维度定义时的常见错误及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include <boost/pool/pool.hpp>
#include <boost/pool/pool_alloc.hpp>

#include <boost/array.hpp>
#include <iostream>
#include <cassert>

using namespace std;

void test_multiArray()
{
	//using namespace boost;

	typedef boost::fast_pool_allocator<double> FastPoolAllocator;
	typedef boost::multi_array<double, 3, FastPoolAllocator> Multi3Array;

	//还可以设置维度的存储顺序
	//Multi3Array mult3Array(boost::multi_array_types::extent_gen::extents()[3][4][5]);
	//Multi3Array mult3Array(boost::extents[3][4][5]); //Right Usage

	//为什么不能直接写
	boost::detail::multi_array::extent_gen<3> extentgen3 = boost::detail::multi_array::extent_gen<0>()[3][4][5];
	//Multi3Array mult3Array(extentgen3); //Right Usage

<span style="font-size:18px;color:#ff0000;"><strong>	//???
	//因为操作符顺序的关系不能写成,因为()的操作符顺序高,所以导致
	//boost::detail::multi_array::extent_gen<0>()[..],[..]没有先解析
	//<em><u>Multi3Array mult3Array(boost::detail::multi_array::extent_gen<0>()[3][4][5]); //Error Usage</u></em>

	Multi3Array mult3Array((boost::detail::multi_array::extent_gen<0>()[3][4][5])); //Right Usage</strong></span>

	int nDims = mult3Array.num_dimensions();

	assert((mult3Array.num_dimensions() == 3));
	assert((mult3Array.num_elements() == 3*4*5));

	int sz = mult3Array.size();

	//可以通过ListExtend来扩展
	boost::array<unsigned,3> dimArray = {3, 4, 5};
	Multi3Array mutl3Array2(dimArray);

	assert(mutl3Array2.num_elements() == mult3Array.num_elements());
}


int _tmain(int argc, _TCHAR* argv[])
{
	test_multiArray();

	getchar();
	return 0;
}


/work/pcl-cross-compile/src/boost$ ls /work/pcl-cross-compile/install/boost/include/ algorithm dynamic_bitset.hpp make_unique.hpp qvm throw_exception.hpp align enable_shared_from_this.hpp math qvm.hpp timer aligned_storage.hpp exception math_fwd.hpp qvm_lite.hpp timer.hpp align.hpp exception_ptr.hpp move random token_functions.hpp archive foreach_fwd.hpp mpi random.hpp token_iterator.hpp asio foreach.hpp mpi.hpp range tokenizer.hpp asio.hpp format mpl range.hpp tti assign format.hpp multi_array rational.hpp tuple assign.hpp functional multi_array.hpp redis type_erasure call_traits.hpp functional.hpp multi_index redis.hpp type_index cerrno.hpp fusion multi_index_container_fwd.hpp regex type_index.hpp charconv geometry multi_index_container.hpp regex_fwd.hpp typeof charconv.hpp geometry.hpp multiprecision regex.h type_traits circular_buffer gil nondet_random.hpp regex.hpp type_traits.hpp circular_buffer_fwd.hpp gil.hpp none.hpp safe_numerics units circular_buffer.hpp graph none_t.hpp scoped_array.hpp unordered cobalt heap nowide scoped_ptr.hpp unordered_map.hpp cobalt.hpp histogram numeric scope_exit.hpp unordered_set.hpp compressed_pair.hpp histogram.hpp operators.hpp serialization url concept hof operators_v1.hpp shared_array.hpp url.hpp concept_archetype.hpp hof.hpp optional shared_ptr.hpp utility concept_check icl optional.hpp signals2 utility.hpp concept_check.hpp interprocess parameter signals2.hpp uuid config intrusive pending smart_ptr uuid.hpp config.hpp intrusive_ptr.hpp phoenix smart_ptr.hpp variant container io phoenix.hpp sort variant2 context io_fwd.hpp pointer_cast.hpp spirit variant2.hpp convert json pointer_to_other.hpp spirit.hpp variant.hpp convert.hpp json.hpp polygon stacktrace version.hpp cregex.hpp leaf pool stacktrace.hpp vmd cstdfloat.hpp leaf.hpp predef statechart wave cstdint.hpp limits.hpp predef.h static_assert.hpp wave.hpp cxx11_char_types.hpp locale program_options static_string weak_ptr.hpp date_time locale.hpp program_options.hpp static_string.hpp winapi date_time.hpp local_function progress.hpp stl_interfaces xpressive describe local_function.hpp property_map system yap describe.hpp lockfree proto system.hpp detail logic ptr_container test dynamic_bitset make_default.hpp python thread dynamic_bitset_fwd.hpp make_shared.hpp python.hpp thread.hpp uidq8326@hzh27145u:/work/pcl-cross-compile/src/boost$ ./b2 install toolset=gcc architecture=arm address-model=64 \--prefix=/work/pcl-cross-compile/install/boost -j$(nproc) Performing configuration checks - default address-model : 64-bit [1] - default architecture : arm [1] - symlinks supported : yes error: Unable to find file or target named error: '/boost/headers' error: referred to from project at error: 'libs/date_time/build'
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值