【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式
boost_array_bind_fun_ref
Array.cpp
#include<boost/array.hpp>
#include <iostream>
#include <string> using namespace std; using namespace boost; void mainA ()
{ array <int, 5> barray = { 1, 2, 3, 4, 5 };
barray[0] = 10;
barray.at(4) = 20;
int *p = barray.data();//存储数组的指针
for (int i = 0; i < barray.size();i++)
{
cout << barray[i] << " " << p[i] << endl;
} array<string, 3> cmd = { "calc", "notepad", "tasklist" }; cin.get();
}
Bind.cpp
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <functional> using namespace std; using namespace boost; //绑定函数的默认值,继承二进制函数类的全部类容
class add:public std::binary_function<int ,int,void>
{
public:
void operator()(int i,int j) const
{
std::cout << i + j << endl;
}
}; void add(int i, int j)
{
std::cout << i + j << endl;
} void mainB()
{
vector<int> myv;
myv.push_back(11);
myv.push_back(23);
myv.push_back(34); //for_each(myv.begin(), myv.end(), bind1st(add(),10));
for_each(myv.begin(), myv.end(), bind(add, 13, _1)); //bind设置默认參数调用,函数副本机制。不能拷贝构造 cin.get();
}
Fun.cpp
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <vector>
#include <algorithm>
#include <functional>
#include <stdlib.h> using namespace std; using namespace boost; void mainC()
{
//atoi //char * to int
boost::function<int(char *)> fun = atoi;
cout << fun("123") + fun("234") << endl;
fun = strlen;
cout << fun("123") + fun("234") << endl; cin.get();
} void mainD()
{
boost::function<int(char *)> fun = atoi;
cout << fun("123") + fun("234") << endl;
fun = boost::bind(strcmp, "ABC", _1);
cout << fun("123") << endl;
cout << fun("ABC") << endl; cin.get();
} class manager
{
public:
void allstart()
{
for (int i = 0; i < 10;i++)
{
if (workid)
{
workid(i);
}
}
}
void setcallback(boost::function<void(int)> newid)//绑定调用
{
workid = newid;
}
public:
boost::function<void(int)> workid;
}; class worker
{
public:
void run(int toid)
{
id = toid;
cout << id << "工作" << endl;
}
public:
int id;
}; void mainE()
{
manager m;
worker w;
//类的成员函数须要对象来调用,绑定了一个默认的对象
m.setcallback(boost::bind(&worker::run, &w, _1)); m.allstart(); cin.get();
}
Ref.cpp
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <vector>
#include <algorithm>
#include <functional>
#include <stdlib.h> using namespace std; using namespace boost; void print(std::ostream &os,int i)
{
os << i << endl;
} void mainF()
{
//不能够拷贝的对象能够用ref
boost::function<void(int)> pt = boost::bind(print,boost::ref(cout), _1);
vector<int > v;
v.push_back(11);
v.push_back(12);
v.push_back(13);
for_each(v.begin(), v.end(), pt); std::cin.get();
}
boost智能指针
RAII原理.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <string> using namespace std; class mystr
{
public:
char *p = nullptr;
public:
mystr(const char *str)
{
cout << "构建" << endl;
int length = strlen(str);
p = new char[length + 1];
strcpy(p, str);
p[length] = '\0';
}
~mystr()
{
cout << "销毁" << endl;
delete[] p;
}
}; void go()
{
char *p = new char[100];
mystr str1 = "ABCD";//RAII避免内存泄漏,普通情况下,堆上的内存当作栈上来使用
//栈内存有限。希望自己主动释放,用非常大的内存。
} void mainHG()
{
go(); cin.get();
}
Smartpointer原理.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <stdlib.h> using namespace std; template<class T>
class pmy
{
public:
pmy()
{
}
pmy(T *t)
{
p = t;
}
~pmy()
{
if (p!=nullptr)
{
delete p;
}
}
T operator *()
{
return *p;
}
private:
T *p=nullptr;
}; class Test
{
public:
Test()
{
cout << "Test create" << endl;
}
~Test()
{
cout << "Test delete" << endl;
}
}; void run()
{
pmy<Test> p(new Test);//智能指针,智能释放
//*p;
} void mainH()
{
run(); cin.get();
}
Smartpointer.cpp
#include <iostream>
#include <vector>
#include<algorithm>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include<boost/weak_ptr.hpp>
#include <windows.h> using namespace std; void mainI()
{
boost::scoped_ptr<int> p(new int);//自己主动释放内存
*p = 12;
cout << *p.get() << endl;
p.reset(new int);
*p.get() = 3;
boost::scoped_ptr<int> pA(nullptr);//独占内存
//pA = p; cout << *p.get() << endl;
cin.get();
} void mainG()
{
boost::scoped_array<int> p(new int[10]);//自己主动释放内存
//boost::scoped_array<int> pA(p);独享指针
*p.get() = 1;
p[3] = 2;
p.reset(new int[5]);//仅仅能指针 cin.get();
} void show(boost::shared_ptr<int> p)
{
cout << *p << endl;
} void mainK()
{
vector<boost::shared_ptr<int> > v;
boost::shared_ptr<int> p1(new int(11));
boost::shared_ptr<int> p2(new int(12));
boost::shared_ptr<int> p3(p2);//拷贝
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
for_each(v.begin(), v.end(), show); cin.get();
} class runclass
{
public:
int i = 0;
public:
runclass(int num) :i(num)
{
cout << "i create" <<i<< endl;
}
runclass()
{
cout << "i create" << i << endl;
}
~runclass()
{
cout << "i delete" <<i<< endl;
}
void print()
{
cout << "i =" << i<<endl;
}
}; void testfun()
{
boost::shared_ptr<runclass> p1(new runclass(10));
boost::shared_ptr<runclass> p2(p1);
boost::shared_ptr<runclass> p3(p1);
p1.reset(new runclass(12));
p1->print();
p2->print();
p3->print();
} void testfunarray()
{
boost::shared_array<runclass> p1(new runclass[5]);
boost::shared_array<runclass> p2(p1);
} void mainL()
{
//testfun();
testfunarray(); cin.get();
} DWORD WINAPI reset(LPVOID p)
{
boost::shared_ptr<int > *sh = static_cast<boost::shared_ptr<int > *> (p);
sh->reset();//指针的重置,释放内存
std::cout << "指针运行释放" << endl;
return 0;
} DWORD WINAPI print(LPVOID p)
{
boost::weak_ptr<int > * pw = static_cast<boost::weak_ptr<int > *>(p);
boost::shared_ptr<int > sh = pw->lock();//锁定不能够释放
Sleep(5000);
if (sh)
{
std::cout << *sh << endl;
}
else
{
std::cout << "指针已经被释放" << endl;
} return 0;
} void main123()
{
boost::shared_ptr<int> sh(new int(99));
boost::weak_ptr<int > pw(sh);
HANDLE threads[2];
threads[0] = CreateThread(0, 0, reset, &sh, 0, 0);//创建一个线程
threads[1] = CreateThread(0, 0, print, &pw, 0, 0);
Sleep(1000); WaitForMultipleObjects(2, threads, TRUE, INFINITE);//等待线程结束 cin.get();
}
boost多线程锁定
Thread.cpp
#include <iostream>
#include <vector>
#include<algorithm>
#include<boost/thread.hpp>
#include <windows.h> using namespace std;
using namespace boost; void wait(int sec)
{
boost::this_thread::sleep(boost::posix_time::seconds(sec));
} void threadA()
{
for (int i = 0; i < 10;i++)
{
wait(1);
std::cout << i << endl;
}
} void threadB()
{
try
{
for (int i = 0; i < 10; i++)
{
wait(1);
std::cout << i << endl;
}
}
catch (boost::thread_interrupted &)
{ }
} void mainO()
{
boost::thread t(threadA );
wait(3);
//t.interrupt();
t.join(); cin.get();
}
哈希库
Unorderred.cpp
#include <iostream>
#include<boost/unordered_set.hpp>
#include<string> using namespace std; void mainAAAC()
{
boost::unordered_set<std::string> myhashset;
myhashset.insert("ABC");
myhashset.insert("ABCA");
myhashset.insert("ABCAG"); for (auto ib = myhashset.begin(); ib != myhashset.end();ib++)
{
cout << *ib << endl;
}
std::cout << (myhashset.find("ABCA1") != myhashset.end()) << endl; cin.get();
}
正則表達式
Regex.cpp
#include <boost/regex.hpp>
#include <locale>
#include <iostream>
#include <string> using namespace std; void mainA123()
{
std::locale::global(std::locale("English"));
string str = "chinaen8Glish";
boost::regex expr("\\w+\\d\\u\\w+");//d代表数字,
//匹配就是1,不匹配就是0
cout << boost::regex_match(str, expr) << endl; cin.get();
} void mainB123()
{
//std::locale::global(std::locale("English"));
string str = "chinaen8Glish9abv";
boost::regex expr("(\\w+)\\d(\\w+)");//d代表数字,
boost::smatch what;
if (boost::regex_search(str,what,expr))//依照表达式检索
{
cout << what[0] << endl;
cout << what[1] << endl;
}
else
{
cout << "检索失败";
}
cin.get();
} void mainC1234()
{
string str = "chinaen8 Glish9abv";
boost::regex expr("\\d");//d代表数字,
string kongge = "______";
std::cout << boost::regex_replace(str, expr, kongge) << endl; cin.get();
}
【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式的更多相关文章
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- [5] 智能指针boost::shared_ptr
[1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
- Boost智能指针使用总结
内存管理是一个比较繁琐的问题,C++中有两个实现方案: 垃圾回收机制和智能指针.垃圾回收机制因为性能等原因不被C++的大佬们推崇, 而智能指针被认为是解决C++内存问题的最优方案. 1. 智能指针定义 ...
- Boost智能指针——weak_ptr
循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引用的对象.一个简单的例子如下: #include <string>#include <iost ...
- Boost智能指针-基础知识
简单介绍 内存管理一直是 C++ 一个比較繁琐的问题,而智能指针却能够非常好的解决问题,在初始化时就已经预定了删除.排解了后顾之忧.1998年修订的第一版C++标准仅仅提供了一种智能指针:std::a ...
- C++ 智能指针 boost::scoped_ptr分析
1.scoped_ptr的实现原理及特性 特性:scoped_ptr和auto_ptr类似,但最大的区别就是不能转让管理权限,也就是说scoped_ptr禁止用户进行拷贝和赋值 实现原理:如何才能禁止 ...
- [4] 智能指针boost::scoped_ptr
[1]boost::scoped_ptr简介 boost::scoped_ptr属于boost库,定义在namespace boost中,包含头文件#include <boost/scoped_ ...
- 关于智能指针boost::shared_ptr
boost库中的智能指针shared_ptr, 功能强大, 且开销小,故受到广大coder的欢迎. 但在实际的使用过程中,笔者也发现了一些不足. 1.定制的删除器 shared_ptr除了可以使用默认 ...
随机推荐
- mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件
Mybatis的逆向工程非常简单,只要一个配置文件和一个Main方法就可以实现,下面以maven工程为例: (1)在pom.xml中引入依赖包 <dependency> <group ...
- 说说Python程序的执行过程
1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在.如果是解释型语言, ...
- 【Java】异常类处理层次
异常处理简介 异常在java的开发中可能没有那么被重视.一般遇到异常,直接上抛,或者随便catch一下处理之后对于程序整体运行也没有什么大的影响.不过在企业级设计开发中,异常的设计与处理的好坏,往往就 ...
- scikit-learn学习之贝叶斯分类算法
版权声明:<—— 用心写好你的每一篇文章,转载请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] ================= ...
- AAPT err(Facade for): libpng error: Not a PNG file 错误解决
在导入项目到Android studio后,若编译出现“AAPT err(Facade for): libpng error: Not a PNG file”错误. 该错误表示项目中的drawable ...
- iOS - UITableView加载网络图片 cell适应图片高度
使用xib创建自定制cell 显示图片 创建一个继承UITableViewCell的类 勾选xib 如下是xib创建图 xib 向.h拖拽一个关联线 .h .m 2.代码创建(使用三方适配 ...
- 基于jQuery和CSS3炫酷图片3D旋转幻灯片特效
在线预览 源码下载 iPresenter是一款效果非常炫酷的jQuery和CSS3 3D旋转幻灯片特效插件.你可以使用它来制作产品展示.图片画廊或者各种幻灯片和轮播图特效.这款幻灯片插件的特点有: ...
- iis下php 500错误
很不想用iis,然而客户不想增加机器,只好按客户的意思了.可是没想到发送短信以在本地 机器上是好的,在iis下直接500. 一开始以为是防火墙问题,后来检查了一下没有,再后来换了一个短信接口, ...
- WebAPI 消息处理器
由上图可以看出消息处理器的使用场合和使用方法. 使用场合: HttpServer 得到请求时. public static class WebApiConfig { public static voi ...
- gcc命令中参数c和o混合使用的详解
gcc -c a.c 编译成目标文件a.o gcc -o a a.o 生成执行文件a.exe gcc a.c 生成执行文件a.exe gcc -o a -c a.c 编译成目标文件a gcc -o a ...