I saw this questio开发者_开发百科n in a book:
What member functions of the std::vector are controlling the allocated memory ?
Can someone answer this?
Thank youMemory allocations of Container classes is managed by the underlying Allocator
defined for each of them.
Almost All member functions will control
the allocated memory.
The expected answer to this is probably resize
, reserve
and clear
although the latter does not in fact control the allocated memory, it just resets the “known” size to zero without modifying the underlying buffer.
All of them. None of them. It's an implementation detail. You do not know and it does not matter.
The default constructor may or may not allocate memory.
Other constructors will allocate memory if the vector is not initially empty, and may or may not if it is empty.
operator=
, assign
, insert
, push_back
, reserve
and resize
will reallocate memory if the new size or capacity exceeds the current capacity, and will not otherwise.
The destructor will free any allocated memory.
No other member function will change the memory allocation, although some may change or invalidate the memory contents.
http://msdn.microsoft.com/en-us/library/k449z507(v=VS.100).aspx
There are limits to asking questions on this place that are answered by googling "vector member functions".
精彩评论