Pre-allocate space for C++ STL queue

前端 未结 6 2262
小鲜肉
小鲜肉 2020-12-24 01:50

I\'m writing a radix sort algorithm using queues and I would like to have a STL queue allocate space before I start adding things to the queue so that I can avoid constant d

6条回答
  •  情书的邮戳
    2020-12-24 02:30

    It sounds like you need a data structure with a reserve() method, and efficient "push" and "pop" operations from opposite ends. How about a ring buffer, wrapped around a std::vector ? You could reserve() the space you need in the constructor, then maintain "front" and "end" indices in your implementation to translate "push" and "pop" operations in the public interface to O(1) operations on the underlying std::vector.

提交回复
热议问题