【发布时间】:2014-03-14 09:10:22
【问题描述】:
我正在编写处理 3d 网格数据的代码(boost::multi_array 和 vtkImageData)。我发现我有嵌套循环
for(int i=0; i<shape[0]; i++){
for(int j=0; j<shape[1]; j++){
for(int k=0; k<shape[2]; k++){
/* ... */
}
}
}
在我的代码中出现的次数比我喜欢的要多;使用在固定大小数组中保存三个索引的迭代器(如boost::array)编写此代码的正确方法是什么?
我们的目标是写一些类似于:
for(boost::array<int,3> ijk: GridIndicesIterator(shape)){ ... }
boost::multi_array 允许遍历元素,但 3d 索引必须显式地反向计算。我还查看了 boost::iterator ,看来正确编写迭代器代码是一项艰巨的任务。
【问题讨论】: