property_writer :: Boost.Graph Documentation

# property_writer

Writes the property value of a vertex or edge to an output iterator when an event occurs. Useful for debugging or logging during a graph traversal.

Defined in: `<boost/graph/visitors.hpp>`
Models: EventVisitor
Typical event: any (vertex or edge), e.g. `on_discover_vertex`, `on_examine_edge`

To use it, wrap it in an algorithm adaptor and optionally combine it with other event visitors; see the visitors overview.

## Synopsis

    template <typename PropertyMap, typename OutputIterator, typename EventTag>
    struct property_writer {
      typedef EventTag event_filter;
      property_writer(PropertyMap pa, OutputIterator out);

      template <class T, class Graph>
      void operator()(T x, Graph& g);
    };

    template <typename PropertyMap, typename OutputIterator, typename EventTag>
    property_writer<PropertyMap, OutputIterator, EventTag>
    write_property(PropertyMap pa, OutputIterator out, EventTag);

## Template Parameters

Parameter | Description
--- | ---
`PropertyMap` | A ReadablePropertyMap whose key type is the graph’s vertex or edge descriptor (depending on the event tag), and whose value type is writable to the output iterator.
`OutputIterator` | The output iterator the property values are written to.
`EventTag` | The event at which to write. May be any event, vertex or edge.

## Associated Types

Type | Description
--- | ---
`event_filter` | The event tag the visitor responds to. Same type as `EventTag`.

## Member Functions

Member | Description
--- | ---
`property_writer(PropertyMap pa, OutputIterator out)` | Constructs a writer reading from the property map `pa` and writing to `out`.
`void operator()(T x, Graph& g)` | Reads the property of `x` (a vertex or edge) and writes it to the output iterator: `*out++ = get(pa, x)`.

## Non-Member Functions

Function | Description
--- | ---
`write_property(PropertyMap pa, OutputIterator out, EventTag)` | Convenience factory that creates a `property_writer`.

## Example

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/breadth_first_search.hpp>
    #include <boost/graph/visitors.hpp>
    #include <iostream>
    #include <iterator>

    int main() {
        using namespace boost;
        using Graph = adjacency_list<vecS, vecS, undirectedS>;

        Graph g(5);
        add_edge(0, 1, g);
        add_edge(0, 2, g);
        add_edge(1, 3, g);
        add_edge(2, 4, g);

        // Print vertex indices as they are discovered
        std::ostream_iterator<int> out(std::cout, " ");
        auto vis = make_bfs_visitor(
            write_property(get(vertex_index, g), out, on_discover_vertex())
        );

        std::cout << "BFS discovery order: ";
        breadth_first_search(g, vertex(0, g), visitor(vis));
        std::cout << "\n";
    }

    BFS discovery order: 0 1 2 3 4