gpt4 book ai didi

c++ - C++中unordered_map的有效笛卡尔积

转载 作者:行者123 更新时间:2023-12-02 10:31:07 26 4
gpt4 key购买 nike

我使用itertools.product()在python中生成了多个字典列表的乘积。

现在,我正在尝试在c++中实现基本的笛卡尔积,但是生成这些积需要大量时间。您能否给我一些建议以使其更高效?谢谢。

vector<vector<unordered_map<string, string>>> iter_product(\
vector<vector<unordered_map<string, string>>> &maps_list){

vector<vector<unordered_map<string, string>>> out;
for (auto map = maps_list[0].begin(); map != maps_list[0].end(); map++){
out.push_back(vector<unordered_map<string, string>>({*map}));
}
if (maps_list.size() > 1){
for (int i = 1; i < maps_list.size(); i++){
vector<vector<unordered_map<string, string>>> new_out;
for (int j = 0; j < out.size(); j++){
for (int k = 0; k < maps_list[i].size(); k++){
out[j].push_back(maps_list[i][k]);
new_out.push_back(out[j]);
}
}
out = new_out;
}
}
return out;
}

最佳答案

如上所述,我也认为您应该使用引用作为参数,而不是实际的 vector 。另外,如果知道的话,您可以预定义 vector 大小。

关于c++ - C++中unordered_map的有效笛卡尔积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62241874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com