This documentation is being rewritten. If something looks off, please cross-check with the Boost 1.91.0 Boost.Graph docs and open an issue.

adjacency_iterator_generator

Synthesizes a vertex-iterator out of an existing out-edge iterator. Reads each out-edge and yields its target vertex.

Defined in: <boost/graph/adjacency_iterator.hpp>

You almost certainly do not need this page.

Most BGL users just call adjacent_vertices(v, g) on any graph that models Adjacency Graph (which every built-in BGL graph class does). This page is for people implementing a new graph class that models Incidence Graph and who want the compiler to give them the adjacency iterator for free.

Example

// Most BGL users never construct an adjacency_iterator directly —
// they call `adjacent_vertices(v, g)`, which returns a pair of them.
//
// This example shows the plain-vanilla use via adjacent_vertices, then
// the `type` exposed by adjacency_iterator_generator for the same graph.
// You only reach for the generator when implementing a new graph type
// that models IncidenceGraph but not (yet) AdjacencyGraph.

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <iostream>
#include <type_traits>

using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;

int main() {
    Graph g(4);
    add_edge(0, 1, g);
    add_edge(0, 2, g);
    add_edge(0, 3, g);

    // Everyday path: adjacent_vertices returns an (iterator, iterator) pair
    // whose value_type is vertex_descriptor.
    std::cout << "neighbors of 0:";
    for (auto v : make_iterator_range(adjacent_vertices(0, g))) {
        std::cout << ' ' << v;
    }
    std::cout << '\n';

    // The generator produces a type equivalent to what adjacency_list
    // already exposes as graph_traits<Graph>::adjacency_iterator.
    using Generated = adjacency_iterator_generator<
        Graph,
        graph_traits<Graph>::vertex_descriptor,
        graph_traits<Graph>::out_edge_iterator>::type;

    using Existing = graph_traits<Graph>::adjacency_iterator;

    std::cout << "generator matches graph_traits: "
              << std::boolalpha << std::is_same<Generated, Existing>::value
              << '\n';
}
neighbors of 0: 1 2 3
generator matches graph_traits: true

The example shows both paths:

  1. adjacent_vertices(v, g) — the everyday way to iterate over neighbors.

  2. adjacency_iterator_generator<…>::type — the type you would publish as adjacency_iterator in your own graph class.

For existing graph types like adjacency_list, the generator produces the same type that graph_traits already exposes; there is no reason to instantiate it yourself.

Synopsis

namespace boost {

template <class Graph,
          class VertexDescriptor = typename graph_traits<Graph>::vertex_descriptor,
          class OutEdgeIter      = typename graph_traits<Graph>::out_edge_iterator>
class adjacency_iterator_generator {
public:
    typedef /* iterator_adaptor instantiation */ type;
};

} // namespace boost

Template parameters

Parameter Description

Graph

The graph type. Must model Incidence Graph.

VertexDescriptor

The vertex-descriptor type. Defaults to graph_traits<Graph>::vertex_descriptor. The parameter exists because the generator is typically instantiated inside the graph class’s declaration, where graph_traits<Graph> cannot yet be resolved.

OutEdgeIter

The source out-edge iterator type. Defaults to graph_traits<Graph>::out_edge_iterator, with the same caveat as above.

Members

adjacency_iterator_generator<…>::type satisfies the Multi-Pass Input Iterator concept. It exposes every operator required of a Random Access Iterator, except that reference equals value_type, so operator*() returns by value.

Constructor:

adjacency_iterator_generator<…>::type(const OutEdgeIter& it, const Graph* g);

See also