【问题标题】:Can I use reduce instead of a for loop?我可以使用 reduce 代替 for 循环吗?
【发布时间】:2019-05-14 08:03:25
【问题描述】:

我有一个包含 0 到 X 个数字和高整数值的 hashSet。
0:1000000001
1:1000000002
...

和位置向量 (22,14,29,59,10)。 我需要生成这个向量的所有组合。 为此,我使用来自https://github.com/mraggi/discreture 的库进行生成。我得到了矢量大小和 need_comb_size 的所有组合
如 (10,3)
0,1,2
0,1,3
...

现在我将生成的组合与我的向量位置和 hashSet 链接起来 hashSet[vect[comb[0]]+...

我可以在一行中使用这个reduce吗?

目标是生成一个高整数(散列)并使用此散列作为键,并将我在梳状示例中的位置:(12,59,11) 作为值。
3423422821 : 向量 ((12,59,11)

void combination(int size, vector<unsigned short> chunk, vector<unsigned long long> hashSet, unordered_map<unsigned long long, pair<vector<unsigned short>, vector<unsigned short>>> collision_map, unsigned long long low, unsigned long long high, int dimension) {
    for (auto&& comb : discreture::combinations_stack(size,KCOMB))
    {
        unsigned long long signature = 0;
        vector<unsigned short> newChunk;
        //signature = reduce(std::execution::par, comb.begin(), comb.end())
        for (auto v : comb) {
            signature += hashSet.at(chunk.at(v));
            newChunk.push_back(chunk.at(v));
        }
        checkSignature(low, high, signature, collision_map, newChunk, dimension);
    }
}

【问题讨论】:

    标签: c++ c++17


    【解决方案1】:

    请注意,std::reduce 有警告

    如果binary_op 不具有关联性或不可交换性,则行为是不确定的。

    如果您关心newChunk 中元素的顺序,那么您不能在内循环中使用reduce

    如果你关心对checkSignature的调用顺序,那么你不能在外循环中使用reduce,否则你仍然需要在最后合成一个值扔掉,你可以' t 传递 void 作为累加器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多