代码改变世界

boost 学习笔记

2012-03-27 11:05  Rollen Holt  阅读(2931)  评论(0编辑  收藏  举报

先来看看如何赋值把:

#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;
}

image

 

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

image

由于我们特化了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);
}

image