我有以下内容:
struct foo_and_number_helper {
std::string foo;
uint64_t number;
};
struct foo_and_number {};
struct bar {};
using my_bimap = boost::bimaps::bimap<
boost::bimaps::unordered_set_of<boost::bimaps::tagged<foo_and_number_helper, foo_and_number>>,
boost::bimaps::multiset_of<boost::bimaps::tagged<std::string, bar>>
>;
my_bimap instance;
我希望能够像这样调用查找和删除方法:
instance.left.find("foo")
而不是 instance.left.find({"foo",1})
和
instance.left.erase("foo")
而不是 instance.left.erase({"foo",1})
.
我只想使用“foo_and_number_helper”的“foo”部分而不是两个部分用于从左侧调用的方法 find 和 erase。如何实现?我尝试阅读 bimap 实现,但我仍然很难做到。
我已经问了更广泛的问题:Is C++ bimap possible with one side of view having different key than other side of the view value? How to do that?
从评论中我必须覆盖 operator <
,但我什至不确定它是否足够。
最佳答案
我会选择 boost::multi_index_container
在 boost::bimap
上。
namespace bmi = boost::multi_index;
struct ElementType {
std::string foo;
std::string bar;
uint64_t number;
}
using my_bimap = boost::multi_index_container<
ElementType,
bmi::indexed_by<
bmi::unordered_unique<
bmi::tagged<struct Foo>,
bmi::member<ElementType, std::string, &ElementType::foo>
>,
bmi::ordered<
bmi::tagged<struct Bar>,
bmi::member<ElementType, std::string, &ElementType::bar>
>,
// and others like
bmi::sequenced<
bmi::tagged<struct InsertionOrder>
>
>
>;
然后你会像这样使用它
my_bimap instance;
instance.get<Foo>().find("foo");
instance.get<Bar>().erase("bar");
std::cout << instance.get<InsertionOrder>()[10].foo;
即您可以拥有任意数量的 View ,而不是拥有左
和右
View
关于c++ - 是否可以覆盖 boost::bimaps::bimap.left 的 "find"和 "erase"方法?怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50700091/