STL映射到自身? [英] STL map onto itself?

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

问题描述

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

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映射到自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆