boost的multi_array的用法

// multiArray.cpp : 定义控制台应用程序的入口点。

//made by davidsu33

//2014-6-20



#include "stdafx.h"
#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <boost/pool/pool.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/pool/detail/mutex.hpp>
#include <boost/multi_array/extent_gen.hpp>
#include <boost/multi_array/view.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/lambda/lambda.hpp>


void use_multiArray()
{
const int nDim = 3;
int xDim = 2,  yDim = 3,  zDim = 4;
boost::multi_array<double, nDim> md(boost::extents[xDim][yDim][zDim]);
unsigned nDimSize = md.num_dimensions();


assert(nDimSize == 3);


for (int i=0; i<xDim; ++i)
{
for (int j=0; j<yDim; ++j)
{
for (int k=0; k<zDim; ++k)
{
md[i][j][k]  = 2012.1314;
}
}
}


boost::array<std::size_t, 3> idx = {1, 2, 3};
md(idx) = 1314.520;


double * ptr = md.data();
int elementsCnt = md.num_elements();


//boost::ref_array<double> refArray(ptr, md.num_elements());
//std::cout<<md<<std::endl;
}


void multi_array_sub_view()
{
typedef boost::fast_pool_allocator<double, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> fastallocator;
boost::multi_array<double, 2, boost::fast_pool_allocator<double>> md(boost::extents[2][3]);


//boost::multi_array::extent_gen<2> extentgen;
//extentgen = extentgen[3][5];


boost::detail::multi_array::extent_gen<2> exgen = boost::detail::multi_array::extent_gen<0>()[5][6];
typedef boost::multi_array<int,2, boost::fast_pool_allocator<int>> FastPoolMultiArray;
FastPoolMultiArray md2(boost::detail::multi_array::extent_gen<0>::extents()[5][6]);


double *elements =  md.data();
for (int i=0; i<md.num_elements(); ++i)
{
elements[i] = 20+i;
}


boost::array<unsigned, 2> index = {0, 2};
md(index) = 300;




//for (int *intPtr = md2.data(), i=0; i<md2.num_elements(); ++i )
//{
// intPtr[i] = 1+i;
//}
int seftAdd = 0;
//注意boost::lambda::constant和
//boost::lambda::var
//boost::lambda::ref_constant的区别
//如果是var可以作为变量在lambda表达式中运算
//constant的不行。变量不会做自加操作。
std::for_each(md2.data(), md2.data()+md2.num_elements(), 
boost::lambda::_1 = boost::lambda::var(seftAdd)++);
std::for_each(md2.data(), md2.data()+md2.num_elements(),
(std::cout<<boost::lambda::_1<<","));


//boost::multi_array_types的类型定义在multi_arary/base.hpp中定义
//索引在multi_array/types.hpp中定义


//索引的四种写法
boost::detail::multi_array::index_gen<2, 2> r;
r = boost::multi_array_types::index_gen()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,3)];
boost::detail::multi_array::index_gen<0,0>::indices()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::detail::multi_array::index_gen<0,0>()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::indices[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];


BOOST_AUTO(view, md2[r]);
BOOST_AUTO(it, view.begin());
BOOST_AUTO(iend, view.end());


std::cout<<*view.shape()<<std::endl;
std::cout<<"view elements:"<<view.num_elements()<<std::endl;


for (int i=0; i<2; ++i)
{
for (int j=0; j<3; ++j)
{
std::cout<<"view["<<i<<"]["<<j<<"]="<<view[i][j]<<std::endl;
}
}


//for (; it != iend; ++it)
//{
// std::cout<<*it<<std::endl;
// *it = 1000;
//}
}


int _tmain(int argc, _TCHAR* argv[])
{
use_multiArray();
multi_array_sub_view();
getchar();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值