SO. I am working with an igraph object and I want to iterate over vertices in a particular order. The order is determined by a vertex attribute called "value" and I'd like to operate highest-to-lowest. igraph can provide all of the values as an igraph_vector_t in vertex id order. If vertex 17 has the highest value, I want to operate on it first.
After searching SO, I starting looking into the C++ boost multi_index . Here is a supporting struct:
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;
}
};
I created the following index object:
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;
My next trick is to visit the vertices in descending order. I've attempted this (from the docs )but fail almost instantly (hey! fail fast, right?)
indexed_vertex_set ivs;
indexed_vertex_set::nth_index<1>::type::iterator it = ivs.get<1>();
with the error
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>();
I've tried a few other variants, but keep getting back to this error. I'd appreciate suggestions. I haven't used the multi_index before, so I'm expecting I am fundamentally misunderstanding the scope.
BONUS QUESTION
Since it's the holidays, I'll point out my next task will be to do something like
for (vertex in iterator) {
get-vertex-id();
get-vertex-value();
look-up-vertex-and-modify();
}
So if you're feeling generous, I'd appreciate guidance there as well.
ivs.get<1>()
gives you index, not iterator. You need to call begin()
, end()
and other methods on that index to get iterator (like you do on containers). You better use typedef
though:
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
}
With C++11 this can be simpler:
mic_structure mic;
// ...
for (auto & it : mic.get<0>()) {
// do something with iterator
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.