美文网首页
boost - 指针容器

boost - 指针容器

作者: 呆呆的张先生 | 来源:发表于2018-08-12 23:34 被阅读22次

    ptr_vector指针向量

    基本概念

    Boost学习之指针容器

    • 基本概念: Boost::ptr_vector属于Boost库中 pointer contiainer 的其中一个,提供了与标准容器相似的行为,但是维护的是堆分派的对象。
    • 使用场景:当需要一个包含指针的容器时,如存放一些不可拷贝的对象,或想在容器中存放基类以实现多态,可以直接使用标准容器存放指针,但是清空或删除容器时,不易保证异常安全回收资源,另外可以使用存放 shared_ptr 的标准容器。
    // file        : demo1.cpp
    // description : show the base usage of ptr_vector
    
    #include <iostream>
    #include <string>
    #include <boost/ptr_container/ptr_vector.hpp>
    
    using namespace std;
    using namespace boost;
    
    class Animal{
        public:
        explicit Animal( const string& name): name_( name ) {}
        virtual ~Animal(){
            cout << "destructor Animal" << endl; }
        virtual void Bark(){
            cout << "Animal " << name_ << " brak" << endl; }
        const string& name() { return name_; } 
        private:
        string name_;
    };
    
    class Cat : public Animal{
        public:
        explicit Cat( const string& name ) : Animal( name ) {}
        virtual void Bark(){
            cout << "Cat " << name() << " brak" << endl; }
        virtual ~Cat(){
            cout << "destructor Cat" << endl;
        }
    };
    
    class Dog : public Animal{
        public:
        explicit Dog( const string& name ) : Animal( name ) {}
        virtual void Bark(){
            cout << "Dog " << name() << " brak" << endl; }
        virtual ~Dog(){
            cout << "destructor Dog" << endl;        
        }
    };
    
    // 1, 采用 vector<Base *> 将基类指针存放于标准容器
    void test1()
    {
        vector<Animal* > pData;               // 1,包含基类指针的标准容器
        pData.push_back( new Animal( "a" ) ); 
        pData.push_back( new Cat( "c" ) ); 
        pData.push_back( new Dog( "d" ) );
    
        for( auto iter = pData.begin(); 
             iter != pData.end();
             ++iter )
        { (*iter)->Bark(); } 
    
        for( auto iter = pData.begin(); 
             iter != pData.end();
             ++iter )
        { delete *iter; }                     // 2,需要手工释放资源,无法保证异常安全性 
    
    }
    
    // 2, 采用 vector< shared_ptr<Base> > 将智能指针存放于标准容器
    void test2()
    {
        vector< shared_ptr<Animal> > pData;                          // 1,包含智能指针的标准容器
        pData.push_back( shared_ptr<Animal>( new Animal( "a" ) ) );  // 2,需要显式创建智能指针
        pData.push_back( shared_ptr<Animal>( new Cat( "c" ) ) ); 
        pData.push_back( shared_ptr<Animal>( new Dog( "d" ) ) );
    
        for( auto iter = pData.begin(); 
             iter != pData.end();
             ++iter )
        { (*iter)->Bark(); }                                 
                                                                    // 3, 内存自动释放
    }
    
    // 3, 采用 ptr_vector 指针容器
    void test3()
    {
        ptr_vector<Animal> pData;              // 1,基类指针的容器
        pData.push_back( new Animal( "a" ) );  // 2, 支持与标准容器类似的操作 vector.push_back
        pData.push_back( new Cat( "c" ) );     // 3, 基类指针支持多态
        pData.push_back( new Dog( "d" ) );
    
        for( auto iter = pData.begin();        // 2, 支持与标准容器类似的操作 iterator 
             iter != pData.end();
             ++iter )
        { iter->Bark(); }                      // 3, 基类指针支持多态
    }
    g++ -o demo demo1.cpp -std=c++11
    int main()
    {
        test1();
        test2();
        test3();
    }
    

    相关文章

      网友评论

          本文标题:boost - 指针容器

          本文链接:https://www.haomeiwen.com/subject/twwebftx.html