我有这样的数据结构:map<string, map<string, map<string, MyObj>>>
现在,我有多个函数,所有函数都使用相同的 for 循环方法:
for (auto p1 : myMap)
for (auto p2 : p1.second)
for (auto p3 : p2.second)
doThingsWith(p1, p2, 3);
哪里doThingsWith(p1, p2, p3)
函数之间以及 for 循环之前和之后的代码各不相同。另外,有些功能只需要访问MyObj
即可。例如对象,而其他人需要访问所有字符串键以及 MyObj
对象。
那么,问题是,有没有什么方法可以在不损失性能的情况下对此进行泛化?我想出了一个返回元组 vector 的函数:
vector<tuple<string, string, string, MyObj>> getData(... &myMap)
{
vector<tuple<string, string, string, MyObj>> data;
for (auto p1 : myMap)
for (auto p2 : p1.second)
for (auto p3 : p2.second)
data.push_back(tuple<string, string, string, MyObj>(
p1.first, p2.first, p3.first, p3.second
));
return data;
}
现在我的函数可以使用这个:
for (auto t : getData(myMap))
doThingsWith(get<0>(t), get<1>(t), get<2>(t), get<3>(t));
但这不必要地构造了很多元组和 vector ,因为myMap
很大。
有没有更好的办法?在 Python 中我可以使用生成器,但我不知道 C++ 中的等价物:
def iterTuples(myMap):
for k1, v1 in myMap.items():
for k2, v2 in v1.items():
for k3, v3 in v2.items():
yield k1, k2, k3, v3
for k1, k2, k3, val in iterTuples(myMap):
doThingsWith(k1, k2, k3, val)
最佳答案
您的 map 中的 map 从一开始就效率不高。更好的做法是合并关键部分:
typedef tuple<string, string, string> Key;
std::map<Key, MyObj> myMap;
现在您有了一个直接有效的解决方案:
for (const auto& pr : myMap)
doThingsWith(get<0>(pr.first), get<1>(pr.first), get<2>(pr.first), pr.second);
而且你避免了很多间接的。如果您可以使用固定长度的字符串和/或将字符串组合成一个分配,则更多。或者,如果共享第一个和第二个键字符串很重要,您可以使用引用计数字符串类型。
关于c++ - 泛化多个嵌套的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905578/