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 映射到自身?
- 使用来自float.h和limits的数据,找到该系统的一些 1970-01-01
- “纯虚函数调用"在哪里?崩溃从何而来? 2022-10-18
- 打印扩展的ASCII字符 1970-01-01
- C++浮点常数 1970-01-01
- 使用最流行的转义序列 1970-01-01
- 运算符优先级 1970-01-01
- 使用整数值初始化char类型的变量 1970-01-01
- C语言可使用的所有转义序列 1970-01-01
- C语言求模 1970-01-01
- C++指向数组的指针 1970-01-01