Boost中的一些实用技术

简介:

类型推断:

#include <iostream>
#include <vector>
#include <string>
#include <boost/typeof/typeof.hpp>
using namespace std;

vector<string> func(){
	vector<string> v(10);
	return v;
}

int _tmain(int argc, _TCHAR* argv[])
{	
	BOOST_TYPEOF(2.0) x=2.0*3;
	BOOST_AUTO(years,2+3);

	BOOST_AUTO(&a , new double[11]);
	BOOST_AUTO(p , new string("hello"));

	BOOST_AUTO(v , func());
	return 0;
}

不能复制的类

#include<boost/utility.hpp>
using namespace std;

//不能复制的类
class demo : public boost::noncopyable{

};

int _tmain(int argc, _TCHAR* argv[])
{	
	demo a;
	demo b;
	//a=b;
	//a(b);
	return 0;
}

//向typeof注册自定义类型

#include <iostream>
#include <vector>
#include <string>
#include<boost/typeof/typeof.hpp>
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
using namespace std;

//向typeof注册自定义类型
// 
namespace ex{
	class demo_class{
		int a,b;
	};
}
BOOST_TYPEOF_REGISTER_TYPE(ex::demo_class)

int _tmain(int argc, _TCHAR* argv[])
{	
	BOOST_AUTO(x, make_pair("test",ex::demo_class()));
	cout<<typeid(x).name()<<endl;
}

赋值:

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <boost/assign.hpp>

int _tmain(int argc, _TCHAR* argv[])
{	
	using namespace boost::assign;
	std::vector<int>v;
	v+=1,2,3,4,5,9*10;

	std::set<std::string> s;
	s+="cpp","java","c","python";

	std::map<int,std::string>m;
	m+=std::make_pair(1,"hello"),std::make_pair(2,"rollen");

}
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <list>
#include <map>
#include <boost/assign.hpp>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{	
	using namespace boost::assign;
	std::vector<int>v;
	push_back(v)(1)(2)(3)(4);
	push_back(v),1,2,3,4,5;
	push_back(v),1,(2),3;


	list<int> l;
	push_front(l)(1)(2);

	set<double>s;
	insert(s)(12.23)(1.23);

	map<int,string>m;
	insert(m)(1,"name")(2,"age");
}

==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/03/26/2418676.html,如需转载请自行联系原作者
相关文章
|
1月前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
33 1
|
4月前
|
存储
boost 入门
boost 入门
38 0
|
算法 计算机视觉
OpenCV系列学习之(三)
OpenCV系列学习之(三)
OpenCV系列学习之(三)
|
人工智能 算法 程序员
OpenCV系列学习之(五)
OpenCV系列学习之(五)
OpenCV系列学习之(五)
|
算法 计算机视觉
OpenCV系列学习之(四)
OpenCV系列学习之(四)
OpenCV系列学习之(四)
|
算法 计算机视觉
OpenCV系列学习之(二)
OpenCV系列学习之(二)
OpenCV系列学习之(二)
|
算法 计算机视觉
OpenCV系列学习之(六)
OpenCV系列学习之(六)
OpenCV系列学习之(六)
|
计算机视觉 Python
OpenCV系列学习之(一)
OpenCV系列学习之(一)
OpenCV系列学习之(一)
|
存储 自然语言处理 安全