boost 学习(1)
智能指针的学习
中文教程网站 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 filsystem 库练习
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <windows.h>
#include <boost/smart_ptr.hpp>
#include <boost/function.hpp>
#include <boost/signal.hpp>
#include <string>
#include <boost/thread.hpp>
#include <stdlib.h> #define BOOST_FILESYSTEM_VERSION 2
#include <boost/filesystem.hpp> using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
boost::filesystem::path p("C:\\Windows\\System");
std::cout << p.root_name() << std::endl;
std::cout << p.root_directory() << std::endl;
std::cout << p.root_path() << std::endl;
std::cout << p.relative_path() << std::endl;
std::cout << p.parent_path() << std::endl;
std::cout << p.filename() << std::endl; boost::filesystem::path p1("photo.jpg");
std::cout << p1.stem() << std::endl;
std::cout << p1.extension() << std::endl; for (boost::filesystem::path::iterator it = p.begin(); it != p.end(); ++it)
std::cout << *it << std::endl; boost::filesystem::path p2("C:\\");
try
{
boost::filesystem::file_status s = boost::filesystem::status(p2);
std::cout << boost::filesystem::is_directory(s) << std::endl;
}
catch (boost::filesystem::filesystem_error &e)
{
std::cerr << e.what() << std::endl;
} return 0;
}
根据muduo开源库作者陈硕的一些文章。对于多线程下C++编程提出了一些观点。主要是多线程下对象的销毁比较困难,但是由于多线程下,mutext是无法保护析构的。而后提出了智能指针的方案并对使用该指针会遇到的困难和陷阱予以说明并提出解决方案。
该作者博客
http://www.cppblog.com/Solstice/
这里主要说说shared_ptr,采用计数方案,计数为零则自动删除对象,不必自己调用delete。可以使用unique()及user_count()判断该指针是否唯一指针获取者,以及指针计数。
示例如下:
#include <iostream>
#include <memory>
#include <string>
#include <set>
#include <map>
#include <boost/smart_ptr.hpp>
#include <assert.h> using std::set;
using std::string;
using std::cout;
using std::endl;
using std::map;
using std::pair; int main()
{
boost::shared_ptr<int> num(new int(77)); cout << "num: " << *num << endl; cout << "use_count: " << num.use_count() << endl; assert( num.unique()); cout << "is unique() " << num.unique() << endl; boost::shared_ptr<int> num2 = num; cout << "use_count: " << num.use_count() << endl;
cout << "use_count2: " << num2.use_count() << endl; boost::shared_ptr<int> spi = boost::make_shared<int>(78);
cout << endl;
cout << "make_shared " << *spi << endl; return 0;
}
与auto_ptr比较 , shared_ptr可以在容器中使用
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
#include <vector> using std::string;
using std::cout;
using std::endl;
using std::vector;
using boost::shared_ptr;
using boost::make_shared; int main()
{
typedef vector<shared_ptr<int> > v_sp;
v_sp v(3); int i = 0;
for(v_sp::iterator pos = v.begin(); pos != v.end(); ++pos)
{
(*pos) = make_shared<int>(++i);
} for(v_sp::iterator pos = v.begin(); pos != v.end(); ++pos)
{
cout << "value: " << *(*pos) << endl;
cout << "use_count: " << (*pos).use_count() << endl;
cout << endl;
} cout << endl; shared_ptr<int> p(v[1]);
cout << "value: " << *p << endl;
cout << "use_count: " << p.use_count() << endl; // 此刻有两个shared_ptr指向该值 return 0;
}
创建智能指针的时候 可以指定释放函数
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <windows.h> using namespace std; HRESULT MyCloseHandle(HANDLE hHandle )
{
cout << "Enter CloseHandle func" << endl;
return CloseHandle(hHandle);
} int main()
{
cout << "Start create shared_ptr for handle " << endl;
boost::shared_ptr<void> h(OpenProcess(PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId()), MyCloseHandle);
cout << "Create shared_ptr for handle finish" << endl;
SetPriorityClass(h.get(), HIGH_PRIORITY_CLASS);
}
示例: 线程库的基本用法 线程和线程组 以及JOIN函数
#include <iostream>
#include <string>
#include <assert.h>
#include <vector>
#include <algorithm> #include <boost/thread.hpp>
#include <boost/chrono.hpp> using namespace std;
using namespace boost; void print(int n)
{
cout << "hello world: " << n << endl;
} void print1(int n)
{
cout << "hello world : " << n << endl;
}
void print2(int n)
{
cout << "hello world : " << n << endl;
}
void print3(int n)
{
cout << "hello world : " << n << endl;
} int _tmain(int argc, _TCHAR* argv[])
{
thread t(print,);
t.join(); thread_group thread_grp;
//===================================================
thread_grp.create_thread(bind(print1,));
thread_grp.create_thread(bind(print2,));
thread_grp.create_thread(bind(print3,)); thread_grp.join_all();
//=================================================== return ;
}
boost 学习(1)的更多相关文章
- Boost学习-Linuxidc上的很好的学习资料
来自 http://www.linuxidc.com/Linux/2011-07/39215.htm,拷贝第一页如下 Boost学习系列 简介及基本用法 [日期:2011-07-25] 来源:Linu ...
- boost 学习笔记 2: timer
boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...
- boost 学习笔记 0: 安装环境
boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...
- BOOST学习笔记
BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...
- Boost学习总结(一)VS2010环境下编译STLport和Boost
Boost学习总结(一)VS2010环境下编译STLport和Boost Boost简介 Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库.1998年,Beman G.Da ...
- boost学习 内嵌类型检测 与 any 的代码练习
本文是学习 boost源码的一些练习 参考文章来自 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba) 目录 http://blog.csdn.net/pongba/ar ...
- Boost学习之语法解析器--Spirit
Boost.Spirit能使我们轻松地编写出一个简单脚本的语法解析器,它巧妙利用了元编程并重载了大量的C++操作符使得我们能够在C++里直接使用类似EBNF的语法构造出一个完整的语法解析器(同时也把C ...
- Ada boost学习
http://blog.csdn.net/dark_scope/article/details/14103983 据说在Deep Learning出来之前,SVM和Adaboost是效果最好的 两个算 ...
- boost学习笔记(七)---date_time库
date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...
随机推荐
- HTML网页Table解析
procedure TForm27.Button1Click(Sender: TObject); var doc2: IHTMLDocument2; doc3: IHTMLDocument3; ita ...
- Mysql 主键常用修改
修改表的定增长初始值: ALTER TABLE 表名 AUTO_INCREMENT=值;
- PuTTY免输密码自动登录Linux
1.使用PuTTY安装目录里的puttygen.exe工具.先点“生成(Generate)”,然后随意移动鼠标直到进度条填满,即可生成密钥 公钥部分:把上边那一段文字全选->复制备用.(不要点击 ...
- 基于OpenGL编写一个简易的2D渲染框架-08 重构渲染器-整体架构
事实上,前面编写的渲染器 Renderer 非常简陋,虽然能够进行一些简单的渲染,但是它并不能满足我们的要求. 当渲染粒子系统时,需要开启混合模式,但渲染其他顶点时却不需要开启混合模式.所以同时渲染粒 ...
- UI5-文档-4.31-Routing and Navigation
到目前为止,我们已经把所有的应用程序内容放在一个页面上.随着我们添加越来越多的特性,我们希望将内容拆分并将其放在不同的页面上. 在这一步中,我们将使用SAPUI5导航特性加载并显示一个单独的详细信息页 ...
- UI5-文档-4.7-JSON Model
现在我们已经设置了视图和控制器,现在是时候考虑MVC中的M了. 我们将在app中添加一个输入字段,将它的值绑定到模型上,并将相同的值绑定到输入字段的描述上.描述将根据用户类型直接更新. Preview ...
- 迷你MVVM框架 avalonjs 学习教程15、属性监听与模块通信
avalon的ViewModel对象从其内部EventManager里继承了三个方法,$watch.$unwatch.$fire三个方法,它们就是我们本节的主题. 词如其名,非常直白,一看就知道做什么 ...
- docker 入門
http://dockone.io/article/277 我的碎碎念:Docker入门指南 [编者的话]之前曾经翻译过很多Docker入门介绍的文章,之所以再翻译这篇,是因为Anders的角度很独特 ...
- dubbo hessian+dubbo协议
Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况 Hessian协议用于集成Hessian的服务,Hessian底层采 ...
- Centos 安装Mongo DB
NOSQL在很短的时间里使用人数据高涨,这不仅是它提出的一种新存储思想,更是因为它在对大数据做操作的效率,明显高于关系数据库 工具/原料 接入Internet的一台Centos计算机 下载安装文件 ...