for_each算法在boost::multi_array上循环

for_each algorithm to loop over boost::multi_array

本文关键字:multi array 循环 boost each 算法 for      更新时间:2023-10-16

以前在这里问过相关问题,但我仍然没有找到满意的答案,所以我会尽力解释我的问题,希望有人能启发我。

我目前正在使用boost::multi_array编写一些代码,代码本身也是维度无关的。我需要循环遍历存储在multi_array中的所有元素,并对它们执行一些操作。我希望以类似STL的方式做到这一点:

for_each(begin(array), end(array), function);

或者类似的东西。其他问题让我想到了提升页面本身的一个例子:

对于每个示例
对于每个实施(_E)

这或多或少正是我想要的。当一个人试图简单地将这些代码导入到一个更大的程序中时,问题就来了。人们自然希望将函数本身封装在某个命名空间中,并使用例如C++函数作为函数对象。执行这两项操作中的任何一项都会给编译器带来模板查找问题。

有人知道我如何解决模板查找问题,或者知道另一种方法(希望也一样漂亮)吗?

附加信息:

在命名空间中包装for_each定义时发生编译错误

./for_each.hpp:28:3: error: call to function 'for_each' that is neither visible in the template definition nor found by
argument-dependent lookup
  for_each(type_dispatch,A.begin(),A.end(),xform);
  ^
./for_each.hpp:41:5: note: in instantiation of function template specialization
'boost_utilites::for_each<boost::detail::multi_array::sub_array<double, 1>,
      double, times_five>' requested here
    for_each(type_dispatch,*begin,xform);
    ^
./for_each.hpp:50:3: note: in instantiation of function template specialization 'boost_utilites::for_each<double,
      boost::detail::multi_array::array_iterator<double, double *, mpl_::size_t<2>, boost::detail::multi_array::sub_array<double,
1>,
      boost::random_access_traversal_tag>, times_five>' requested here
  for_each(boost::type<typename Array::element>(),A.begin(),A.end(),xform);
  ^
foreach_test.cpp:46:19: note: in instantiation of function template specialization
'boost_utilites::for_each<boost::multi_array<double, 2,
      std::allocator<double> >, times_five>' requested here
  boost_utilites::for_each(A,times_five());
                  ^
./for_each.hpp:37:6: note: 'for_each' should be declared prior to the call site or in an associated namespace of one of its
arguments
void for_each (const boost::type<Element>& type_dispatch,
     ^
1 error generated.

当使用std::function对象而不是示例中的times_live对象时,会得到基本相同的编译错误。

使用clang 3.4-1ubuntu3版本编译。

std::for_each(array.data(), array.data() + array.num_elements(), function);

要使其与期望随机访问范围的函数(使用.begin().end().size())一起工作,请使用

auto elements = boost::make_iterator_range(array.data(), array.data() + array.num_elements();
// e.g.
for (auto& element : elements) {
    ...
}

我个人喜欢将其与generate_nfill_n等一起使用:

std::for_each(array.data(), array.num_elements(), 0);

参考文件

  • element* data();
  • const element* data() const;

    这将返回一个指向连续块开头的指针包含数组的数据。如果数组的所有维度0-索引并按升序存储,这相当于origin()。请注意,const_multi_array_ref只提供常量此函数的版本。

  • size_type a.num_elements()

    这将返回数组中包含的元素数。它相当于以下代码:

    std::accumulate(a.shape(),a.shape+a.num_dimensions(), 
               size_type(1),std::multiplies<size_type>());>