简体   繁体   English

大小未知的无向图的C ++数据结构

[英]C++ data structure for an undirected graph of unknown size

I'm trying to make a program that explores an undirected graph of an unknown size and builds an adjacency list as it goes. 我正在尝试制作一个程序,该程序探索未知大小的无向图,并随其建立一个邻接表。 Normally I would make a set<set<String>> (the rooms are identified by a string) but I've been told this is unstable in C++. 通常我会set<set<String>>一个set<set<String>> (房间由一个字符串标识),但有人告诉我这在C ++中是不稳定的。 What would be a better data structure? 什么是更好的数据结构?

It depends on how you want to query the information later. 这取决于您以后如何查询信息。

I can suggest two alternatives: 我可以提出两种选择:

using namespace std;
set< pair<string,string> >

or 要么

using namespace std;
multimap<string,string>

In the first case set , you can check if two nodes are connected but you need to know both nodes (A and B) to run a query. 在第一种情况下set ,您可以检查是否两个节点连接,但你需要知道两个节点(A和B)运行查询。 In the second case multimap , given node A you can easily obtain an iterator with all adjacent nodes. 在第二种情况下multimap ,给定的节点A可以很容易地获得与所有相邻节点的迭代器。

You will need to insert pairs twice or use some rule like always adding edges in lexicographical order. 您将需要插入两次对或使用某些规则,例如始终按字典顺序添加边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM