STL map onto itself?(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 映射到自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:STL 映射到自身?


基础教程推荐
- 总计将在节日礼物上花多少钱 1970-01-01
- C语言数组 1970-01-01
- C++多态 1970-01-01
- 对 STL 容器的安全并行只读访问 2022-10-25
- C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
- 迭代std :: bitset中真实位的有效方法? 2022-10-18
- C语言3个整数的数组 1970-01-01
- 用指数格式表示浮点数 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 向量<unique_ptr<A>>使用初始化列表 2022-10-23