boost 学习(1)
智能指针的学习
中文教程网站 http://zh.highscore.de/cpp/boost/
不过代码可能 由于BOOST 版本不同需要稍作修改
scoped_ptr 离开作用域则自动调用类析构函数或者函数delete方法
shared_ptr 使用率最高的指针 类似scoped_ptr 但是所有权可以转移
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #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; } |
再来一个示例
智能指针创建空间 保存输入的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | // 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,7); t.join(); thread_group thread_grp; //=================================================== thread_grp.create_thread(bind(print1,12)); thread_grp.create_thread(bind(print2,22)); thread_grp.create_thread(bind(print3,32)); thread_grp.join_all(); //=================================================== return 0; }
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话