Why can't I wrap a T* in an std::vector?

后端 未结 3 2073
清歌不尽
清歌不尽 2020-12-11 21:55

I have a T* addressing a buffer with len elements of type T. I need this data in the form of an std::vector, for

3条回答
  •  时光取名叫无心
    2020-12-11 22:20

    The short answer is that a vector can't use your buffer because it wasn't designed that way.

    It makes sense, too. If a vector doesn't allocate its own memory, how does it resize the buffer when more items are added? It allocates a new buffer, but what does it do with the old one? Same applies to moving - if the vector doesn't control its own buffer, how can it give control of this buffer to another instance?

提交回复
热议问题