C++容器的迭代器失效规则是什么?
优选地以摘要列表格式.
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
我对如何使用boost库实际创建Graph感到困惑,我查看了示例代码,并且没有任何注释解释它的作用.
如何制作图形,并随时添加顶点和边缘?
由于我的图形使用setS for vertex,我必须为我的图形提供vertex_index属性映射,或者为write_graphviz提供一个显式的vertex_id参数,以便能够使用write_graphviz.
My graph is defined as: typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
NodeData和EdgeData是结构的地方.你能给我一个如何为我的图提供vertex_index属性映射的一个非常简单的例子吗?或者如何给write_graphviz一个明确的vertex_id参数?
谢谢
我是boost noob。我想知道为什么在以下代码中编译失败。我正在创建一组顶点,并尝试分配我自己的顶点索引和顶点名称。(我正在关注此页面:http : //fireflyblue.blogspot.com/2008/01/boost-graph-library.html。)
我知道vertS顶点列表Boost不需要明确的顶点 id 创建,我也在 Stackoverflow(如何为我的图提供 vertex_index 属性)中看到了这个非常相关的问题,它讨论了如何使用 anassociative_property_map来分配顶点索引。下面虽然 - 获取 vertex_index 映射,并分配键值对 - 似乎是一件相当简单的事情,我想了解它为什么失败。任何帮助是极大的赞赏!
编译错误如下:
error: expression is not assignable
vertIndx[v] = i;
//Define graph
typedef boost::property<boost::vertex_name_t, std::string> sv_namePty;
typedef boost::property<boost::vertex_index_t, int, sv_namePty > sv_indx_n_name_pty;
typedef boost::property<boost::edge_weight_t, int> se_weightPty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
sv_indx_n_name_pty, se_weightPty> ScafGraph;
//descriptors
typedef boost::graph_traits<ScafGraph>::vertex_descriptor SV;
typedef boost::graph_traits<ScafGraph>::edge_descriptor SE;
//Graph Object
ScafGraph SG;
//property accessors
boost::property_map<ScafGraph,
boost::vertex_name_t>::type vertName = boost::get(boost::vertex_name, SG);
boost::property_map<ScafGraph,
boost::vertex_index_t>::type …Run Code Online (Sandbox Code Playgroud) 我已经花了几天的时间来处理Boost图形库。据我了解,在考虑VertexList和EdgeList存储时:
vecS:
listS:
这有点短,但这是我的问题的关键。我需要这些索引号,并且希望以后能够轻松删除顶点。
我有这种图形结构的工作算法:
typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS,
topologicalmap::Intersection_Graph ,
boost::edge_weight_t,
boost::no_property > Graph_boost;
Run Code Online (Sandbox Code Playgroud)
我有一个Intersection_Graph需要使用的顶点自定义结构。在这里我使用vecS。
我想改用listS来删除顶点。同样,我希望以后能够与Dijkstra算法一起使用。
我有点理解我需要boost::vertex_index_t在列表中,但是我对如何做到并同时保留自定义结构感到非常困惑。
我尝试了一些方法:
typedef boost::adjacency_list<
boost::listS, boost::listS, boost::undirectedS,
boost::property<boost::vertex_index_t, topologicalmap::Intersection_Graph>,
boost::edge_weight_t,
boost::no_property > Graph_boost;
Run Code Online (Sandbox Code Playgroud)
但是我什至无法访问我的自定义结构。另外,索引访问不起作用。
我真的需要那种索引访问功能,因为我的图的算法将取决于返回父节点的索引。我觉得我可以摆脱使用Vertex而不是索引的习惯,但这意味着代码需要大量重写,我想知道是否可以避免使用它。
所以我的问题是:在保持listS优势的同时,有什么方法可以使listS表现得像vecS一样?
请,如果这听起来很愚蠢,请忍受我。我现在很困惑,所以我可能会说些愚蠢的话。如果您需要更多信息,请询问。
我一直试图让升级图lib的dijkstra_shortest_paths编译大约一个星期,现在没有用.我试图使用外部属性映射来模板化方法所需的不同命名参数.我的图形使用顶点和边缘的捆绑属性,我已经能够成功构建图形.我会告诉你我的代码:
// vertex bundled properties
struct BusStop
{
unsigned int id; //used for creating vertex index property map
string name;
Location* pLocation;
};
// edge bundled properties:
struct Route
{
string routeName;
BusType type;
float distance;
};
Run Code Online (Sandbox Code Playgroud)
这是我的图表声明:
typedef boost::adjacency_list<boost::vecS, boost::setS, boost::undirectedS, BusStop, Route> BusRouteGraph;
这是我尝试在给定图形上执行dijkstra最短路径的方法:
template<typename Graph>
bool shortestPathSearch(Graph& g, typename
boost::graph_traits<Graph>::vertex_descriptor src,
typename boost::graph_traits<Graph>::vertex_descriptor dest)
{
bool bPathFound = false;
VertexIndexMap index_map = get(&BusStop::id, g);
// Initialize index_map
typedef typename graph_traits<Graph>::vertex_iterator V_Iter;
V_Iter v_iter, v_iter_end;
int c = …Run Code Online (Sandbox Code Playgroud) 我有一个图表(adjacency_list(listS,vecS,bidirectionalS,VertexVal)),我需要删除100,000多个节点.每个节点还包含2个64位整数和另一个64位整数的结构.下面代码中发生的guid检查是检查结构中的第一个整数.
根据VTune,在我的笔记本电脑(i7 2.7GHz,16GB RAM)上大约需要88秒.
以下是我删除节点的方法:
vertex_iterator vi,vi_end;
boost::tie(vi, vi_end) = boost::vertices(m_graph);
while (vi!=vi_end) {
if (m_graph[*vi].guid.part1 == 0) {
boost::remove_vertex(*vi,m_graph);
boost::tie(vi, vi_end) = boost::vertices(m_graph);
} else
++vi;
}
Run Code Online (Sandbox Code Playgroud)
Vtune显示boost :: remove_vertex()调用需要88.145秒.有没有更有效的方法来删除这些顶点?
