for_each算法在boost::multi_array上循环
for_each algorithm to loop over boost::multi_array
以前在这里问过相关问题,但我仍然没有找到满意的答案,所以我会尽力解释我的问题,希望有人能启发我。
我目前正在使用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_n
或fill_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>());>
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- OpenGL VBO Indexing ( How to compute Index Array)
- 标准::unordered_map 中的 std::array 的值初始化
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 将 **float array 从 C++ Dll 传递给 python
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 我可以安全地复制矢量<array>吗?
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- 如何读/写或遍历 std::array 中的特定元素范围?
- 通过 host() 从 af::array 检索数据会导致错误的数据
- "dynamically allocated memory emulating multi-dimensional array"的正确术语?