boost的multi_array的用法

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

//made by davidsu33

//2014-6-20



#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include


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


assert(nDimSize == 3);


for (int i=0; i {
for (int j=0; j {
for (int k=0; k {
md[i][j][k]  = 2012.1314;
}
}
}


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


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


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


void multi_array_sub_view()
{
typedef boost::fast_pool_allocator fastallocator;
boost::multi_array> 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> FastPoolMultiArray;
FastPoolMultiArray md2(boost::detail::multi_array::extent_gen<0>::extents()[5][6]);


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


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




//for (int *intPtr = md2.data(), i=0; 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::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::cout<<"view elements:"<

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


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


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

你可能感兴趣的:(c++模板泛化,boost)