审查与lambda到Java的C for_each的移植
Review of porting a C++ for_each with Lambda to Java
我正在尝试将C 代码移到Java。
摘要如下:
uint32_t maxNumSource = newLayer.size.x * newLayer.size.y;
for (auto &plane : newLayer.flatConvolveMatrix) {
std::for_each(std::begin(plane), std::end(plane), [maxNumSource](float &weight) {
weight = (float)(((randomFloat() * 2) - 1.0f) / sqrt(maxNumSource));
});
}
flatConvolvolvematrix是Newlayer的成员,如下所示:
vector<vector<float>> flatConvolveMatrix;
我不确定在C 中如何处理"重量"变量。在for_each循环中以作为参数而进入,对吗?'[maxnumsource]''是什么意思?
到目前为止,在Java中,我想出了:
Integer maxNumSource = newLayer.size.x * newLayer.size.y;
for ( Vector<Double> plane : newLayer.flatConvolveMatrix ) {
double weight = 0;
for ( Double value : plane ) {
weight += randomFloat() * 2 - 1.0 / Math.sqrt(maxNumSource);
}
}
我的解释正确吗?
[maxNumSource]
零件是lambda的捕获列表,它允许lambda在其体内使用变量maxNumSource
的副本。
不,有一个错误:
C 版本修改了plane
中的元素,而Java版本则没有。应该是:
for (Double value : plane)
value = (randomFloat() * 2 - 1.0) / Math.sqrt(maxNumSource);
^^^^^^^^^^^^^^^^^^^^^^^^^^^
operator precendence :)
^^^^
modifies the current 'value' in 'plane'
相关文章:
- C++ 中用于二维数组的 for-each 循环
- 计算 fib(n) 的次数称为 FOR EACH n
- 支持自定义 const 本机C++容器类的"for each"
- C++ for-each 语句触发"vector iterators incompatible"断言失败:this->_Getcont() == 0
- 为什么我的矢量中的项目在 C++ 中的 for-each 循环期间不会改变?
- 当循环返回"for each"项的 nullptr 时?
- 正确使用C++ 'for each'选项
- "for each"能保证使用更改数组吗?
- 基于范围的循环与 for-each 循环有何不同
- C++:从"for each"循环中的映射中删除值
- 将函数应用于std::map值,其中包含for each和lambda函数
- 正在擦除for(-each)自动循环中的项目
- g++ 4.4.7 -std=gnu++0x 应该编译"for each"结构吗?
- 将循环转移到C++03 for each
- 在 for-each 循环中使用 'auto' 时是否需要添加'const'?
- C++中的"for each"循环如何知道数组的长度
- 基本c++: for-each循环
- 如何使c++中的for each循环函数与自定义类一起工作
- 如何使用for-each循环来美化析构函数
- for-each 循环生成错误,但 for(it=begin()...在遍历 std::map 包含 unique_ptr 时不会