gpt4 book ai didi

c++ - 关于 C++ Boost 图创建和 vertex_index 属性。

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:12 28 4
gpt4 key购买 nike

我是 boost 菜鸟。我想知道为什么以下代码编译失败。我正在创建一组顶点,并尝试分配我自己的顶点索引和顶点名称。 (我正在关注此页面:http://fireflyblue.blogspot.com/2008/01/boost-graph-library.html。)

我知道 Boost 中的 vertS 顶点列表不需要显式创建顶点 ID,而且我还在 Stackoverflow ( how provide a vertex_index property for my graph ) 中看到了这个非常相关的问题讨论如何使用 associative_property_map 分配顶点索引。以下虽然 - 获取 vertex_index 映射,并分配键值对 - 似乎是一件相当简单的事情,我想了解它失败的原因。任何帮助是极大的赞赏!

编译错误如下:

错误:表达式不可赋值
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 vertIndx = boost::get(boost::vertex_index, SG);
boost::property_map<ScafGraph,
boost::edge_weight_t>::type edgeWeight = boost::get(boost::edge_weight, SG);

//Populate Graph
std::vector<SV> svlist;
for(int i=0; i<4; i++) {
SV v = boost::add_vertex(SG);
svlist.push_back(v);
vertName[v] = std::to_string(i);
vertIndx[v] = i;
}

最佳答案

表达式 vertIndx[v] 按值返回一个顶点。因此,您会收到错误消息,因为当您尝试分配给它时它不是左值。

此外,它实际上返回v。下面是 vertIndx[v] 运行的代码:

inline value_type operator[](key_type v) const { return v; }

这里有一个版本,希望能清楚地说明它是如何工作的:

#include <boost\graph\adjacency_list.hpp>

int main()
{
//Define graph
typedef boost::adjacency_list
<
boost::vecS //! edge list
, boost::vecS //! vertex list
, boost::undirectedS //! undirected graph
, boost::property<boost::vertex_name_t, std::string> //! vertex properties : name
, boost::property<boost::edge_weight_t, int> //! edge properties : weight
> 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 vertIndx = boost::get(boost::vertex_index, SG);
boost::property_map<ScafGraph,
boost::edge_weight_t>::type edgeWeight = boost::get(boost::edge_weight, SG);

//Populate Graph
std::vector<SV> svlist;
for (int i = 0; i < 4; i++) {
SV v = boost::add_vertex(ScafGraph::vertex_property_type(std::to_string(i)), SG);
svlist.push_back(v);
assert(vertName[v] == std::to_string(i));
assert(vertIndx[v] == i);
}
return 0;
}

关于c++ - 关于 C++ Boost 图创建和 vertex_index 属性。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061033/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com