简体   繁体   中英

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++. 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. In the second case multimap , given node A you can easily obtain an iterator with all adjacent nodes.

You will need to insert pairs twice or use some rule like always adding edges in lexicographical order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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