std::container - preallocation size?

G

Gernot Frisch

Hi,

can I tell e.g. a queue that it should re-allocate 512 elements each
time it comes to it's boundaries?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
P

Peter van Merkerk

Gernot said:
can I tell e.g. a queue that it should re-allocate 512 elements each
time it comes to it's boundaries?

For some containers like std::vector() you could use the reserve()
function to implement this behavior yourself. As far as I know there is
no standard way to change the reallocation behaviour of containers.
 
T

tom_usenet

Hi,

can I tell e.g. a queue that it should re-allocate 512 elements each
time it comes to it's boundaries?

By default, queue uses std::deque as its storage. std::deque can't be
configured in how it behaves, but under VC++7.1 it is very time
efficient for queues since the segment map is implemented as a
circular buffer.

Tom
 
J

John Harrison

Gernot Frisch said:
Hi,

can I tell e.g. a queue that it should re-allocate 512 elements each
time it comes to it's boundaries?

std::queue is just a wrapper around a STL sequence. It will have the same
reallocation strategy as the underlying sequence does.

If you want a particular reallocation strategy and none of the standard STL
sequences provide it, then you must code your own sequence that reallocates
in the way that you want, and then use that as a parameter to std::queue.

john
 
G

Gernot Frisch

std::queue is just a wrapper around a STL sequence. It will have the same
reallocation strategy as the underlying sequence does.

If you want a particular reallocation strategy and none of the standard STL
sequences provide it, then you must code your own sequence that reallocates
in the way that you want, and then use that as a parameter to
std::queue.

The second template argument? How would I do that? Can you give an
example?
 
J

John Harrison

Gernot Frisch said:
std::queue.

The second template argument? How would I do that? Can you give an
example?

std::queue<int> my_queue; // std::deque is the default
std::queue<int, std::vector<int> > my_queue_2; // use std::vector instead
std::queue<int, MyContainer<int> > my_queue_3; // user defined class
instead

In each case std::queue is just a wrapper around another class, you can
specify which class by using the second template argument.

I forget exactly what methods MyContainer would have to have in order to be
successfully wrapped by std::queue, but it's the usual stuff, push_back,
pop_front, size, empty etc. etc. The point is that you can use whatever
reallocation strategy you like in MyContainer.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top