STL 映射到自身?

  
本文介绍了STL 映射到自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 std::map,其中包含一个 std::vector 迭代器,以实现一个简单的基于邻接列表的图形结构.

但是,类型声明让我很困惑:您似乎需要整个映射类型定义来获取所述映射的迭代器类型,如下所示:

地图int, 东西 >::iterator MyMap_it;//Something 应该是什么?地图是否有某种部分映射迭代器类型我可以只使用键类型来获得,所以我可以声明完整映射? 解决方案 你可以使用新类型的前向声明.类 MapItContainers;typedef map::iterator MyMap_it;类 MapItContainers{上市:矢量<MyMap_it>向量;};使用这种间接方式,编译器应该可以让你摆脱它.它不是很漂亮,但老实说,我认为你不能轻易打破自我引用.I'd like to create a std::map that contains a std::vector of iterators into itself, to implement a simple adjacency list-based graph structure.  

However, the type declaration has me stumped: it would seem you need the entire map type definition to get the iterator type of said map, like so:map< int, Something >::iterator MyMap_it;  // what should Something be?
map< int, vector<MyMap_it> > MyMap_t;
Is there some sort of partial map iterator type I can get with just the key type, so I can declare the full map? 解决方案 You could use forward declaration of a new type.class MapItContainers;
typedef map<int, MapItContainers>::iterator MyMap_it;

class MapItContainers
{
public:
 vector<MyMap_it> vec;
};
With this indirection the compiler should let you get away with it.
It is not so very pretty but honestly I don't think you can break the self referencing easily.

                        这篇关于STL 映射到自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
相关文章
C/C++实例代码最新文章 SFINAE 编译器问题 如何从 C++ 多图中删除特定对? 缓存结束迭代器——好主意还是坏主意? 如何在 C++ 中反向迭代地图? STL Container:构造函数的分配器参数和作用域分配器 何时使用 C++ forward_list 在 C++ 库接口中安全使用容器 STL Containers - 向量、列表和双端队列之间的区别 对 STL 容器的安全并行只读访问 std::map&lt;int,int&gt;与向量的向量 c++中数组的动态大小? 如何定义运算符,以便将用户定义类型数组转换为原始类型数组? 热门教程 HTML/CSS教程JAVASCRIPT教程PHP教程JAVA教程ASP.NET教程Python教程Go教程Ruby教程C教程C++教程
© 2023-2024 沃梦达编程网 版权所有并保留所有权 ICP备案号:粤ICP备14083021号 SiteMap 本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!