Код: Выделить всё
// input is std::unordered_map
template
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;
}
< /code>
Я бы хотел заменить это на std :: code>, чтобы использовать параллельное выполнение; Я думал, что это будет просто, так как мне нужно было заменить каждый цикл на вызов на STD :: CREAND Код: Выделить всё
#include
#include
#include
#include
#include
// reduces the vectors
template
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
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(cur.begin(), cur.end(), prev);
//return get_vector_sum(cur.second.begin(), cur.second.end(), prev);
});
}
Код: Выделить всё
int main()
{
std::unordered_map in({
{1, std::vector{1,2,3,4,5} },
{2, std::vector{1,2,3,4,5}},
{3, std::vector{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;
}
Подробнее здесь: https://stackoverflow.com/questions/550 ... rdered-map