首页 > 代码库 > boost.multi_array 的基本用法(2)

boost.multi_array 的基本用法(2)

//made by davidsu33
//2014-9-14 11:51

#include "stdafx.h"
#include <boost/config.hpp>
#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 <boost/lambda/lambda.hpp>
#include <boost/assign.hpp>
#include <boost/typeof/typeof.hpp>

#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>

using namespace std;

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

void mult_array_access(Multi3Array &mult3);
void show_dims(Multi3Array& mult3);
void sub_view(Multi3Array &mult3);

template<class T>
bool check_ptr(T& v1, T& v2);

void test_multi_array()
{
	//using namespace boost;

	//还可以设置维度的存储顺序
	//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

	//???
	//因为操作符顺序的关系不能写成,因为()的操作符顺序高,所以导致
	//boost::detail::multi_array::extent_gen<0>()[..],[..]没有先解析
	//Multi3Array mult3Array(boost::detail::multi_array::extent_gen<0>()[3][4][5]); //Error Usage

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

	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());
	mult_array_access(mult3Array);
	show_dims(mult3Array);
	sub_view(mult3Array);
}

void mult_array_access(Multi3Array &mult3)
{
	boost::array<long, 3> idx = {0, 0, 0};
	assert(mult3[0][0][0] == mult3(idx));

	mult3[0][0][0] = 100;
	assert(mult3(idx) == 100);

	int i=0;
	double *pData = http://www.mamicode.com/mult3.data();>

boost.multi_array 的基本用法(2)