Summary: This article describes a very common situation: When you store data in a cache, you often need to adjust the size of the cache at runtime so that more data can be accommodated. This article will discuss how to use STL's VECTOR for memory redistribution.
What is described here is a very common situation: When you store data in a cache, you often need to adjust the size of the cache at runtime so that you can accommodate more data. Traditional memory redistribution technology is very cumbersome, and it is easy to go wrong: in the C language, it is generally called Realloc every time you need to expand the cache. The situation is worse in C , you can't even reapply the memory in the array assigned to the New operation in the function. You must not only assign itself, but also copy the data in the original cache to the new destination cache, then release the previous array cache. This article will provide a safe, easy and automated C memory redistribution technique for this issue - even with STL's Vector.
Replace the built-in array to save the acquired data with the STL Vector object to save the acquired data, which is safe and easy, and is automated.
Further problem analysis
Before proposing a solution, I will give a specific example to illustrate C to reassign the malfunction and complexity of memory. Suppose you have an catalog application that reads the ISBNs entered by the user, then insert it into an array until the user enters 0. If the user is inserted more than the capacity of the array, then you must add its size accordingly:
#include
INT main () {int size = 2; // Initialization of the array size; adjusts at runtime. INT * P = new int [size]; int isbn; for (int N = 0; N) {cout << "Enter an isbn; press 0 to stop"; cin >> ISBN; if (ISBN == 0) BREAK; if (n == size) // Does the array reach the upper limit? Reallocate (p, size); p [n] = ISBN; // Insert the element into the extended array} delete [] p; // Don't forget This step!}
Note how cumbersome processes are inserted into the array of data described above. Each time it is repeated, the loop must check if the cache reaches the upper limit. If so, the program calls the user-defined function reallocate (), which is implemented as follows:
#include
INTRLOCATE (INT * & P, INT & SIZE) {size * = 2; // Double THE ARRAY ''s Size with Each Reallocation Int * Temp = New Int Int (Size]; std :: copy (p, p (size / 2 ), TEMP); delete [] p; // Release Original, Smaller Buffer P = Temp; // Reassign P to the newly allocated buffer}
Reallocate () Use the STL std :: copy () algorithm to make reasonable expansion of the cache - double each time the expansion is doubled. This approach avoids pre-allocating excess memory, and reduces memory that needs to be reassigned from quantity. This technology needs to be fully tested and debugged, especially when the scholars are implemented. In addition, Reallocate () is not universal, which can only handle the situation of integer arrays. For other data types, it is in powerless, you must define additional versions of this function or template it. Fortunately, there is a more clever way to achieve. Create and optimize Vector
Each STL container has a splitter (allocator), which is a built-in memory manager that automatically reassigns the storage space of the container. Therefore, the above program can be greatly simplified and get rid of the Reallocator function.
Step 1: Create a Vector
Replace the built-in array to save the acquired data with the Vector object. The loop in main () reads ISBN, check if it is 0, if not 0, then insert the value by calling the push_back () member function
Vector: #include
INT main () {Vector
During the VECTOR object configuration, it first assigns a default cache size that is defined by it. The general VECTOR assigned data storage initial space is a 64-256 storage slot (Slots). When the VECTOR feels that the storage space is not enough, it automatically reassigns more memory. In fact, as long as you like, you can call push_back () any time, or even know where the assignment is happening again.
In order to access the Vector element, use the overloaded [] operator. The following loops displays all Vector elements on the screen:
For (int N = 0; n Step 2: Optimization In most cases, you should automatically manage your own memory, just as we do in the above program. However, in the task of paying attention to time, it is also useful to rewrite the default allocation scheme. Suppose we know that the number of ISBNs is at least 2000. Then you can point out capacity during the object construct so that the VECTOR has at least 2000 elements: Vector In addition, we can also call the resize () member function: vi.resize (2000); // Establish space for not less than 2000 elements In this way, the intermediate redistribution is avoided, thereby improving efficiency.