adding an external property to include index for vertix in graph (boost)
I'm trying to use an associative_property_map to include index for vertices, but I get the following error with the following simple code, what is the problem ?
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
struct NodeData
{
int label;
};
struct EdgeData
{
int age;
};
typedef map<vecS, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
typedef Graph::vertex_descriptor NodeID;
typedef Graph::edge_descriptor EdgeID;
int main()
{
Graph g;
NodeID n0 = add_vertex(g); g[n0].label = -1;
NodeID n1 = add_vertex(g); g[n1].label = -1;
EdgeID edge; bool ok;
tie(edge, ok) = boost::add_edge(n0, n1, g);
if (ok) g[edge].age = 10;
int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
put(propmapIndex, v, i++);
}
return 0;
}
Errors:
c:program filescodeblocksmingwbin..libgccmingw324.4.1........includeboostproperty_mapproperty_map.hpp||In
function 'void boost::put(const boost::put_get_helper&, K, const V&) [with PropertyMap = boost::associative_property_map, std::allocator >
, Reference = unsigned int&, K = void*, V = int]':| C:UsersmemoDesktopDebugedboostGraphmain.cpp|39|instantiated from here| c:program filescodeblocksmingwbin..libgccmingw324.4.1........includeboostproperty_mapproperty_map.hpp|361|error: no match for 'operator[]' in '(const boost::associative_property_map, std::allocator > > >&)((const boost::associative_property_map, std::allocator > > >*)(& pa))[k]'| c:program filescodeblocksmingwbin..libgccmingw324.4.1........includeboostproperty_mapproperty_map.hpp|498|note: candidates are: typename UniquePairAssociativeContainer::value_type::second_type& boost::associative_property_map::operator[](const typename UniquePairAssociativeContainer::key_type&) const [with UniquePairAssociativeContainer = std::map, std::allocator > >]| ||=== Build finished: 1 errors, 0 warnings ===|
Thanks
顶点描述符必须指定给IndexMap,因此它的map<NodeID, size_t>
而不是map<vecS, size_t>
:
<...>
typedef Graph::vertex_descriptor NodeID;
typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
<...>
// indexing all vertices
int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
put(propmapIndex, v, i++);
}
链接地址: http://www.djcxy.com/p/29744.html