boost-容器
1、array
array相当于是一个增加了STL容器接口的数组,但它不像vector等容器一样可以动态增长,如果需要动态变动array的容量可以使用boost::scoped_array。array适用与对运行速度要求很高的场合。C++11中已支持array。
#include <algorithm>
using std::sort; #include "boost/array.hpp"
#include "boost/typeof/typeof.hpp"
using namespace boost; ....... array<int, > ary;
array<int, > ary2 = { , , , , };//可以使用{}初始化array
ary = ary2;//赋值
swap(ary, ary2)//互换 ary.assign();//所有元素赋值为0
ary[] = ;//头元素
ary.back() = ;//尾元素
ary.at();//使用at访问元素 int*p = ary.c_array();//获得原始数组指针
int s = ary.size();//获得数组中元素个数
sort(ary.begin(), ary.end());//使用STL排序函数对其排序 for (BOOST_AUTO(pos, ary.begin()); pos != ary.end(); ++pos)//遍历数组,使用BOOST_AUTO需要包含"boost/typeof/typeof.hpp"头文件
{
int iNum;
iNum = *pos;
}
2、unordered_map、unordered_set
unordered_map和unordered_set都是散列容器(hash container),他们的查询和修改性能都优于map和set。unordered_map中的元素是乱序的,map则默认按照键key从小到大排序存储。
C++11中已支持unordered_map和unordered_set,且其用法与map, set相同。
对于自定义类型的key,map需要在自定义类型中重载< 或者使用函数对象类,如下所示:
//方法1
struct Data
{
int num;
bool operator < (Data const& A) const
{
return num < A.num;
}
}; map<Data, int> m; //方法2
struct Data
{
int num;
}; class sort
{
public:
bool operator() (Data const &data1, Data const &data2) const
{
return data1.num < data2.num;
}
}; map<Data, int, sort> m;
如果unordered_map的key是自定义类型的话,需要添加两个函数对象类,一个用来计算哈希值,一个用来比较值是否相同,如下所示:
#include <unordered_map>
using std::unordered_map;
using std::pair; #include "boost\functional\hash.hpp" struct Data
{
int x;
int y;
}; struct HashValue
{
size_t operator()(const Data& p)const
{
size_t seed = ;
boost::hash_combine(seed, p.x);
boost::hash_combine(seed, p.y);
return seed;
}
}; struct IfEqual
{
bool operator()(const Data &data1, const Data &data2) const
{
return (data1.x == data2.y && data1.y == data2.y);
}
}; unordered_map<Data, int, HashValue, IfEqual> m;
boost中的hash类可以计算int、float、string等类型的哈希值,如下所示,hash_combin()函数可以获得两个哈希值的合并值。C++11中也提供了hash类来计算哈希值,不过经我实验,boost与C++11获取相同的一个int值的hash值不同,不知是不是二者使用的hash算法不同导致的还是怎么回事
#include "boost\functional\hash.hpp"
size_t hashValue1 = , hashValue2 = ;
hashValue1 = boost::hash<int>()();
boost::hash<int> HashObj;
hashValue2 = HashObj();
3、bimap
map是单向的(key—>value)关联容器,bimap则可以提供双向映射。bimap有两个视图,左视图和右视图,分别用成员变量left和right访问,相当于两个不同方向的map,其用法与map基本一致。
由于是双向映射,所以bimap里不仅元素的key不能相同,value也不能相同。
bimap<int, std::string> bmCity;
typedef bimap<int, std::string>::value_type bimap_value;//map元素类型为pair(使用make_pair()来生成),bimap元素类型为value_type bmCity.insert(bimap_value(, "北京"));
bmCity.insert(bimap_value(, "上海"));
bmCity.insert(bimap_value(, "广州"));
bmCity.insert(bimap_value(, "北京")); /*正常作为map使用*/
bimap<int, std::string>::left_map::const_iterator iter, iter_end;
iter = bmCity.left.begin();
iter_end = bmCity.left.end();
for (; iter != iter_end; iter++)//遍历
{
cout << iter->first << "-->" << iter->second << endl;
}
iter = bmCity.left.find();//通过key索引value
if (iter != iter_end)
cout << iter->second << endl; /*将value作为key,key为value*/
bimap<int, std::string>::right_map::const_iterator iter_right, iter_end_right;
iter_right = bmCity.right.begin();
iter_end_right = bmCity.right.end();
for (; iter_right != iter_end_right; iter_right++)//遍历
{
cout << iter_right->first << "-->" << iter_right->second << endl;
}
iter_right = bmCity.right.find("上海");//通过value索引key
if (iter_right != iter_end_right)
cout << iter_right->second << endl;
4、circular_buffer
circular_buffer实现了一个循环容器,故其大小是固定的,当达到末尾时自动循环使用容器的另一端空间,如图所示:

添加元素:push_back()、push_front()、insert()。
删除元素:pop_back()、pop_front()、erase()。
判断容器是否已满:full()。
把缓冲区线性化一个连续的普通数组:linearize()。
来检测是否可以线性化:is_linearize()。
从指定的迭代器位置旋转整个容器:rotate()。
需要注意的是circular_buffer的迭代器不是循环的,指向或超过end()的迭代器也会引发断言异常,前面所说的到达末尾时自动循环使用容器的另一端空间指的是当达到容器的容量上限,继续push_back方法压入元素时,原来begin处的元素就会被覆盖,原来begin + 1处的元素成为新的begin。如果我们需要一种容器在达到容量上限的情况下自动覆盖前面的数据的话就可以使用circular_buffer,比如用来存储用户最近3天的通话记录。
circular_buffer_space_optimized是circular_buffer的适配器,circular_buffer是在其定义的时候就分配好了内存,而circular_buffer_space_optimized只有在确实需要时(比如插入元素时)才分配内存,且容器内元素减少时也会自动释放内存。
#include "boost\circular_buffer.hpp"
#include "boost\typeof\typeof.hpp"
using namespace boost; int main()
{
circular_buffer<int> cb();//定义大小为5的循环容器
cb.push_back();
cb.push_front(); BOOST_AUTO(pos, cb.begin());//需包含"boost\typeof\typeof.hpp"
for (; pos != cb.end(); pos++)//遍历循环容器,仅会输出0, 1
std::cout << *pos << ", " ; std::cout << *(pos) << std::endl;//迭代器到达end(),继续输出的话会报错! return getchar();
}
5、dynamic_bitset
dynamic_bitset类似与STL的bitset,但它可以动态改变长度。dynamic_bitset不是容器 ,不支持迭代器和assign库等STL操作。所需头文件:"boost/dynamic_bitset.hpp"。
6、multi_array
multi_array是多维容器,相当于vector< vector<T> >这种效果,但操作更方便。
本文引用和参考出处:《boost程序库完全开发指南》.罗剑锋
boost-容器的更多相关文章
- boost容器bimap简单使用
C++标准提供了map和multi_map,把key映射到value; 但是这种映射是单向的,只能是key到value,不能反过来; boost.bimap扩展了标准库映射型容器,提供双向 ...
- gdb打印STL和boost容器
http://note.youdao.com/noteshare?id=b581e0db0084b6ba3011d9d27d372c91
- boost开发指南
C++确实很复杂,神一样的0x不知道能否使C++变得纯粹和干爽? boost很复杂,感觉某些地方有过度设计和太过于就事论事的嫌疑,对实际开发工作的考虑太过于理想化.学习boost本身就是一个复杂度,有 ...
- 为 CmakeLists.txt 添加 boost 组件
目录 为 CmakeLists.txt 添加 boost 组件 Boost 常用组件 1.时间与日期 timer, date_time, chrono 2.内存管理 system 3.实用工具库 4. ...
- boost之数据结构和容器
1.静态数组array,boost对静态数组进行了封装,使用和普通数组一样的初始化式进行初始化. #include <iostream> #include <boost/array. ...
- boost::multi_index 多索引容器
#include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...
- 2.boost遍历数组容器
#include <iostream> #include <string> #include<boost/array.hpp>//区别 using namespac ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
随机推荐
- 专项测试——移动app安装包检测
一.背景和现状 安装包的重要性无需多提,针对安装包质量控制越来越严格和规范,包括证书.文件大小.安装成功率等,APP的证书及混淆是影响APP的安装成功率及代码安全性的很大因素,随着功能迭代,安装包也会 ...
- Instrumentation类——Android自动化测试学习历程
这里需要把Instrumentation类的视频的上.中.下三集一起看,把内容总结一下... 视频地址: http://study.163.com/course/courseLearn.htm?cou ...
- day 14 项目目录规范; time ; logging
import sys print(sys.modules) 程序一运行,解释器将里面的所有内容全部加载到内存 项目目录规范: 代码不可能全部只写在一个文件,十几万行代码,调整,修改都很不方便. 所 ...
- Hibernate+struct web项目问题总结
问题一: ClassTable is not mapped [from ClassTable] 解决办法:在添加资源路径 <mapping resource="***/***/***/ ...
- c# 关于取小数点后值四舍五入精度问题
---恢复内容开始--- 最近做一个校验码验证法算法的生成程序,涉及到取小数点后值的问题;对其中遇到的问题做一下总结: 1:ToString()转换时碰到0.9999999999999之类的数据,给自 ...
- 4J - 前m大的数
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就 ...
- andorid 三种方式的练习
layout1 线性布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- 简单使用DESeq2/EdgeR做差异分析
简单使用DESeq2/EdgeR做差异分析 Posted: 五月 07, 2017 Under: Transcriptomics By Kai no Comments DESeq2和EdgeR都 ...
- Luogu 2059 [JLOI2013]卡牌游戏 - 概率DP
Solution 设状态 $F[i][j] $为 还剩余 $i$ 个人时, 第 $j$ 个人 的胜率. 边界: $F[1][1] = 1$(只剩下一个人了). 这样设置状态就能使 $i-1$ 个人的答 ...
- Luogu 3690 LCT - 模板
推荐几篇比较好的博客: FlashHu 的 讲解比较好 : 传送门 Candy 的 代码~ : 传送门 以及神犇Angel_Kitty的 学习笔记: 传送门 Code V 模板 #include< ...