我正在嘗試迭代提升圖。在迭代時,我試圖從該頂點找到傳入邊和傳出邊。但是我遇到了分段錯誤。
我嘗試除錯并找到引發分段錯誤的行。
(頂點 -> 需要找出傳入和傳出邊的圖的頂點)
(圖 -> 提升圖)
//Finding out edges of vertex
boost::graph_traits<BGType>::out_edge_iterator ei, ei_end;
boost::tie(ei, ei_end) = out_edges( vertex, graph ); // error at this line
for( boost::tie(ei, ei_end) = out_edges(vertex, graph); ei != ei_end; ei)
{
auto target = boost::target ( *ei, graph );
graph[target]._isVisible = false;
}
//Finding in edges of vertex
boost::graph_traits<BGType>::in_edge_iterator ein, ein_end;
boost::tie(ein, ein_end) = in_edges( vertex, graph ); // error at this line
for( boost::tie(ein, ein_end) = in_edges(vertex, graph); ein != ein_end; ein)
{
auto source = boost::source ( *ein, graph );
graph[source]._isVisible = false;
}
uj5u.com熱心網友回復:
boost::tie(ei, ei_end) = out_edges( vertex, graph ); // error at this line
boost::tie(ein, ein_end) = in_edges( vertex, graph ); // error at this line
兩條評論都表明要么無效,graph要么vertex無效。檢查它graph仍然是對物件的有效參考。
如果是這樣,那么“顯然”vertex是不正確的。它可能超出范圍。考慮檢查它:
void hide_neighbours(BGV vertex, BGType& graph) {
assert(vertex < num_vertices(graph));
當您使用它時,我們可以簡化實作:
void hide_neighbours(BGV vertex, BGType& graph) {
assert(vertex < num_vertices(graph));
using boost::make_iterator_range;
for (auto e : make_iterator_range(out_edges(vertex, graph)))
graph[target(e, graph)]._isVisible = false;
for (auto e : make_iterator_range(in_edges(vertex, graph)))
graph[source(e, graph)]._isVisible = false;
}
現場演示
Live On 編譯器資源管理器
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/function_property_map.hpp>
struct VertexProps {
bool _isVisible = true;
};
using BGType = boost::adjacency_list< //
boost::vecS, //
boost::vecS, //
boost::bidirectionalS, //
VertexProps>;
using BGV = BGType::vertex_descriptor;
void hide_neighbours(BGV vertex, BGType& graph) {
assert(vertex < num_vertices(graph));
using boost::make_iterator_range;
for (auto e : make_iterator_range(out_edges(vertex, graph)))
graph[target(e, graph)]._isVisible = false;
for (auto e : make_iterator_range(in_edges(vertex, graph)))
graph[source(e, graph)]._isVisible = false;
}
int main() {
BGType graph(8);
add_edge(1, 5, graph);
add_edge(5, 3, graph);
add_edge(3, 7, graph);
add_edge(7, 4, graph);
add_edge(6, 5, graph);
add_edge(2, 5, graph);
auto annotate = boost::make_function_property_map<BGV>([&graph](auto v) {
auto id = std::to_string(v);
return graph[v]._isVisible ? id : "(" id ")";
});
print_graph(graph, annotate, std::cout << std::boolalpha);
hide_neighbours(4, graph);
print_graph(graph, annotate, std::cout << "\nNeigbours of 4 hidden:\n");
hide_neighbours(5, graph);
print_graph(graph, annotate, std::cout << "\nNeigbours of 5 hidden:\n");
}
印刷
0 -->
1 --> 5
2 --> 5
3 --> 7
4 -->
5 --> 3
6 --> 5
7 --> 4
Neigbours of 4 hidden:
0 -->
1 --> 5
2 --> 5
3 --> (7)
4 -->
5 --> 3
6 --> 5
(7) --> 4
Neigbours of 5 hidden:
0 -->
(1) --> 5
(2) --> 5
(3) --> (7)
4 -->
5 --> (3)
(6) --> 5
(7) --> 4
請注意,如果您指定一個無效的頂點,您將得到:
sotest: /home/sehe/Projects/stackoverflow/test.cpp:17: void hide_neighbours(BGV, BGType&): Assertion `vertex < num_vertices(graph)' failed.
其他提示
如果上述內容沒有立即洗掉/暴露問題,那么您有Undefined Behavior。也許graph確實是一個懸空參考,或者graph已經以非法方式修改(例如洗掉未清除的頂點)。
如果您需要幫助查找此類原因,請隨時使用更相關的代碼發布另一個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/464684.html
