首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

boost_shared_ptr放入set跟vector中

2012-09-09 
boost_shared_ptr放入set和vector中shared_ptr放入set和vector中#include vector #include set #inclu

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;}


热点排行