- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 unordered_map
的 vectors
并且我正在尝试使用 std::reduce
来获取所有值的总和 map 中的 vector 。我当前的功能代码(我想替换)如下所示:
// input is std::unordered_map<std::vector<uint64_t>>
template<typename T>
uint64_t get_map_sum(T& my_map)
{
uint64_t totalcount = 0;
for (auto& p : my_map)
{
for (const auto& q : p.second)
totalcount += q;
}
return total_count;
}
我想将其替换为 std::reduce
以利用并行执行;我认为这会很简单,因为我只需要用对 std::reduce
的调用替换每个循环,但这似乎不起作用。我的尝试是这样的:
#include <numeric>
#include <execution>
#include <vector>
#include <unordered_map>
#include <cstdint>
// reduces the vectors
template <typename Iter, typename T>
T get_vector_sum(Iter begin, Iter end, T initial = 0)
{
return std::reduce(std::execution::par_unseq, begin, end, initial,
[&](auto cur, auto prev) { return cur + prev; });
}
// calls get_vector_sum for all vectors and then reduces vector sums
template<typename Iter>
uint64_t get_map_sum(Iter begin, Iter end)
{
return std::reduce(std::execution::par_unseq, begin, end, 0ULL,
[&](auto prev, auto cur)
{
return get_vector_sum<std::vector<uint64_t>::iterator,
uint64_t>(cur.begin(), cur.end(), prev);
//return get_vector_sum<std::vector<uint64_t>::iterator,
// uint64_t>(cur.second.begin(), cur.second.end(), prev);
});
}
使用上面的代码,我收到一条错误消息,指出 error C2039: 'begin': is not member of 'std::pair'
referenced to the auto cur
在 get_map_sum
内的 lambda 中。我最初使用 cur
作为 std::pair
,但是当我这样做时,我得到了一个不同的错误,说 error C2228: left of '.second' must have类/结构/union
。
int main()
{
std::unordered_map<uint64_t, std::vector<uint64_t>> in({
{1, std::vector<uint64_t>{1,2,3,4,5} },
{2, std::vector<uint64_t>{1,2,3,4,5}},
{3, std::vector<uint64_t>{1,2,3,4,5}}});
auto x = get_map_sum(in); // output 45
auto y = get_map_sum(in.begin(), in.end()); // error
return 0;
}
是否可以将 std::reduce
与这样的映射一起使用,如果可以,我需要进行哪些更改才能使其正常工作?
最佳答案
请注意 std::reduce
的 binary_op 的要求:
binary FunctionObject that will be applied in unspecified order to the result of dereferencing the input iterators, the results of other binary_op and init.
这意味着您的 lambda result 和 init 的结果需要与 map 的值类型具有相同的类型,即 std::pair<const uint64_t, std::vector<uint64_t>>
.
因此,您需要对该类型的值执行外部约简,这将涉及构建新 vector 。
我还尝试创建一个示例代码如下:
using M = std::unordered_map<uint64_t, std::vector<uint64_t>>;
using V = M::value_type;
M in({ {1, std::vector<uint64_t>{1,2,3,4,5}},
{2, std::vector<uint64_t>{1,2,3,4,5}},
{3, std::vector<uint64_t>{1,2,3,4,5}} });
auto p = std::reduce(in.begin(), in.end(), V{},
[](const V& a, const V& b) {
auto ra = std::reduce(a.second.begin(), a.second.end(), 0UL,
[](uint64_t i1, uint64_t i2){ return i1 + i2; });
auto rb = std::reduce(b.second.begin(), b.second.end(), 0UL,
[](uint64_t i1, uint64_t i2){ return i1 + i2; });
return V{0, { ra + rb }};
});
但由于似乎缺少 std::reduce
,它无法使用 GCC 进行编译实现和 Clang 提示缺少值类型的复制赋值运算符,由于 const key:https://wandbox.org/permlink/FBYAhCArtOHvwu8C,它不可复制赋值。 .
但是在cppreference中,对值类型的要求只是MoveConstructible,而不是Copy/MoveAssignable。所以,似乎在 libc++ 中有一个不正确的实现。
在这个示例代码中,我能够通过定义 V
使其工作没有 const 如下:
using V = std::pair<uint64_t, std::vector<uint64_t>>;
关于c++ - std::reduce 与 std::unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55096214/
我不知道如何从 reducerRegister.js 中的 reducerForm.js reducer 访问 bool 值 isLoading 标志。我使用了 combineReducers() 并
我正在尝试找到一种理想的方法来更新我的状态树上的几个顶级字段,同时仍然维护拆分 reducer 。 这是我想出的一个简单的解决方案。 var state = { fileOrder: [0],
如果我们想按相同的键(第一个 reducer 的输出)分组,是否可以将 reducer 的输出直接发送到另一个 reducer 有时在链接时我发现我正在使用一个映射器来读取输入并将其复制到输出。因此想
我有一个如下所示的 reducer : const chart = combineReducers({ data, fetchProgress, fetchError,
当Map Reduce代码中有多个reduce时,它们之间没有任何形式的通信。但是,当执行诸如聚合之类的操作时,所有化简器共同产生单个最终输出。当它们之间没有通信时,聚合如何发生?是通过写入上下文吗?
我在 hive 中有一个表,我想从中获取所有数据。问题是: select * from tbl; 给我的结果与以下情况截然不同: select count(*) from tbl; 这是为什么?第二个
假设我有一个带有两个 reducer 的应用程序 - 使用 combineReducers() 组合的 tables 和 footer。 当我点击某个按钮时,将分派(dispatch)两个操作 - 一
我正在学习更深入的 redux,并且在处理高阶 reducer 时遇到一些麻烦。 我试图使用一个简单的分页示例来了解它是如何工作的。 NB:下面的代码只是 Nodejs 上下文中 redux 的一个快
我调用 RSS 提要并使用解析器对其进行解析。我收到一个数组。我现在想在最后创建一个对象,看起来像这样: { "2019-06-13": { "rates": { "usd":
我有一份学生列表,我的应用程序始终显示当时的一个学生,即 activePupil。到目前为止我有两个 reducer 。其中一个包含并默认返回所有子项的列表(以数组的形式): [ { id:
我有一个叫做 animals 的特征缩减器(切片缩减器)。我想将这些 reducer 拆分为哺乳动物、鸟类、鱼类等。这部分很简单,因为我可以简单地使用 ActionReducerMap。 现在假设哺乳
空数组上的简单reduce会抛出: 线程“main”java.lang.UnsupportedOperationException 中的异常:无法减少空的可迭代对象。 链接时同样的异常: val a
我有一些 25k 文档(原始 json 中为 4 GB)的数据,我想对其执行一些 javascript 操作,以使我的最终数据使用者 (R) 更容易访问这些数据,并且我想通过为每个更改添加一个新集合来
我只是想验证我对这些参数及其关系的理解,如果我错了请通知我。 mapreduce.reduce.shuffle.input.buffer.percent 告诉分配给 reducer 的整个洗牌阶段的内
我想将 redux 状态的值从 reducer 传递到另一个 reducer。在我的例子中,我想将 groups 的值从 groupReducer.js 中的状态传递到 scheduleReducer
所以,我有一个应用程序,它有多个 reducer ,因此有多个关联的 Action 创建者。 有一段时间,我的一个 reducer 更新了状态(由于编辑),因此,我必须确保其他 reducer 看到此
我有一个 reducer ,可以在调度操作时重新调整适当的状态。现在我定期调用 API,因此结果会一次又一次地触发操作。所以我想要的是,如果 reducer 状态已经有数据,那么另一个 reducer
当我尝试执行来自 here 的 DISTINCT reduce 时,出现错误。我已经在啤酒 sample 桶上重现了这个错误,所以这应该很容易重现。我没有在 mapreduce_errors.txt
在以下语法的简单优先级解析(分解)中,我们存在 shift-reduce 和 reduce-reduce 冲突。 X 是开始符号,X'-->$X$ 是添加规则。另外+和下符号是终结符。 X'-->$X
我需要编写一个连续调用两个reducer的Mapreduce程序。即,第一个 reducer 的输出将是第二个 reducer 的输入。我如何实现这一目标? 到目前为止我发现的内容表明我需要在我的驱动
我是一名优秀的程序员,十分优秀!