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 映射到自身?
基础教程推荐
- C++按值调用 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C++输入/输出运算符重载 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- 初始化变量和赋值运算符 1970-01-01
- C++定义类对象 1970-01-01
- C++ #define 1970-01-01
- C语言访问数组元素 1970-01-01