您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  boost c++