K. Shores
K. Shores

Reputation: 1005

Using Boost Property Maps with Bundled Types

I am having issues getting the property map for the graph I am making. I am using bundled properties. I have stripped it down to the example below. I receive an error when I try to grab the type for IndexMap. The error from both a VC++ compiler and gcc is something along the lines of error forming a reference to void or illegal use of type void. The error is inside of boost's adjacency_list.hpp, but is caused by my instantiation of boost::property_map. I have been unable to understand what the correct type should be. I read the boost docs on bundled properties, but found them a little unhelpful. Any thoughts?

Edit: I am using boost 1.67.0.

Edit2: Apparently changing to vecS instead of listS for the vertex representation fixes this. However, I would prefer to use listS since I need to modify the graph while iterating over it.

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <string>
#include <memory>
#include <utility>
#include <vector>

struct MyVertex
{
    std::string m_name;
};

int main()
{
    using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
    using Vertex = boost::graph_traits<graph_t>::vertex_descriptor;
    using IndexMap = boost::property_map<graph_t, boost::vertex_index_t>::type;
    std::unique_ptr<graph_t> graph;
    std::vector<std::pair<int, int>> edges{ {0,1}, {0,2}, {1,2}, {3,4}, {1,3}, {1,4}};
    graph = std::make_unique<graph_t>(edges.begin(), edges.end(), 5);
    IndexMap index = boost::get(boost::vertex_index, *graph);
    return 0;
}

Upvotes: 2

Views: 686

Answers (1)

sehe
sehe

Reputation: 393829

Like I explained earlier today your graph does not have a vertex index. If you want it to have one, you must add it yourself.

Live On Coliru

#include <boost/graph/adjacency_list.hpp>

struct MyVertex {
    int id;
    std::string name;
};

using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
using Vertex = graph_t::vertex_descriptor;

int main() {
    graph_t g;
    auto v0 = add_vertex(MyVertex{0, "zero"},  g);
    auto v1 = add_vertex(MyVertex{1, "one"},   g);
    auto v2 = add_vertex(MyVertex{2, "two"},   g);
    auto v3 = add_vertex(MyVertex{3, "three"}, g);
    auto v4 = add_vertex(MyVertex{4, "four"},  g);

    for (auto [from, to] : { std::pair { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
        add_edge(from, to, g);
    }
}

Now you can use the id as the vertex index:

auto index = get(&MyVertex::id, g);

PS. In C++11 write

for (auto p : std::vector<std::pair<Vertex, Vertex> > { { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
    add_edge(p.first, p.second, g);
}

In C++03 write: Live On Coliru

Upvotes: 2

Related Questions