[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppAdjacency_listTutorial2
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppAdjacency_listTutorial2
(C++) boost::adjacency_list tutorial #2
Goal of this tutorial:- Create a boost::adjacency_list with vertices
- Name the vertices and put these names in the boost::adjacency_list
- Retrieve the vertex names from the boost::adjacency_list
int main() { typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::property<boost::vertex_name_t,std::string> > MyGraph; typedef boost::property_map<MyGraph, boost::vertex_name_t>::type MyNameMap; MyGraph myGraph; //Add vertices and add names { for (int i=0; i!=10; ++i) { boost::add_vertex(myGraph); const std::string name(i+1,'x'); std::cout << "Vertex name created: '" << name << "'." << std::endl; boost::put(boost::vertex_name,myGraph,i,name); } } //Get names { const MyNameMap myNameMap = boost::get(boost::vertex_name,myGraph); typedef boost::graph_traits<MyGraph>::vertex_iterator VertexIterator; typedef std::pair<VertexIterator,VertexIterator> VertexRange; for (VertexRange i = boost::vertices(myGraph) ; i.first != i.second; ++i.first) { std::cout << "Vertex name found: '" << boost::get(myNameMap, *i.first) << "'." << std::endl; } } }
- include <string>
- include <boost/graph/adjacency_list.hpp>
boost::adjacency_list tutorials
- boost::adjacency_list tutorial #1
- boost::adjacency_list tutorial #2
- boost::adjacency_list tutorial #3
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
