Pool内存池: 只能开辟常规内存,数据类型为int,float,double,string等。
#include <iostream> #include <boost/pool/pool.hpp> #include <boost/pool/object_pool.hpp>
using namespace std; using namespace boost;
int main(int argc, char const *argv[]) { boost::pool<> pool(sizeof(int)); int *ptr[10] = { 0 };
for (int x = 0; x < 10; x++) { ptr[x] = static_cast<int *>(pool.malloc()); if (ptr[x] == nullptr) cout << "分配空间失败" << endl; }
for (int x = 0; x < 10; x++) *ptr[x] = x;
for (int x = 0; x < 10; x++) { cout << "内存地址: " << &ptr[x] << " 数值: " << *ptr[x] << endl; }
getchar(); return 0; }
|
objectPool 内存池: 该内存池支持对结构体,对象的分配与初始化。
#include <iostream> #include <string> #include <boost/pool/pool.hpp> #include <boost/pool/object_pool.hpp>
using namespace std; using namespace boost;
struct MyStruct { public: int uuid; string uname; int uage; string usex;
MyStruct(int uuid_,string uname_,int uage_, string usex_) { uuid = uuid_; uname = uname_; uage = uage_; usex = usex_; } };
template<typename P, typename ... Args> inline typename P::element_type* construct(P& p, Args&& ... args) { typename P::element_type* mem = p.malloc(); new(mem) typename P::element_type( std::forward<Args>(args)...); return mem; }
int main(int argc, char const *argv[]) { boost::object_pool<MyStruct> object; auto ptr = object.malloc();
auto ref = construct(object, 1001, "lyshark", 24, "男"); cout << "姓名: " << ref->uname << endl; object.free(ref); object.free(ptr);
getchar(); return 0; }
|
shared_ptr 智能指针:
#include <iostream> #include <string> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
int main(int argc, char const *argv[]) { boost::shared_ptr<int> int_ptr(new int); *int_ptr = 1024; cout << "指针: " << &int_ptr << " 数值: " << *int_ptr << endl;
boost::shared_ptr<string> string_ptr(new string); *string_ptr = "hello lyshark"; cout << "指针: " << &string_ptr << " 长度: " << string_ptr->size() << endl;
boost::shared_ptr<int> shared_ptr(new int(10)); cout << "持有者: " << shared_ptr.unique() << endl;
boost::shared_ptr<int>shared_copy = shared_ptr; cout << "引用数: " << shared_ptr.use_count() << endl; shared_ptr.reset();
getchar(); return 0; }
|
#include <iostream> #include <string> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
class shared { private: boost::shared_ptr<int> ptr; public: shared(boost::shared_ptr<int> p_) :ptr(p_){} void print() { cout << "内部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl; } };
void print_func(boost::shared_ptr<int> ptr) { cout << "外部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl; }
int main(int argc, char const *argv[]) { boost::shared_ptr<int> ptr(new int(100)); shared s1(ptr), s2(ptr);
s1.print(); s2.print();
*ptr = 200; print_func(ptr);
s1.print(); s2.print();
getchar(); return 0; }
|
make_shared 工厂函数: 工厂函数常用于初始化特定的指针数据的。
#include <iostream> #include <string> #include <vector> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
int main(int argc, char const *argv[]) { boost::shared_ptr<string> string_ptr = boost::make_shared<string>("hello lyshark"); cout << "初始化字符串: " << *string_ptr << endl;
typedef std::vector<boost::shared_ptr<int>> vector_ptr; vector_ptr vec(10);
int x = 0; for (auto pos = vec.begin(); pos != vec.end(); ++pos) { (*pos) = boost::make_shared<int>(++x); cout << "输出值: " << *(*pos) << endl; }
boost::shared_ptr<int> int_ptr = vec[9]; *int_ptr = 100; cout << "修改后: " << *vec[9] << endl;
x = 0; for (auto& ptr : vec) { cout << "输出值: " << *ptr << endl; }
getchar(); return 0; }
|
shared_ptr 桥接模式: 又称为PIMPI模式,仅对外部暴漏最小的细节,内部类实现用一个shared_ptr来保存指针。
#include <iostream> #include <string> #include <vector> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
class sample { private: class impl; boost::shared_ptr<impl> ptr;
public: sample(); void print(); };
class sample::impl { public: void print() { cout << "impl print" << endl; } };
sample::sample() :ptr(new impl){}
void sample::print() { ptr->print(); }
int main(int argc, char const *argv[]) { sample lsp;
lsp.print();
getchar(); return 0; }
|
markshare 实现工厂模式
#include <iostream> #include <string> #include <vector> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
class abstract { public: virtual void f() = 0; virtual void g() = 0; protected: virtual ~abstract() = default; };
class impl :public abstract { public: impl() = default; virtual ~impl() = default;
public: virtual void f() { cout << "class impl f" << endl; }
virtual void g() { cout << "class impl g" << endl; } };
boost::shared_ptr<abstract> create() { return boost::make_shared<impl>(); }
int main(int argc, char const *argv[]) {
auto ptr = create(); ptr->f(); ptr->g();
abstract *q = ptr.get(); q->f(); q->g();
getchar(); return 0; }
|
weak_ptr : 配合shared_ptr 作用在于协助shared_ptr 像旁观者一样观察资源的使用情况。
#include <iostream> #include <string> #include <vector> #include <boost/smart_ptr.hpp>
using namespace std; using namespace boost;
int main(int argc, char const *argv[]) { boost::shared_ptr<int> ptr(new int(10)); boost::weak_ptr<int> weak(ptr);
if (!weak.expired()) { boost::shared_ptr<int> new_ptr = weak.lock(); *new_ptr = 100; }
ptr.reset(); getchar(); return 0; }
|