boost中的multi_array--多维数组
简单使用
#include <iostream>
#include <boost/multi_array.hpp>
using namespace boost;
int main(void)
{
const int row = 3;
const int col = 3;
//extents指定行列大小,可以是三维及以上
multi_array<int, 2> ma(extents[row][col]);
//像普通数组一样赋值和使用
for (int i = 0; i < row; ++i) {
for(int j = 0; j < col; j++) {
ma[i][j] = i * j + 1;
}
}
for (int i = 0; i < row; ++i) {
for(int j = 0; j < col; j++) {
std::cout << ma[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
常用方法
#include <iostream>
#include <boost/multi_array.hpp>
using namespace boost;
int main(void)
{
const int row = 3;
const int col = 3;
multi_array<int, 2> ma(extents[row][col]);
//获取元素总数
std::cout << ma.num_elements() << std::endl;
//指定访问位置
std::array<int, 2> idx = {1, 2};
std::cout << ma(idx) << std::endl;
//改变形状
//std::array<int,2> arr = {1,9};
//ma.reshape(arr);
//std::cout << ma.shape()[0] << std::endl;
//std::cout << ma.shape()[1] << std::endl;
//创建子视图
using range = multi_array<int, 2>::index_range;
auto view = ma[indices[range(1,3)][range(1,3)]];
std::cout << view[0][1] << std::endl;
}
boost官网
更多信息请参照boost官网