boost 学习

智能指针的学习

中文教程网站 http://zh.highscore.de/cpp/boost/

不过代码可能 由于BOOST 版本不同需要稍作修改

scoped_ptr 离开作用域则自动调用类析构函数或者函数delete方法

shared_ptr 使用率最高的指针 类似scoped_ptr 但是所有权可以转移

#include <iostream>
#include <vector>
#include <windows.h>
#include <boost/smart_ptr.hpp>

using namespace std;

class CHandle
{
	HANDLE hProcess;
public:
	CHandle() { hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId()); }
	~CHandle() { cout << "Enter destructor handle" << endl; if(NULL != hProcess){CloseHandle(hProcess);hProcess = NULL;} }
	void PrintHandle() {cout << hProcess << endl;}
};

int _tmain(int argc, _TCHAR* argv[])
{
	{
		boost::scoped_ptr<CHandle> sp(new CHandle);
		sp->PrintHandle();
		sp.reset(new (CHandle));
		sp->PrintHandle();
	}
	cout << endl;

	typedef boost::shared_ptr<int> SHP;
	vector<SHP> v;
	v.push_back(SHP (new int(1)));
	v.push_back(SHP (new int(2)));
	v.push_back(SHP (new int(3)));

	for(vector<SHP>::iterator it = v.begin();
		it != v.end();++it)
	{
		cout << *(*it) << endl;
	}
	cout << endl;

	boost::shared_ptr<int> i1(new int(99));
	boost::shared_ptr<int> i2(i1);
	cout << *i1 << endl;
	cout << *i2 << endl;

	i1.reset(new int(5));
	cout << *i1 << endl;
	cout << *i2 << endl;

	return 0;
}

  

再来一个示例

智能指针创建空间 保存输入的字符串

int _tmain(int argc, _TCHAR* argv[])
{
	char sz[] = "this is  test string";
	int strLen = strlen(sz) + 1;
	typedef boost::shared_ptr<char> SHPCHAR;
	SHPCHAR szp(new char[strLen]);
	strncpy(szp.get(),sz,strLen);

	cout << szp.get() <<endl;

	return 0;
}

  

boost 学习

时间: 2024-12-09 07:16:05

boost 学习的相关文章

Boost学习总结(一)VS2010环境下编译STLport和Boost

Boost简介 Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库.1998年,Beman G.Dawes(C++标准委员会成员之一)发起倡议并建立了Boost社区,目的是向C++程序员提供免费的.同行审查.可移植的高质量C++源程序库.Boost涵盖了字符串与文本处理.容器.迭代器.算法.图像处理.模板元编程.并发编程等等,使用Boost,将大大增强了C++的功能和表现力. STLport是什么? STLport是一个完全符合C++98标准的一个免费的C++标准库实现

boost学习 内嵌类型 与 any 的代码联系

本文是学习 boost源码的一些练习 参考文章来自 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba) 目录 http://blog.csdn.net/pongba/article/category/37521 检测内嵌类型 检测一个类中是否存在指定的类型 那么只需要利用模板检测输入参数 根据参数的不同 导入到不同的函数中 类似 template <typename T> void Register(T person) { Register(person, typ

Boost学习笔记(三) progress_timer

progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include <boost\timer.hpp> #include <boost\progress.hpp> #include <iostream> using namespace boost; using namespace std; int main() { boost::progress_

Boost学习笔记(二) 时间与日期

timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简单的例子 #include <boost\timer.hpp> #include <iostream> using namespace boost; using namespace std; int main() { timer t; //声明一个计时器,开始计时 cout<&l

boost学习2.4:progress_diplay

progress_diplay可以在显示台显示程序执行的进度. (1)类摘要 #include "stdafx.h" #include<stdlib.h> #include <vector> #include<iostream> #include <fstream> #include <boost/progress.hpp> using namespace std; using namespace boost; class p

C++ Boost 学习资源列表

文档书籍下载 Boost Documentation Boost代码下载       优秀网站导航 Boost官方网站 Boost中文站 Boost Consulting     专题资源报告 Linux伊甸园- STL/boost专区 CSDN-Boost系列专题       个人博客推荐 C++有价值blog索引 <Learning boost>系列文章 C++罗浮宫-C++0x C++罗浮宫-boost源码剖析 roger007专栏-Boost C/C++多平台编程-Boost技术 吴尔

boost学习2.6:data_time库(2,处理日期)

(1)处理年月日,格里高历,位与命名空间boost::gregorian #define BOOST_DATE_TIME_SOURCE #include <boost/date_time/gregorian/gregorian.hpp> using namespace boost::gregorian; (2)创建日期对象 #define BOOST_DATE_TIME_SOURCE #include <boost/date_time/gregorian/gregorian.hpp>

boost学习2.3:progress_timer类(继承自timer)

progress_timer类 (1)是继承自timer,会在析构时自动输出时间(而不用手动调用elapsed函数) (2)progress_timer位于命名空间boost中,还有需要包含文件progress.hpp          #include <boost/progress.hpp>         using namespace boost; (3)progress_timer继承了timer的全部能力.          可以用花括号在一个程序中进行多次计时操作.{...}{.

Boost学习之可移植路径操作--filesystem

Boost.Filesystem 库为对路径.文件和目录进行查询和操作提供了可移植的工具,已经被C++标准委员会接纳包含到TR2中. 编译 使用Boost.Filesystem 库之前要先编译它,请参考<Boost的编译> 头文件 #include <boost/filesystem.hpp> 所有Boost.Filesystem库的内容都处于名空间boost::filesystem之内. 认识basic_path类 在Boost.Filesystem库里basic_path是最重