Hi all,
This topic follows a discussion on StackOverflow:
http://stackoverflow.com/questions/10674289/is-there-an-equivalent-of-vectorreserve-for-an-stdlist
I thought this would be a better place to delve into the details of allocation.
My problem is the following: I need an efficient sequence container that supports insertions and deletions at any place, while preserving the validity of previously referenced pointers during these operations, and I don't need to be able to iterate both ways on the stored elements, nor to access elements by their index; forward_list seems to be the best candidate for that. The order of the size is typically a hundred thousand elements, and I am concerned about memory reallocations as I initialize my list before actually using it in my program. Indeed, refering to the discussion on StackOverlow:
(EitanT) Everything works, except that profiling shows that the operator new is invoked too much due to list::push_back() |
Efficiency is of great concern to me, and using a dequeue instead does not seem like a good solution:
(Reference) For operations that involve frequent insertion or removals of elements at positions other than the beginning or the end, deques perform worse and have less consistent iterators and references than lists. |
The resize function, as well as the corresponding constructor overload, do not perform the desired action, since they actually append elements to the list:
(Reference) Notice that this function changes the actual content of the container by inserting or erasing elements from it. |
Finally, the suggestion of Eliott -- using forward_list::get_allocator().allocate() -- seems good to me, but hasn't received any vote, and to be honest I am not sure what it does exactly, because this command is supposed to return a pointer to the newly allocated memory, and not to provide a memory pool for the list to use (
http://www.cplusplus.com/reference/std/memory/allocator/allocate/).
How can I reproduce the behavior of a "reserve" function for a forward_list?
Thanks in advance!