boost 学习笔记

先来看看如何赋值把:

#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_of
	vector<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_of

	map<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,5

	multiset<int>ms;
	insert(ms).repeat_fun(5,&rand).repeat(2,1),10;
	//ms=x,x,x,x,x,1,1,10

	deque<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_of
	assert(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  原则上不能动std

namespace 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);
}

时间: 2024-08-30 20:47:37

boost 学习笔记的相关文章

C++内存管理学习笔记(5)

/****************************************************************/ /*            学习是合作和分享式的! /* Author:Atlas                    Email:wdzxl198@163.com  /*  转载请注明本文出处: *   http://blog.csdn.net/wdzxl198/article/details/9112123 /************************

C++内存管理学习笔记(4)

/****************************************************************/ /*            学习是合作和分享式的! /* Author:Atlas                    Email:wdzxl198@163.com    /*  转载请注明本文出处: *   http://blog.csdn.net/wdzxl198/article/details/9094793 /**********************

JetSpeed学习笔记(一)

笔记 JetSpeed学习笔记(一) fuweilin 2005-4-7 前言 参加了公司的portal的兴趣小组,今天对portal进行学习.首先上网看了看一些portal的资料,对portal.portlet.portlet container以及JSR168等概念有个基本的了解.决定进一步实战的方式感受portal,于是学习JetSpeed.     1.  JetSpeed介绍JetSpeed是Apache组织开发的一个采用Java和XML的开放源代码的企业信息门户的实现.门户可以让终端

PHP输入输出流学习笔记

  这篇文章主要介绍了PHP输入输出流学习笔记,PHP输入和输出流是通过php://来访问的,它允许访问 PHP 的输入输出流.标准输入输出和错误描述符,内存中.磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器,需要的朋友可以参考下 PHP输入和输出流是通过php://来访问的,它允许访问 PHP 的输入输出流.标准输入输出和错误描述符, 内存中.磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器. php://stdin, php://stdout 和 php://std

PHP学习笔记 (1) 环境配置与代码调试

一配置PHP环境 1.了解什么是PHP PHP("PHP: Hypertext Preprocessor",超文本预处理器的字母缩写) PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写.PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用 2.PHP的背景和优势 PHP的发展背景 1).1994年由Rasmus Lerdorf创建,开始是一个简单的Perl语言编写的程序,用统计

Node.js 学习笔记之简介、安装及配置

 本文是Node.js学习笔记系列文章的第一篇,主要给大家讲解的是在Windows和Linux上安装Node.js的方法.软件安装版本以0.12.0为例.希望大家能够喜欢.     简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好. 谁适合阅

node.js学习笔记(9) 和谐模式

众所周知,ECMAScript 是一种开放的.国际上广为接受的脚本语言规范. 它本身并不是一种脚本语言.正如在 Web 应用程序中执行有用操作的 bean 集合(例如,Netscape 的 AWT)是 Sun 的 JavaBean 规范的一种实现一样,JavaScript 是 ECMAScript 规范的一种实现. 2015年6月17日,ECMA国际发布了EcmaScript2015,即EcmaScript6(以下简称ES6)草案的正式版.ES6是继ES5之后的一次主要改进,语言规范由ES5.1

php5学习笔记(转)

php5|笔记 作者: whhwq在phpv.net看到的感觉不错/*+-------------------------------------------------------------------------------+| = 本文为Haohappy读<<Core PHP Programming>> | = 中Classes and Objects一章的笔记 | = 翻译为主+个人心得 | = 为避免可能发生的不必要的麻烦请勿转载,谢谢 | = 欢迎批评指正,希望和所有

动态网页学习:JSP学习笔记全记录

js|笔记|动态|网页 JSP学习笔记(一)-----概述 JSP学习笔记(二)-----使用Tomcat运行JSP文件 JSP学习笔记(三)-----使用JSP处理用户注册和登陆 JSP学习笔记(四)-----JSP动作的使用 JSP学习笔记(五)-----JSP中使用JavaBean JSP学习笔记(六)-----在多个JSP页面之间传递参数 JSP学习笔记(七)-----猜数字游戏 JSP学习笔记(八)-----include的运用 JSP学习笔记(九)-----JSP标签库