【问题标题】:find elements for a specific index value in a multi_index_container indexed by a composite_key在由复合键索引的 multi_index_container 中查找特定索引值的元素
【发布时间】:2021-03-03 16:44:01
【问题描述】:

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

如果第二个容器索引是一个简单的ordered_non_unique 索引,我想我可以弄清楚,但我对multi_index::tagmulti_index::composite_key 感到非常困惑。我不关心TimePoint,我只想简单地搜索所有Nodes 指定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>,
                boost::multi_index::member<Node, TimePoint,
                                           &Node::nextRequestTime>>>>>;

NodeSet nodes;

【问题讨论】:

    标签: c++ boost-multi-index


    【解决方案1】:

    使用

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2017-11-11
      • 2019-11-16
      • 2021-07-16
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多