unique_ptr or vector?

后端 未结 5 745
孤城傲影
孤城傲影 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:50

    std::vector stores the length of both the size of the variable and the size of the allocated data along with the pointer to the data it's self. std::unique_ptr just stores the pointer so there may be a small gain in using std::unique_ptr.

    No one has yet mentioned the vector provides iterators and function such and size() where as unique ptr does not. So if iterators are needed use std::vector

提交回复
热议问题