Using vector as a buffer without initializing it on resize()

前端 未结 5 878
执笔经年
执笔经年 2020-11-29 07:01

I want to use vector as a buffer. The interface is perfect for my needs, but there\'s a performance penalty when resizing it beyond its current size

5条回答
  •  盖世英雄少女心
    2020-11-29 08:06

    There's nothing in the standard library that meets your requirements, and nothing I know of in boost either.

    There are three reasonable options I can think of:

    • Stick with std::vector for now, leave a comment in the code and come back to it if this ever causes a bottleneck in your application.
    • Use a custom allocator with empty construct/destroy methods - and hope your optimiser will be smart enough to remove any calls to them.
    • Create a wrapper around a a dynamically allocated array, implementing only the minimal functionality that you require.

提交回复
热议问题