不,你不能。
当然,您可以拥有非唯一索引,但对于所有无父条目仍具有开销。
可以做什么,但是,是把指针放在一个单独的“索引”容器。然后负责你来保护这些容器的同步(以及指向对象的生命期)。
更新你甚至可以透明地使用参考包装:
using ParentIndex = bmi::multi_index_container<
boost::reference_wrapper<Node const>,
bmi::indexed_by<
bmi::ordered_non_unique<
bmi::tag<struct ordered_by_parent_id>,
bmi::const_mem_fun<Node, boost::optional<unsigned int>, &Node::parentId>
>
>
>;
这里有一个演示:
Live On Coliru
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#include <iostream>
namespace bmi = boost::multi_index;
struct Node {
unsigned int Id;
boost::optional<unsigned int> for_demo;
boost::optional<unsigned int> parentId() const { return for_demo; };
};
static std::ostream& operator<<(std::ostream& os, Node const& n) {
return os << "{" << n.Id << ", " << n.parentId() << "}";
}
using Nodes = bmi::multi_index_container<
Node,
bmi::indexed_by<
bmi::sequenced<>
>
>;
using ParentIndex = bmi::multi_index_container<
boost::reference_wrapper<Node const>,
bmi::indexed_by<
bmi::ordered_non_unique<
bmi::tag<struct ordered_by_parent_id>,
bmi::const_mem_fun<Node, boost::optional<unsigned int>, &Node::parentId>
>
>
>;
int main() {
Nodes table {
{1, boost::none}, {2, 5}, {3, boost::none}, {4, 5}, {5, 5}, {6, 2}
};
ParentIndex extra;
for (auto const& e : table)
if (e.parentId()) extra.emplace(e);
for (auto& e : table)
std::cout << e.Id << " ";
std::cout << "\nParented: ";
for (Node const& e : extra)
std::cout << e << "; ";
}
我能想到的唯一的事情是parentId的非唯一索引。您可以忽略空条目。 –