Overhead to using std::vector?

前端 未结 7 1185
星月不相逢
星月不相逢 2021-02-13 04:09

I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector?

To give a crude e

7条回答
  •  长情又很酷
    2021-02-13 04:49

    You should try to avoid C-style-arrays in C++ whenever possible. The STL provides containers which usually suffice for every need. Just imagine reallocation for an array or deleting elements out of its middle. The container shields you from handling this, while you would have to take care of it for yourself, and if you haven't done this a hundred times it is quite error-prone.
    An exception is of course, if you are adressing low-level-issues which might not be able to cope with STL-containers.

    There have already been some discussion about this topic. See here on SO.

提交回复
热议问题