On 28 May 2004 07:47:54 -0700,
ti****@yahoo.com (Tino) wrote:
In using std::priority_queue, I'm concerned about the expense of
memory allocation and copying as the priority_queue grows large.
std::vector has reserve() to address this concern, though there
doesn't seem to be anything like this for priority_queue. Also, I
don't know anything about how priority_queue is implemented
(specifically, MSVC++ 6.0), so maybe I don't have anything to worry
about. Guidance, please?
Regards,
Ryan
std::vector guarantees that the memory it uses is contiguous so when
the allocated chunk of memory needs to be expanded a new chunk of
memory large enough for the whole vector needs to be allocated and all
the contents of the vector copied into the new memory before the old
memory is released.
The other containers do not have to be contiguous so they can leave
the old contents where they are and just allocate a new chunk of
memory large enough to hold the new object being added to the
container. They do not have to copy the existing contents since these
are left in the old memory.
Hence they do not need a reserve function like vector. Reserve is
useful when you know the size of the vector in advance and can
allocate all the memory needed in one chunk at the beginning and so
avoid having to copy large pieces of the vector.
rossum
--
The Ultimate Truth is that there is no Ultimate Truth