0

クラス内にコールバックを格納するためにデータ構造を使用している場合、起動時にベクトルと予約を使用する必要があるか、この場合は単に両端キューを使用する必要があるかを議論しています。は不明ですが、15 前後と比較的小さくなります。これらの 2 つのシナリオで、毎回割り当てることと、クラスで前もって予約するためにヒットを取得することのトレードオフは何だと思います。

#ifndef __Pricer_hpp__
#define __Pricer_hpp__

#include <utility>
#include <vector>

class Pricer
{
  public: 
    typedef  void (*callback_fn)(double price, void *subscription);

    Pricer(): _initialSubscriberNum(15), _callbacks() { _callbacks.reserve(_initialSubscriberNum); }  // is it better to use a deuqe and remove the need for the reserve?

    void attach (const callback_fn& fn, void *subscription )
    {
      _callbacks.emplace_back(fn,subscription); // is emplace_back better than using push_back with std::pair construction which I am assuming would do a move version of push_back?  
    }

    void broadcast(double price)
    {
      for ( auto callback : _callbacks)
      {
        (*callback.first)(price, callback.second);
      }
    }

  private:
    typedef std::pair<callback_fn, void *> _callback_t;
    const unsigned int _initialSubscriberNum;
    std::vector<_callback_t> _callbacks; // should this be a deque?
};

#endif 
4

1 に答える 1

4

一般的な提案は、ベクターを使用してから、パフォーマンスの問題がある場合 (あなたの場合はおそらく起こらないでしょう)、他のデータ構造に変更することです。

時々害を及ぼす可能性がある「時期尚早の最適化」について覚えておいてください。

push_backemplace_back :リンクはこちら

于 2013-07-09T06:02:17.953 に答える