c++ - 迭代 boost multi_index

标签 c++ boost igraph boost-multi-index

所以。我正在使用 igraph 对象,我想以特定顺序迭代顶点。顺序由称为“值”的顶点属性确定,我想按从高到低的顺序进行操作。 igraph 可以按顶点 ID 顺序将所有值作为 igraph_vector_t 提供。如果顶点17的值最大,我想先对它进行操作。

搜索 SO 之后,我开始研究 C++ boost multi_index .这是一个支持结构:

struct indexed_vertex {
    igraph_integer_t vid;
    igraph_real_t value;
    indexed_vertex(igraph_integer_t vid, igraph_real_t value):vid(vid),value(value){}

    bool operator<(const indexed_vertex &vertex) const {
        return value<vertex.value;
    }
};

我创建了以下索引对象:

typedef boost::multi_index::multi_index_container<
        indexed_vertex,
        boost::multi_index::indexed_by<
                boost::multi_index::hashed_unique<
                    boost::multi_index::member<indexed_vertex, igraph_integer_t, &indexed_vertex::vid>
                >,
                boost::multi_index::ordered_non_unique<
                        boost::multi_index::member<indexed_vertex, igraph_real_t, &indexed_vertex::value>
                >
        >
> indexed_vertex_set;

我的下一个技巧是按降序访问顶点。我尝试过这个(来自 docs )但几乎立即失败(嘿!快速失败,对吧?)

indexed_vertex_set ivs;
indexed_vertex_set::nth_index<1>::type::iterator it = ivs.get<1>();

错误

 error: no viable conversion from 'typename nth_index<1>::type' (aka 'boost::multi_index::detail::ordered_index<boost::multi_index::member<indexed_vertex, double, &indexed_vertex::value>, std::__1::less<double>, boost::multi_index::detail::nth_layer<2, indexed_vertex, boost::multi_index::indexed_by<boost::multi_index::hashed_unique<boost::multi_index::member<indexed_vertex, int, &indexed_vertex::vid>, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::member<indexed_vertex, double, &indexed_vertex::value>, mpl_::na, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, std::__1::allocator<indexed_vertex> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag, boost::multi_index::detail::null_augment_policy>') to 'indexed_vertex_set::nth_index<1>::type::iterator' (aka 'bidir_node_iterator<node_type>')
    indexed_vertex_set::nth_index<1>::type::iterator it = ivs.get<1>();

我已经尝试了一些其他变体,但总是回到这个错误。我会很感激建议。我以前没有使用过 multi_index,所以我预计我从根本上误解了范围。

奖励问题

既然是假期,我会指出我的下一个任务是做类似的事情

for (vertex in iterator) {
   get-vertex-id();
   get-vertex-value();
   look-up-vertex-and-modify();
}

因此,如果您慷慨解囊,我也非常感谢您的指导。

最佳答案

ivs.get<1>()给你索引,而不是迭代器。您需要调用begin() , end()和该索引上的其他方法来获取迭代器(就像你在容器上所做的那样)。你最好使用typedef虽然:

indexed_vertex_set ivs;
typedef indexed_vertex_set::nth_index<1>::type sorted_index;
sorted_index &idx = ivs.get<1>();
for( sorted_index::iterator it = idx.begin(); it != idx.end(); ++it ) {
    it->vid = 123; // getting access to fields
}

关于c++ - 迭代 boost multi_index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34322650/

相关文章:

c++ - 代码块电源功能在 c 中不起作用

c++ - 如何在 VSCode 上自动创建 C++ 类

c++ - C2470 错误 Visual Studio 2012 和 Qt 4.8.6

c++ - Boost WITH Writer block 中的多读取器、单写入器锁定

c++ - boost asio : "host not found (authorative)"

c++ - 在 DLIB 中通过霍夫变换结果进行过滤

c++ - 侵入式 rbtree 中第一个元素的恒定时间测试

r - 来自两列数据帧的简单网络/集群成员资格

python - igraph 中的可选箭头

javascript - 在 D3.js 中预先计算和设置节点的初始位置