unique_ptr or vector?

后端 未结 5 718
孤城傲影
孤城傲影 2020-12-16 14:23

If you don\'t need dynamic growth and don\'t know the size of the buffer at compile time, when should unique_ptr be used instead of vector<

5条回答
  •  抹茶落季
    2020-12-16 14:51

    Objective Part:

    No, there probably shouldn't be a significant performance difference between the two (though I suppose it depends on the implementation and you should measure if it's critical).

    Subjective Part:

    std::vector is going to give you a well known interface with .size() and .at() and iterators, which will play nicely with all sorts of other code. Using std::unique_ptr gives you a more primitive interface and makes you keep track of details (like the size) separately. Therefore, barring other constraints, I would prefer std::vector.

提交回复
热议问题