9.4 how vector objects grow
In order to support fast random access, the physical storage mode of vector object is continuous storage, and because vector is dynamic size, it involves a problem.
If the storage space allocated by the current vector container is full and no new elements can be added, then a block of memory space needs to be reallocated to copy the original value and add new elements.
But if you reallocate the memory space every time you add, the efficiency of vector will be very low.
Therefore, in order to avoid this inefficient way, vector has its own memory growth mode. It should be noted that the strategies of standard library implementers with different growth modes are different.
In general, vector s and string s usually allocate more memory space than the new space requirement, which is how many elements the container currently needs to store.
The member functions related to capacity are as follows:
1. It was said before that the number of elements that the container can actually hold is usually greater than or equal to the current demand, that is, the container will have extra space. Using shrink_to_fit() can reclaim the extra space, but this depends on the implementer of the standard library, and the implementer has the right not to reclaim the extra space.
2.capacity, which indicates the maximum number of elements that a container can hold without reallocating memory space.
3.reserve, change the capacity of the container. It should be noted that the memory space allocated by reserve(n) is less than or equal to capacity. When n is less than the number of elements currently stored in the container, reserve does not work. That is to say, n of reserve is only a reference. When n < size(), reserve will not work. When n > size(), capacity() > = n
The difference between capacity() and size()
The former represents the maximum number of elements that can be accommodated without reallocating memory space.
The latter represents how many elements are currently contained.
The difference between resize() and reserve()
resize() changes the size of size(), but if resize(n),n is greater than capacity(), it will change capacity(),reserve() changes the size of capacity(), reserve (n), n < = capacity()
Only when n in insert (push back is also insert),seize==capacity(), or resize(n),reverse(n) is greater than capacity(), vector will reallocate memory space
Practice
9.35
capacity indicates how many elements vec can hold at most without reallocating memory space
size() indicates how many elements vec currently holds
9.36
capacity() always > = size()
9.37
This list is related to the storage interface of array. List is not continuously stored in memory, so when inserting new elements, new memory space can be allocated directly. The memory of array is fixed, so there is no way to open up new storage space. size () is its maximum capacity.
9.38
VS2017 standard library
vector<int> vec; cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; vec.push_back(1); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; for (int i = 0; i < 10;i++) { vec.push_back(i); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; } vec.reserve(20); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; for (int i = 0; i < 50; i++) { vec.push_back(i); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; }
Program output:
size:0capacity:0 size:1capacity:1 size:2capacity:2 size:3capacity:3 size:4capacity:4 size:5capacity:6 size:6capacity:6 size:7capacity:9 size:8capacity:9 size:9capacity:9 size:10capacity:13 size:11capacity:13 size:11capacity:20 size:12capacity:20 size:13capacity:20 size:14capacity:20 size:15capacity:20 size:16capacity:20 size:17capacity:20 size:18capacity:20 size:19capacity:20 size:20capacity:20 size:21capacity:30 size:22capacity:30 size:23capacity:30 size:24capacity:30 size:25capacity:30 size:26capacity:30 size:27capacity:30 size:28capacity:30 size:29capacity:30 size:30capacity:30 size:31capacity:45 size:32capacity:45 size:33capacity:45 size:34capacity:45 size:35capacity:45 size:36capacity:45 size:37capacity:45 size:38capacity:45 size:39capacity:45 size:40capacity:45 size:41capacity:45 size:42capacity:45 size:43capacity:45 size:44capacity:45 size:45capacity:45 size:46capacity:67 size:47capacity:67 size:48capacity:67 size:49capacity:67 size:50capacity:67 size:51capacity:67 size:52capacity:67 size:53capacity:67 size:54capacity:67 size:55capacity:67 size:56capacity:67 size:57capacity:67 size:58capacity:67 size:59capacity:67 size:60capacity:67 size:61capacity:67
It can be seen that when the size() is less than 10, the container's size() and capacity () are the same. When the size() is greater than 10, the capacity () > = size() is allocated later. When the capacity()==size(), the memory space to be reallocated is larger than the increment of the previously reallocated memory space.
9.39
Add elements to the svec, and after adding, the size of the vec will be 1.5 times of the original size of resize().
9.40
Because reserve(n) and capacity may be greater than or equal to N, the actual results are uncertain and need to be determined according to the specific compiler.
Assuming that after reserve(n), capacity is equal to N, then
Read in 256 words, capacity is 1024
Read 512 words, capacity is 2014,
Later, it depends on how the specific standard library is implemented
Under VS2017
1000, capacity 1536
1048, capacity 2304
The code used is as follows
vector<string> vec; vec.reserve(1024); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl; int count = 1048; for (int i = 0; i < count;++i) { vec.push_back("233"); } vec.resize(vec.size()+vec.size()/2); cout << "size:" << vec.size() << "capacity:" << vec.capacity() << endl;