我正在争论哪个会更快,如果我使用数据结构在类中存储回调,我应该使用向量并在启动时保留,还是在这种情况下我应该只使用双端队列,因为订阅者的总数不知道,但会相对较小,比如 15 岁左右。我想在这两种情况下每次分配与在我的班级中预先保留命中率之间的权衡是什么。
#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