微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在由复合键索引的 multi_index_container 中查找特定索引值的元素

如何解决在由复合键索引的 multi_index_container 中查找特定索引值的元素

我必须使用以下容器,我想知道是否有办法找到共享特定 peerId 的所有节点。

如果第二个容器索引是一个简单的 ordered_non_unique 索引,我想我可以弄清楚,但我对 multi_index::tagmulti_index::composite_key 感到非常困惑。我不在乎 TimePoint,我只想简单地搜索具有指定 Node

的所有 peerid
using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;

typedef int64_t NodeId;
using PeerId = uint32_t;

struct Node {
    NodeId nodeid;  
    PeerId peerid;
    TimePoint nextRequestTime;

    Node(NodeId nodeid_,PeerId peerid_)
        : nodeid(nodeid_),peerid(peerid_),nextRequestTime(std::chrono::steady_clock::Now()) {}
};

using NodeSet = boost::multi_index_container<
    Node,boost::multi_index::indexed_by<
        // index by nodeid
        boost::multi_index::hashed_unique<
            boost::multi_index::member<Node,NodeId,&Node::nodeid>>,// sorted by peerid/nextRequestTime
        boost::multi_index::ordered_non_unique<
            boost::multi_index::tag<next_request_time>,boost::multi_index::composite_key<
                Node,boost::multi_index::member<Node,PeerId,&Node::peerid>,TimePoint,&Node::nextRequestTime>>>>>;

NodeSet nodes;

解决方法

使用

nodes.get<next_request_time>().equal_range(my_peerid)

返回一对迭代器到查找范围的开头和结尾。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。