【发布时间】:2021-03-03 16:44:01
【问题描述】:
我必须使用以下容器,我想知道是否有办法找到共享特定 peerId 的所有节点。
如果第二个容器索引是一个简单的ordered_non_unique 索引,我想我可以弄清楚,但我对multi_index::tag 和multi_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