Say I have some boost graph
#include <boost/graph/adjacency_list.hpp>
struct Vertex {
double property_1;
int property_2;
};
using Graph_t = boost::adjacency_list<boost::listS,
boost::listS,
boost::undirectedS,
Vertex,
boost::no_property>;
Graph_t g(5);
and now want to iterate over the vertices in different orders, say:
- by its id
- in a random order
- descending by
property_2
- ascending by
property_1
- descending/ascending by more bundled properties in a generic way.
How do I do this in the most efficient way?
As of now, I created std::vector
s with the properties, and vectors containing indices, and sorted them by the properties. But if you have many properties that creates a ton of structure that could be avoided.
I also looked at boost::multi_index
maps, as in this cplusplus.com question, but that doesn't seem slim to me either.
How can I do this? Happy about any hint!
That's (obviously) not a feature of the library.
You can however use ranges or range adaptors, like you would in any other situation:
Live On Coliru
Prints
More, From Here
std::ranges could give you most of these but in my experience has a few more limitations. However, it will be generally safer (because Boost Range V2 is quite old).
to have "living indexes" (like a database) make your vertex container selector select a Multi Index Container. See e.g. the advice here https://marc.info/?l=boost&m=118835654637830
to model your own graph datastructure, see e.g. here for inspiration
What is needed to use BGL algorithms on existing data structures ( edges and vertices as vector<Object *>)?
What is required for a custom BGL graph to work with topological sort?
UPDATE Code Generation With Boost PFR
In response to the comments, you could use Boost PFR to generate a array with comparators simple types statically:
Which you could use like Live On Compiler Explorer
Printing