boost::remove_vertex函数介绍及测试程序

理解Boost Graph库的boost::remove_vertex函数
177 篇文章 ¥59.90 ¥99.00
本文介绍了Boost库中的boost::remove_vertex函数,用于删除图中的节点。详细解释了函数的参数、实现原理,并提供了一个测试程序,展示了如何在实际中使用此函数删除图节点。

boost::remove_vertex函数介绍及测试程序

在使用Boost库中的Graph库时,有时候我们需要对图进行一些操作,如添加或删除节点等。而boost::remove_vertex就是用来删除节点的一个便利函数。下面我将介绍该函数的用法,并提供一个测试程序供大家参考。

函数定义

该函数的定义如下:

template <class VertexListGraph, class Vertex>
inline void remove_vertex(Vertex v, VertexListGraph& g)
{
BOOST_CONCEPT_ASSERT(( VertexListGraphConcept ));
BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<
typename property_map<VertexListGraph, vertex_index_t>::const_type,
Vertex
> ));
typedef typename graph_traits::vertex_descriptor VertexDescriptor;
typedef typename std::vector VertexVector;
typedef typename property_map<VertexListGraph, vertex_index_t>::type VertexIndexMap;
VertexIndexMap index = get(vertex_index, g);

for (auto e = boost::edges(sdMap).first; e != boost::edges(sdMap).second; ++e) { auto start = std::chrono::high_resolution_clock::now(); const OsmEdgeProperties &edgeProps = sdMap[*e]; // get vertices geometry LineString linestring_global; std::string line_geometry_string = edgeProps.geometry; if (line_geometry_string.empty()) { boost::adjacency_list<>::vertex_descriptor source = boost::source(*e, sdMap); boost::adjacency_list<>::vertex_descriptor target = boost::target(*e, sdMap); if (source == target) continue; line_geometry_string = "LINESTRING ("; const OsmNodeProperties &startNodeProps = sdMap[*(vertices_pair.first + source)]; line_geometry_string = line_geometry_string + startNodeProps.x + " " + startNodeProps.y + ", "; const OsmNodeProperties &endNodeProps = sdMap[*(vertices_pair.first + target)]; line_geometry_string = line_geometry_string + " " + endNodeProps.x + " " + endNodeProps.y + ")"; } // auto end = std::chrono::high_resolution_clock::now(); boost::geometry::read_wkt(line_geometry_string, linestring_global); // auto end = std::chrono::high_resolution_clock::now(); // auto delay = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); // std::cout << "sdmap encode timer delay : " << delay << std::endl; // get Properties std::string fclass = edgeProps.fclass; std::vector<std::string> fclass_explode; if (fclass.front() == '[' && fclass.back() == ']') { fclass = fclass.substr(1, fclass.size() - 2); fclass.erase(std::remove_if(fclass.begin(), fclass.end(), ::isspace), fclass.end()); boost::split(fclass_explode, fclass, boost::is_any_of(",")); } else { fclass_explode.push_back(fclass); } for (size_t i = 0; i < fclass_explode.size(); i++) { fclass = "other"; for (const auto &pair : HIGHWAY_SETS) { if (pair.second.find(fclass_explode[i]) != pair.second.end()) { fclass = pair.first; break; } } fclass_explode[i] = fclass; } // transform geometry to local coordinate LineString linestring_local; transformGlobalToEgo(transformationMatrix, linestring_global, linestring_local); std::vector<LineString> inner_lines; // crop try { if (crop) { get_inner_lines(canvas_polygon, linestring_local, inner_lines); } else { inner_lines.push_back(linestring_local); } } catch (const std::exception &e) { std::cerr << "error: " << e.what() << std::endl; continue; } // 将裁剪得到的线段加入结果 for (std::string sub_fclass : fclass_explode) { for (LineString inner_line : inner_lines) { EdgeInfo edge_info{fclass, inner_line}; edges_info.push_back(edge_info); } } } 分析并理解这段代码,发现耗时逐渐递增主要发生在这行段for循环里。
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值