C++视图的一侧与视图值的另一侧具有不同的键时,bimap 是否可能?怎么做?
Is C++ bimap possible with one side of view having different key than other side of the view value? How to do that?
一开始我需要一张地图,所以我使用了std::map。
然后,添加了一些要求,我还需要获取"值"的"键"(foos 代表bar(,所以我使用了
boost::bimaps::bimap<
boost::bimaps::unordered_set_of<boost::bimaps::tagged<std::string, foo>>,
boost::bimaps::multiset_of<boost::bimaps::tagged<std::string, bar>>>
在那之后,添加了更多要求,所以现在我需要为每个 foo 存储一个号码,从右侧视图我需要能够调用<bimap>.righ.find(bar)
并获得成对的(foo + 为 foo 存储的号码(,但我仍然希望能够拨打<bimap>.left.find(foo)
并获取酒吧。
如何实现?如果可能的话,我更喜欢一些现代C++而不是提升,但我想没有提升就很难拥有 bimap 功能。
编辑:我应该注意尺寸很重要,所以我不想存储任何涉及两次的部分,速度也很重要。
我应该有类似"foo1"+100 <-> "bar1"
和"foo2"+300 <-> "bar4"
.
我希望能够调用<bimap>.left.find("foo1")
并得到"bar1",
但也<bimap>.right.find("bar1")
并得到对("foo1",100(。
#include <boost/multi_index/hashed_index.hpp>
#include <boost/bimap/bimap.hpp>
using namespace std;
struct ElementType {
string foo;
string bar;
uint64_t number;
};
using namespace boost::multi_index;
using my_bimap = multi_index_container<
ElementType,
indexed_by<
hashed_unique<member<ElementType, string, &ElementType::foo>>,
ordered_non_unique<member<ElementType, string, &ElementType::bar>>
>
>;
int main() {
my_bimap instance;
instance.insert({"foo", "bar", 0});
instance.insert({"bar", "bar", 1});
cout << instance.get<0>().find("bar")->foo << endl;
cout << instance.get<0>().find("bar")->bar << endl;
cout << instance.get<0>().find("bar")->number << endl;
auto range = instance.get<1>().equal_range("bar");
for (auto it = range.first; it != range.second; ++it) {
cout << it->foo << endl;
cout << it->number << endl;
}
cin.sync();
cin.ignore();
}
输出:
bar
bar
1
foo
0
bar
1
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 我们是否能够在 C++20 中构建具有视图的容器?
- 是否存在一个范围::视图::group_by对应项,它将所有元素都考虑在内,而不是只考虑连续的元素
- 是否有一种安全的方法可以断言字符串视图是否以 null 终止?
- C++视图的一侧与视图值的另一侧具有不同的键时,bimap 是否可能?怎么做?
- 是否有范围::视图::转换的可修改视图版本
- OPENCV是否有一个软件包将3D点转换为2D自上而下的视图
- MFC 中是否有任何类似数据网格视图的控件
- 规则迭代器(或类似的范围/视图类)是否应从const_iterator派生
- 释放内存映射文件后取消映射视图是否安全
- 在显示视图之前,是否可以在内部更改模型数据
- 在树视图中使用复合模式是否有意义
- 是否可以从树视图的节点中删除一些复选框?
- 是否可以在Qt QAxWidget中关闭Internet Explorer兼容性视图?
- 在OpenGL 4.0中,是否有一种围绕局部坐标(即从模型视图矩阵)旋转的标准方法