STL のマップとマルチマップはどちらも一方向のマッピングであり、キーを通じてのみ値を見つけることができますが、実際のプロジェクト開発では、値から対応するキーを見つけることも必要になる場合があります。Boost のバイマップはそのような二重マッピング コンテナーですキーが必要です。値は一意である必要があります。
使用するには、ヘッダー ファイルをインクルードする必要があります。
#include<boost/bimap.hpp>
例
#include<iostream>
using namespace std;
//包含头文件
#include<boost/bimap.hpp>
using boost::bimap;
int main()
{
bimap<int, string> bi;
//左视图,代表 key 类型是int , value 类型是string
bi.left.insert(std::make_pair<int, string>(1, "jack"));
bi.left.insert(std::make_pair(2, "jay"));
//右视图,代表 key 类型是string , value 类型是int
bi.right.insert(std::make_pair("hello", 3));
cout << "遍历\n";
for (auto &node :bi.left)
{
cout << node.first << " " << node.second << endl;
}
cout << "查找\n";
//查找
auto it = bi.left.find(2); //左视图的key为整形
if (it != bi.left.end())
cout << it->first << "~" << it->second << endl;
auto it2 = bi.right.find("hello"); //右视图的key为字符串
if (it2 != bi.right.end())
cout << it2->first << "~" << it2->second << endl;
system("pause");
return 0;
}
結果: