C++ Boost库:双向映射容器 bimap
文章目录1. 简介2. 代码示例1. 简介STL中的map和 multimap都是单向映射只能通过key查找到value,但是实际项目开发中,有时也需要从value找到对应的key,boost的 bimap便是这样一种双映射容器它要求key,value都必须唯一。使用需要包含头文件:#include<boost/bimap.hpp>2. 代码示例#include<iostream
C++ Boost库:简介和第一个示例程序
C++ Boost库:数值转换 lexical_cast
C++ Boost库:字符串格式化 format
C++ Boost库:字符串string_algo
C++ Boost库:字符串算法string_algo
C++ Boost库:类型推导BOOST_AUTO/BOOST_TYPEOF
C++ Boost库:分词处理库 tokenizer
C++ Boost库:windows下编译Boost库
C++ Boost库:日期时间库 date_time
C++ Boost库:智能指针scoped_ptr
C++ Boost库:数组智能指针 scoped_array
C++ Boost库:共享所有权的智能指针 shared_ptr
C++ Boost库:工厂函数 make_shared
C++ Boost库:共享有权的数组智能指针shared_array
C++ Boost库:弱引用智能指针 weak_ptr
C++ Boost库:禁止拷贝 nocopyable
C++ Boost库:计时器 timer
C++ Boost库:普通数组array
C++ Boost库:散列容器 unordered_set、unordered_multiset
C++ Boost库:散列容器 unordered_map、unordered_multimap
C++ Boost库:双向映射容器 bimap
C++ Boost库:环形缓冲区 circular_buffer
C++ Boost库:动态多维数组 multi_array
C++ Boost库:使用property_tree解析XML和JSON
C++ Boost库:简化循环 BOOST_FOREACH
C++ Boost库:随机数库 Random
C++ Boost库:引用库 ref
C++ Boost库:绑定库 bind
C++ Boost库:线程库 thread 跨平台多线程
C++ Boost库:互斥量 mutex
1. 简介
STL
中的map
和 multimap
都是单向映射只能通过key
查找到value
,但是实际项目开发中,有时也需要从value
找到对应的key
,boost
的 bimap
便是这样一种双映射容器它要求key
,value
都必须唯一。
使用需要包含头文件:
#include<boost/bimap.hpp>
2. 代码示例
#include<iostream>
using namespace std;
#include<boost/bimap.hpp>
#include<boost/timer/timer.hpp>
using namespace boost;
using namespace boost::timer;
int main()
{
bimap<int, string > bm;//左右视图有序
//左视图,代表 key 类型是int , value 类型是string
bm.left.insert(make_pair(1,"AAA"));
bm.left.insert(make_pair(1, "AAAA"));//不能插入重复的key
//右视图,代表 key 类型是string , value 类型是int
bm.right.insert(make_pair("BBB" ,2 ));
bm.right.insert(make_pair("BBB", 222));//不能插入重复的key
bm.left.insert(make_pair(4, "DDD"));
bm.right.insert(make_pair("CCC", 3));
cout << bm.size() <<" , "<< bm.left.size() << " , " << bm.right.size()<< endl;
//查找
auto it = bm.left.find(2); //左视图的key为整形
if(it != bm.left.end())
cout << it->first << " ->" << it->second << endl;
auto it2 = bm.right.find( "CCC" ); //右视图的key为字符串
if (it2 != bm.right.end())
cout << it2->first << " ->" << it2->second << endl;
cout << "------------------------------------" << endl;
//左视图的遍历
for (auto it = bm.left.begin(); it != bm.left.end(); ++it)
{
cout << it->first << " ->" << it->second << endl;
}
cout << "------------------------------------" << endl;
//右视图的遍历
for (auto it = bm.right.begin(); it != bm.right.end(); ++it)
{
cout << it->first << " ->" << it->second << endl;
}
getchar();
return 0;
}
运行结果:
更多推荐
所有评论(0)