boost::adjacent_vertices() and boost::out_edges()

boost::adjacent_vertices() and boost::out_edges()


#include <boost/graph/adjacency_list.hpp>
#include <tuple> #include <algorithm> #include <iterator> #include <iostream> using namespace std; using namespace boost; int main() { typedef adjacency_list<setS, vecS,undirectedS> graph; graph g; enum { topLeft, topRight, bottomRight, bottomLeft }; add_edge(topLeft, topRight, g); add_edge(topRight, bottomRight, g); add_edge(bottomRight, bottomLeft, g); add_edge(bottomLeft, topLeft, g); graph::adjacency_iterator vit, vend; tie(vit, vend) = adjacent_vertices(topLeft, g); copy(vit, vend,ostream_iterator<graph::vertex_descriptor>{cout, "\n"}); graph::out_edge_iterator eit, eend; tie(eit, eend) = out_edges(topLeft, g); for_each(eit, eend,[&g](graph::edge_descriptor it) { cout << target(it, g) << '\n'; }); }


The above example code introduces functions to gain additional information on  points.  boost::adjacent_vertices() returns a pair of iterators that refer to points a point connects to. You call boost::out_edges() if you want to access all outgoing lines from a point. boost::in_edges() accesses all ingoing lines. With undirected lines, it doesn’t matter which of the two functions is called.

boost::target() returns the end point of a line. The start point is returned with boost::source().

The code writes 1 and 3, the indexes of the top right and bottom left fields, to standard output twice. boost::adjacent_vertices(), is called with topLeft and returns and displays the indexes of the top right and bottom left fields. topLeft is also passed to boost::out_edges() to retrieve the outgoing lines. Because boost::target() is called on every outgoing line with std::for_each(), the indexes of the top right and bottom left fields are displayed twice.