如何遍历向量映射映射映射映射映射映射映射映射 我试图从地图矢量地图的地图地图中收集所有数据,而不需要在C++中有7个循环。
以下是数据的外观:如何遍历向量映射映射映射映射映射映射映射映射 我试图从地图矢量地图的地图地图中收集所有数据,而不需要在C++中有7个循环。,c++,map,iteration,C++,Map,Iteration,以下是数据的外观: Map 1=>Map 1.1=>Map 1.1.1=>Map 1.1.2=>Map 1.1.3=>Map 1.1.4=>Vector 1.1.5=>Elem 1 =>Elem 2 Map 1.2=>Map 1.2.1=>Map 1.2.2=>Map 1.2
Map 1=>Map 1.1=>Map 1.1.1=>Map 1.1.2=>Map 1.1.3=>Map 1.1.4=>Vector 1.1.5=>Elem 1
=>Elem 2
Map 1.2=>Map 1.2.1=>Map 1.2.2=>Map 1.2.3=>Map 1.2.4=>Vector 1.2.5=> Elem 1
=>Elem 2
Map 2 =>Map 1.1=>Map 1.1.1=>Map 1.1.2=>Map 1.1.3=>Map 1.1.4=>Vector 1.1.5=>Elem 1
=>Elem 2
Map 1.2=>Map 1.2.1=>Map 1.2.2=>Map 1.2.3=>Map 1.2.4=>Vector 1.2.5=>Elem 1
=>Elem 2
所以我试图把所有地图上的元素1,元素2收集到一张地图上
有人能帮助我做这一点而不是通过每个映射的明显循环,并导致C++中的7个循环?
谢谢你的帮助。如果你真的需要,你可以做一些模板元编程,比如使用boostmpl,来抽象掉循环。然而,正如许多人所建议的,对于原始问题,很可能有一个更好的解决方案,而不是需要7个嵌套映射和一个向量的解决方案。除非更改数据类型,否则您可能会陷入循环中的循环 然而,我将从重新编写地图的地图开始。。。。矢量化为1个矢量图。
我建议使用boost::tuple或std::tuple C++0x创建该类,但您也可以定义自己的重载运算符,您可以将其重新编写为map
#include <algorithm>
#include "prettyprint.hpp"
using namespace pretty_print; // for "is_container" trait
template <typename T> struct is_pair : public std::false_type { };
template <typename S, typename T> struct is_pair<std::pair<S,T>> : public std::true_type { };
template <typename T> struct final_value { typedef T type; };
template <typename S, typename T> struct final_value<std::pair<S,T>> { typedef T type; };
template <typename Iter, typename F> void for_each_recursive(Iter begin, Iter end, F f);
template <typename F, bool Recurse> struct for_each_rec_impl;
template <typename F>
struct for_each_rec_impl<F, false>
{
template <typename Iter>
static typename std::enable_if<is_pair<typename std::iterator_traits<Iter>::value_type>::value, void>::type
go(Iter begin, Iter end, F f)
{
for (Iter it = begin; it != end; ++it) f(it->second);
}
template <typename Iter>
static typename std::enable_if<!is_pair<typename std::iterator_traits<Iter>::value_type>::value, void>::type
go(Iter begin, Iter end, F f)
{
for (Iter it = begin; it != end; ++it) f(*it);
}
};
template <typename F>
struct for_each_rec_impl<F, true>
{
template <typename Iter>
static typename std::enable_if<is_pair<typename std::iterator_traits<Iter>::value_type>::value, void>::type
go(Iter begin, Iter end, F f)
{
for (Iter it = begin; it != end; ++it)
{
for_each_recursive(it->second.begin(), it->second.end(), f);
}
}
template <typename Iter>
static typename std::enable_if<!is_pair<typename std::iterator_traits<Iter>::value_type>::value, void>::type
go(Iter begin, Iter end, F f)
{
for (Iter it = begin; it != end; ++it)
{
for_each_recursive(it->begin(), it->end(), f);
}
}
};
template <typename Iter, typename F>
void for_each_recursive(Iter begin, Iter end, F f)
{
typedef typename std::iterator_traits<Iter>::value_type value_type;
typedef typename final_value<value_type>::type type;
for_each_rec_impl<F, is_container<type>::value>::go(begin, end, f);
}
用法:for_each_recursivev.begin、v.end、my_谓词 为什么会有如此深的嵌套贴图和向量?也许有更好的方法来构造代码以使其不必要,而不是尝试迭代映射向量映射。您显然感觉到这有点过分,因为您不愿意编写七个嵌套for循环,但是如果您有七个嵌套映射,您还能做什么?这有点可笑,一定有别的选择。如果你解释了你想要达到的目标,有人会建议一个更好的方法。一个程序员如何计算?1,2,很多。神圣的嵌套容器,蝙蝠侠!蝙蝠侠骑着七个嵌套的地图,这使得你的论点无效。@Xirdus:一旦标准发布,它可能会失效。我还是没有屏住呼吸。