Using STL Allocator with STL Vectors

被刻印的时光 ゝ 提交于 2019-11-30 15:51:55

You have to use the default allocator just as the function expects. You have two different types, and there's no way around that.

Just call reserve prior to operating on the vector to get the memory allocations out of the way.

Think about the bad things that could happen. That function may take your vector and start adding more elements. Soon, you could over-flow the stack space you've allocated; oops!

If you're really concerned about performance, a much better route is to replace operator new and kin with a custom memory manager. I have done so and allocations can be hugely improved. For me, allocating sizes of size 512 or less is about 4 operations (move a couple pointers around); I used a pool allocator)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!