Vector and string of Effective STL

1. Vector and string are better than dynamically allocated arrays

  When you decide to dynamically allocate memory with new, it means:

  • Make sure there is a delete operation, otherwise it will cause a memory leak

  • Make sure to use the delete form correctly, new-delete, new[]—delete[].

  • Make sure to delete only once.

  vector and string avoid the above situation because they manage memory by themselves. When elements are added to the container, their memory grows; when a vector or string is destructed, the destructor automatically frees the memory.

【Note】:
(1) If you are dynamically allocating the array, then you may have to do more work, in order to reduce the burden, please use vector or string.

2. Use reverse to avoid unnecessary reallocation

1. STL container growth mechanism

  Vector is the most common container in STL. It occupies a piece of continuously allocated memory. From the perspective of data storage, it is very similar to an array. The difference is that the array is statically allocated space. Once the size of the space is allocated, it cannot be Changed again, and the vector is dynamically allocated space. As the elements continue to increase, it will continue to expand its capacity according to its own set of growth mechanisms.

  When the space for the new element is not enough, the vector or string will perform the following operations: 1. Allocate twice the space of the current space; 2. Copy the current element to the new space; 3. Destruct the objects in the old memory; 4. Free old memory.



2. The reverse function avoids unnecessary reallocation

  The reverse function allows you to minimize the number of reallocations, thus avoiding the overhead of reallocation and pointer/iterator invalidation.

  vector and string provide the following four functions:

  • size() tells you how many elements are in the container.

  • capacity() tells you how many elements the container can hold using the allocated memory.

  • resize() forces the container to change to a state containing n elements.

  • reverse() forces the container to make its capacity at least n, provided that n is not less than the current size.

vector<int> v3;
v3.reserve(1000);
for (int i = 0; i < 100; ++i)
{
    v.push_back(i);//在循环过程中不会发生重新分配。
}

3. Use the "swap trick" to remove excess capacity

  Unlike a deque, a vector's memory footprint will only grow, not shrink. For example, if you first allocate 10,000 bytes, and then erase the last 9,999 bytes, although there is only one valid element, the memory usage is still 10,000. All space is reclaimed when the vector is destructed. empty() is used to detect whether the container is empty, clear() can clear all elements. But even with clear(), the occupied memory space remains the same. If you need the space to shrink dynamically, consider using deque. If you have to use vector, here is a way:

class Contestant{...};
vector<Contestant> contestants;
vector<Contestant>(contestants).swap(contestants);//去除多余容量

vector<Contestant>().swap(contestants);//把容器清空
string s;
string().swap(s);//string也适用

  The swap trick is to make the vector or string leave its own scope by exchanging the function swap(), thereby forcibly releasing the memory space occupied by it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324550818&siteId=291194637