读书人

boost_shared_ptr放入set跟vector中

发布时间: 2012-09-09 09:27:54 作者: rapoo

boost_shared_ptr放入set和vector中

shared_ptr放入set和vector中

#include <vector> #include <set> #include <iostream> #include <algorithm> #include <boost/shared_ptr.hpp>  using namespace std; struct Foo{     Foo(int _x) : x(_x){}     ~Foo()     {         cout<<"~Foo()"<<endl;     }     int x; }; typedef boost::shared_ptr<Foo> FooPtr;  struct FooPtrOps{     void operator() ( const FooPtr & a )     {         cout<< a->x <<endl;    }  bool operator()( const FooPtr & a, const FooPtr & b )    { return a->x > b->x; }};int main(){    vector<FooPtr>  foo_vector;    set<FooPtr, FooPtrOps> foo_set;    FooPtr foo_ptr(new Foo(2));    foo_vector.push_back(foo_ptr);    foo_set.insert(foo_ptr);        foo_ptr.reset(new Foo(1));    foo_vector.push_back(foo_ptr);    foo_set.insert(foo_ptr);        foo_ptr.reset(new Foo(3));    foo_vector.push_back(foo_ptr);    foo_set.insert(foo_ptr);        foo_ptr.reset(new Foo(2));    foo_vector.push_back(foo_ptr);    foo_set.insert(foo_ptr);            foo_ptr.reset(new Foo(4));    foo_vector.push_back(foo_ptr);    foo_set.insert(foo_ptr);        cout<<"foo_vector"<<endl;    for_each(foo_vector.begin(), foo_vector.end(), FooPtrOps() );    cout<<"foo_set"<<endl;    for_each(foo_set.begin(), foo_set.end(), FooPtrOps() );    return 0;}


读书人网 >操作系统

热点推荐