boost::ptr_vector
2016-07-24 14:50
405 查看
boost::ptr_vector
这是我在学习陈硕muduo网络库过程中记录的关于C++的知识碎片,对muduo感兴趣的同学可以加入QQ群365972535一起讨论:boost::ptr_vector 官网例子
代码/home/chenzw/jmuduo/tests/ptr_vector.cc
#include <boost/ptr_container/ptr_vector.hpp> #include <boost/shared_ptr.hpp> //#include <boost/lambda/lambda.hpp> #include <iostream> //#include <algorithm> using namespace std; // // A simple polymorphic class // class Poly { int i_; static int cnt_; public: Poly() : i_( ++cnt_ ) { cout << " construct cnt_ = " << cnt_ << endl;} virtual ~Poly() { cout << " destroy i_ = " << i_ << endl; } void foo() { doFoo(); } private: virtual void doFoo() { cout << " i_ = " << i_ << endl; } public: friend inline bool operator>( const Poly& l, const Poly r ) { return l.i_ > r.i_; } }; int Poly::cnt_ = 0; // // Normally we need something like this to compare pointers to objects // template< typename T > struct sptr_greater { bool operator()( const boost::shared_ptr<T>& l, const boost::shared_ptr<T>& r ) const { return *l > *r; } }; // // one doesn't need to introduce new names or live with long ones // typedef boost::shared_ptr<Poly> PolyPtr; void simple_test1() { enum { size = 5 }; typedef vector<PolyPtr> vector_t; //typedef boost::ptr_vector<Poly> ptr_vector_t; // 用 vector<boost::shared_ptr<Poly>> svec 存储智能指针,来管理对象 vector_t svec; //ptr_vector_t pvec; for( int i = 0; i < size; ++i ) { svec.push_back( PolyPtr( new Poly ) );//push对象指针前,要强制类型转换 // pvec.push_back( new Poly ); // no extra syntax } cout << "---------------------------------" << endl; for( int i = 0; i < size; ++i ) { svec[i]->foo(); //手动间接访问 // pvec[i].foo(); // automatic indirection svec[i] = PolyPtr( new Poly );//原来的实例被覆盖,会自动析构 // pvec.replace( i, new Poly ); // direct pointer assignment not possible, original element is deleted } for( vector_t::iterator i = svec.begin(); i != svec.end(); ++i ) (*i)->foo(); //显得都有些麻烦 // for( ptr_vector_t::iterator i = pvec.begin(); i != pvec.end(); ++i ) // i->foo(); // automatic indirection } void simple_test2() { enum { size = 5 }; // typedef vector<PolyPtr> vector_t; typedef boost::ptr_vector<Poly> ptr_vector_t; // vector_t svec; // 用 boost::ptr_vector<Poly> pvec 指针容器管理对象 ptr_vector_t pvec; for( int i = 0; i < size; ++i ) { // svec.push_back( PolyPtr( new Poly ) ); pvec.push_back( new Poly ); // 不需要强制类型转换 } cout << "---------------------------------" << endl; for( int i = 0; i < size; ++i ) { // svec[i]->foo(); pvec[i].foo(); // automatic indirection // svec[i] = PolyPtr( new Poly ); // pvec[i] = new Poly;// error pvec.replace( i, new Poly ); //原来的对象被覆盖,自动析构 } // for( vector_t::iterator i = svec.begin(); i != svec.end(); ++i ) // (*i)->foo(); for( ptr_vector_t::iterator i = pvec.begin(); i != pvec.end(); ++i ) i->foo(); // 自动间接访问 }//函数运行结束,ptr_vector中对象自动析构 /* #include <boost/test/unit_test.hpp> using boost::unit_test::test_suite; test_suite* init_unit_test_suite( int argc, char* argv[] ) { test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" ); test->add( BOOST_TEST_CASE( &simple_test ) ); return test; } */ int main() { cout << < 9e4d span class="hljs-string">"----------------case1----------------------" << endl; simple_test1(); cout << "----------------case2----------------------" << endl; simple_test2(); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- boost相关小知识(长期顶置更新)
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析