提升多阵列维度
Boost Multiarray Dimensions
本文关键字:阵列 更新时间:2023-10-16
我有一个Boost多数组,它的维度是在运行时根据用户的输入设置的。
我现在想通过x,y,z
组件对该数组进行迭代。
如果这是一个std::矢量,我会使用:
for(int i=0;i<v.size();i++){
或者可能是某种迭代器。
如何获取多阵列维度的数值?
如何在多数组上迭代?
您可以将shape()
用于一种不那么复杂的方式:
#include <iostream>
#include <string>
#include <boost/multi_array.hpp>
int main() {
boost::multi_array<std::string, 2> a(boost::extents[3][5]);
for(size_t x = 0; x < a.shape()[0]; x++) {
for(size_t y = 0; y < a.shape()[1]; y++) {
std::ostringstream sstr;
sstr << "[" << x << ", " << y << "]";
a[x][y] = sstr.str();
}
}
for(size_t x = 0; x < a.shape()[0]; x++) {
for(size_t y = 0; y < a.shape()[1]; y++) {
std::cout << a[x][y] << "n";
}
}
return 0;
}
(请参阅coliru上的操作)
#include "boost/multi_array.hpp"
#include <iostream>
#include <algorithm>
#include <iterator>
int main () {
typedef boost::multi_array_types::index_range range;
typedef boost::multi_array<char, 2> Array2d;
Array2d a(boost::extents[8][24]);
//to view the two-dimensional array as a one-dimensional one can use multi_array_ref?
boost::multi_array_ref<char, 1> a_ref(a.data(), boost::extents[a.num_elements()]);
std::fill(a_ref.begin(), a_ref.end(), '-');
//to apply algorithm to one row or column, can use array_view
//especially useful for traversing it vertically?
//e.g:
Array2d::array_view<1>::type views[4] = {
a[boost::indices[range()][0]], //left column
a[boost::indices[range()][a[0].size() - 1]], //right column
a[boost::indices[0][range()]], //top row
a[boost::indices[a.size()-1][range()]] //bottom row
};
for (unsigned i = 0; i != sizeof(views)/sizeof(views[0]); ++i) {
std::fill ( views[i].begin(), views[i].end(), 'X' );
}
//output
for (unsigned i = 0; i != a.size(); ++i) {
std::copy(a[i].begin(), a[i].end(), std::ostream_iterator<char>(std::cout, ""));
std::cout << 'n';
}
}
来源:http://cboard.cprogramming.com/cplusplus-programming/112584-boost-multiarray.html
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- 二维阵列的动态分配
- 重新增长阵列时出错:写入时缓冲区溢出