先来看看如何赋值把:

#include <iostream>
#include <vector>
#include <string>
#include<deque>
#include <set>
#include <map>
#include <boost/assign.hpp>
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{   using namespace boost::assign;//使用list_ofvector<int> v=list_of(1)(2)(3)(4)(5);deque<string>d=(list_of("hello")("rollen"));set<int>s=(list_of(10),20,30,40);map<int, string>m=list_of(make_pair(1,"hello"))(make_pair(2,"rollen"));//list_of可以全部使用括号,也可以将括号和逗号一起使用,但是对于后者需要// 将整个lits_of用括号括起来。否则编译器无法推导出list_of的类型而无法赋值。// 下面使用map_list_of 和pair_list_ofmap<int,int>mp=map_list_of(1,1)(2,2)(3,3);map<int,string>mp2=pair_list_of(1,"hello")(2,"rollen");//其实还有tuple_list_of
}

#include <iostream>
#include <vector>
#include <string>
#include<deque>
#include <set>
#include <map>
#include <multiset>
#include <boost/assign.hpp>
using namespace std;//减少重复输入int _tmain(int argc, _TCHAR* argv[])
{   using namespace boost::assign;vector<int>v=list_of(1).repeat(2,3)(4)(5);  //将3重复2次//v=1,3,3,4,5multiset<int>ms;insert(ms).repeat_fun(5,&rand).repeat(2,1),10;//ms=x,x,x,x,x,1,1,10deque<int>d;push_front(d).range(v.begin(),v.end()); //将一个序列的元素插入另外一个序列//d=1,3,3,4,5
}

与非标准容器一起使用

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <boost/assign.hpp>
using namespace std;//与非标准容器一起使用int _tmain(int argc, _TCHAR* argv[])
{   using namespace boost::assign;stack<int>s=(list_of(1),2,3).to_adapter();while(!s.empty()){cout<<s.top()<<" ";s.pop();}cout<<endl;queue<string>q=(list_of("hello")("rollen").repeat(2,"holt")).to_adapter();while(!q.empty()){cout<<q.front()<<" ";q.pop();}cout<<endl;priority_queue<double>pq=(list_of(1.21)(2.23)).to_adapter();while(!pq.empty()){cout<<pq.top()<<" ";pq.pop();}cout<<endl;
}

assign也支持部分不在STL中定义的非标准容器,比如slist和hash_map  hash_set 用法和标准容器一样、

此外,assign也支持大部分Boost的库容器

#include <iostream>
#include <vector>
#include <string>
#include <boost/assign.hpp>
using namespace std;//list_of的嵌套使用
// 构建二维数组int _tmain(int argc, _TCHAR* argv[])
{   using namespace boost::assign;vector<vector<int> >v=list_of(list_of(1)(2))(list_of(3)(4));v+=list_of(5)(6),list_of(7)(8);int a=1,b=2,c=3;vector<int>v1=cref_list_of<3>(a)(b)(c);  //也可以使用ref_list_ofassert(v.size()==3);
}

#include <boost/swap.hpp>
using namespace std;//交换两个数组,两个数组的长度必须一致int _tmain(int argc, _TCHAR* argv[])
{   int a1[10];int a2[10];std::fill_n(a1,10,1);std::fill_n(a2,10,2);boost::swap(a1,a2);
}

特化 swap

#include <iostream>
#include <vector>
#include <string>
#include <boost/swap.hpp>
using namespace std;class point{
public:explicit point(int a,int b,int c):x(a),y(b),z(c){}void print()const{cout<<x<<" "<<y<<" "<<z<<endl;}void swap(point &p){std::swap(x,p.x);std::swap(y,p.y);std::swap(z,p.z);cout<<"inner swap"<<endl;}
private:int x,y,z;
};//特化std::swap  原则上不能动stdnamespace std{template<>void swap(point &x,point &y){x.swap(y);}
}int _tmain(int argc, _TCHAR* argv[])
{   point a(1,2,3);point b(4,5,6);cout<<"std swap"<<endl;std::swap(a,b);cout<<"boost swap"<<endl;boost::swap(a,b);
}

由于我们特化了swap,因此boost::swap 和std::swap效果一样

特化ADL可找到的swap

#include <iostream>
#include <vector>
#include <string>
#include <boost/swap.hpp>
using namespace std;class point{
public:explicit point(int a,int b,int c):x(a),y(b),z(c){}void print()const{cout<<x<<" "<<y<<" "<<z<<endl;}void swap(point &p){std::swap(x,p.x);std::swap(y,p.y);std::swap(z,p.z);cout<<"inner swap"<<endl;}
private:int x,y,z;
};void swap(point &x,point &y){x.swap(y);
}int _tmain(int argc, _TCHAR* argv[])
{   point a(1,2,3);point b(4,5,6);cout<<"std swap"<<endl;std::swap(a,b);cout<<"boost swap"<<endl;boost::swap(a,b);
}

转载于:https://www.cnblogs.com/rollenholt/archive/2012/03/27/2419217.html

boost 学习笔记相关推荐

  1. Boost学习笔记(一)——Boost使用基础、内存管理

    一.Boost使用基础 Boost库的大部分组件(90%左右),不需要进行编译,直接包含头文件即可使用. #include <boost/logic/tribool.hpp> using ...

  2. Boost学习笔记-智能指针

    1.  智能指针 scoped_ptr 只在作用域内生效,离开作用域既释放资源,不能复制和赋值.类似于标准库的auto_ptr,但它相对于auto_ptr的优势在于,他的要求更严格,使用起来更安全.a ...

  3. C/C++ 开发 boost 库参考手册整理(2) 【学习笔记】

    文档声明: 以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正.并且该文档在后期会随着学习的深入不断补充完善.感谢各位的参考查看. 笔记资料仅供学习交流使用,转载请标明出处 ...

  4. 学习笔记: BOOST拓扑

    BOOST 拓扑的应用 学习笔记:https://www.bilibili.com/video/BV1D4411y7V8/ ⭕️ 升压 boost升压电路(boost converter or ste ...

  5. Boost库学习笔记(二)算法模块-C++11标准

    Boost库学习笔记(二)算法模块-C++11标准 一.综述 Boost.Algorithm是一系列人通用推荐算法的集合,虽然有用的通用算法很多,但是为了保证质量和体积,并不会将太多通用算法通过审查测 ...

  6. Boost库学习笔记(一)安装与配置

    Boost库学习笔记(一)安装与配置 1. 获取boost https://www.boost.org/users/history/version_1_79_0.html 任选其一 boost的目录结 ...

  7. SVO 学习笔记(深度滤波)

    SVO 学习笔记(深度滤波) 这篇博客 论文中的深度滤波 深度滤波的代码流程 更新Seed对象 初始化Seed对象 结尾 这篇博客  这篇博客将介绍SVO论文中的Mapping部分,主要介绍深度滤波器 ...

  8. SVO学习笔记(二)

    SVO学习笔记(二) 这篇文章 稀疏图像对齐 地图点投影(地图与当前帧间的关系) reprojectMap reprojectPoint reprojectCell 特征点对齐中的非线性优化 结尾 这 ...

  9. OpenCV学习笔记(四十一)——再看基础数据结构core OpenCV学习笔记(四十二)——Mat数据操作之普通青年、文艺青年、暴力青年 OpenCV学习笔记(四十三)——存取像素值操作汇总co

    OpenCV学习笔记(四十一)--再看基础数据结构core 记得我在OpenCV学习笔记(四)--新版本的数据结构core里面讲过新版本的数据结构了,可是我再看这部分的时候,我发现我当时实在是看得太马 ...

最新文章

  1. 1.2.3 OSI参考模型(2)
  2. display:inline-block的深入理解 转
  3. HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了
  4. 最新!北京电影学院成立“翟天临事件”调查组
  5. nginx 部署_部署 hexo 到 nginx
  6. nginx安装到指定目录
  7. Fedora 18在ASUS N6系列电脑上以太网卡驱动的安装
  8. Flink : UnknownTaskExecutorException: No TaskExecutor registered under
  9. git使用git push 命令跳出remote: Permission to Aname denied to usernameB 的问题
  10. 【网络安全工程师面试题】数据库存在的漏洞及渗透方法
  11. Kubernetes在游戏业务应用调研
  12. android代码删除wifi,Android Wifi的forget()操作实例详解_Android_脚本之家
  13. 如何让元素支持 height:100%效果
  14. 推荐5款优质的黑科技软件,好不好用你来判断
  15. 英特尔最新超级计算机,全球超级计算机500强三分之二使用英特尔的处理器
  16. word保存时出现tmp文件、保存出错的恢复方法
  17. 音乐计算机曲谱狂妄之人,undertale狂妄之人简谱
  18. Win7的评分工具WinSAT
  19. 百度地图和谷歌地图的比例尺和分辨率
  20. Ceph PG 归置组状态

热门文章

  1. 英语学习app源码_无纸化英语学习APP击败%89英语学习者
  2. 小程序分享如何自定义封面?
  3. 知识计算机硬件 教学设计,“计算机硬件系统”教学设计
  4. php opcache 安装,php opcache安装和配置
  5. oracle 循环修改数据库,oracle对一个表的多行数据进行修改,SQL批量修改
  6. linux snmp 限制ip_Windows/Linux服务器监控软件推荐
  7. springboot怎么返回404_Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理
  8. 帆软报表如何传递主表原有参数给子表呢_报表工具--钻取功能--超链接下钻
  9. python绘制散点图的函数_Python用PyQt5绘制多彩随机散点图,基本控件之QPainter使用详解...
  10. linux日志2 1,cmd log 21 和 cmd 21 log的区别