Function std::函数的向量

Function std::函数的向量,function,vector,c++11,visual-c++-2010,Function,Vector,C++11,Visual C++ 2010,我有以下资料: typedef std::function<void(const EventArgs&)> event_type; class Event : boost::noncopyable { private: typedef std::vector<event_type> EventVector; typedef EventVector::const_iterator EventVector_cit; EventVec

我有以下资料:

  typedef std::function<void(const EventArgs&)> event_type;

  class Event : boost::noncopyable
  {
  private:
   typedef std::vector<event_type> EventVector;
   typedef EventVector::const_iterator EventVector_cit;
   EventVector m_Events;

  public:
   Event()
   {
   }; // eo ctor

   Event(Event&& _rhs) : m_Events(std::move(_rhs.m_Events))
   {
   }; // eo mtor

   // operators
   Event& operator += (const event_type& _ev)
   {
    assert(std::find(m_Events.begin(), m_Events.end(), _ev) == m_Events.end());
    m_Events.push_back(_ev);
    return *this;
   }; // eo +=

   Event& operator -= (const event_type& _ev)
   {
    EventVector_cit cit(std::find(m_Events.begin(), m_Events.end(), _ev));
    assert(cit != m_Events.end());
    m_Events.erase(cit);
    return *this;
   }; // eo -=
  }; // eo class Event

现在,我知道这是因为向量和操作符
=
中存储了什么。在STL容器中存储
std::function
还有其他方法吗?我需要用别的东西来包装它吗?

你可以在向量中存储
boost::function
,只要你不使用
std::find
。因为您似乎需要这样做,所以在自己的类中用相等的形式包装函数可能是最好的

class EventFun
{
  int id_;
  boost::function<...> f_;
public:
  ...
  bool operator==(const EventFun& o) const { return id_==o.id_; } // you get it...
};
class事件乐趣
{
int-id_2;;
boost::函数f;
公众:
...
bool操作符==(const EventFun&o)const{return id_==o.id_;}//您得到它了。。。
};
请注意,这要求您以合理的方式维护
id
(例如,两个不同的
EventFun
s将具有不同的
id
s等)


另一种可能是使用一个标记存储
boost::function
s,客户端将记住该标记,并在删除该标记时使用该标记来标识特定的函数。

谢谢。我想我主要关心的是如何动态生成该ID。我想要使用
+=
将函数/lambda传入事件向量的可读性。所以我猜包装器类需要根据传递的函数生成ID。也许是它的地址?
class EventFun
{
  int id_;
  boost::function<...> f_;
public:
  ...
  bool operator==(const EventFun& o) const { return id_==o.id_; } // you get it...
};