The vector class. Methods that define and modify the general characteristics of array
The theme is a continuation of the theme:
Contents
- 1. The size() method. Determine the size of the vector
- 2. Method max_size(). Maximum allowed array size
- 3. Method capacity(). Determine the size of the array taking into account the reserved memory
- 4. Method empty(). Determine if the vector is empty
- 5. Method shrink_to_fit(). Set the size of the array in memory by the number of elements in it without additional memory reservation
- 6. Method resize(). Resize array
- 7. Method reserve(). Reserve additional memory for array items
- Related topics
Search other resources:
1. The size() method. Determine the size of the vector
The current number of elements in a dynamic vector array can be determined using the size() method. The syntax for declaring the size() method is as follows
size_t vector<T>::size() const
here T – the type of the array items.
Example.
// Some class class MyClass { // ... }; ... // Method size() // 1. Create a vector of 5 int numbers and get its size vector<int> A1(5); int n1 = A1.size(); // n1 = 5 // 2. Create a vector of 10 instances of type MyClass and get its size vector<MyClass> A2(10); int n2 = A2.size(); // n2 = 10 // 3. Create an empty vector of type bool and get its size vector<bool>* A3 = new vector<bool>(); // create via pointer int n3 = A3->size(); // n3 = 0 ...
⇑
2. Method max_size(). Maximum allowed array size
The max_size() method allows you to get the maximum allowed number of array elements. This value depends on the type of the elements of the vector array. It is different for different types.
This value can fluctuate depending on the data type of the vector array. The larger the data size of each element in the vector array, the smaller the max_size() value.
The syntax for declaring the max_size() method is as follows:
size_t vector<T>::max_size() const
here T – the type of the elements of the dynamic array.
Example.
... // Some class class Complex { public: double re; double im; // ... }; ... // The max_size() method // 1. Create a vector of 5 int numbers and get its maximum allowable size vector<int> A1(5); int max1 = A1.max_size(); // max1 = 1073741823 // 2. Create a vector of 10 instances of the Complex type // and get its maximum allowable size vector<Complex> A2(10); int max2 = A2.max_size(); // max2 = 268435455 // 3. Create an empty vector of type bool and get its maximum allowed size vector<bool>* A3 = new vector<bool>(); // create via pointer int max3 = A3->max_size(); // max3 = 2147483647 cout << max3 << endl; ...
⇑
3. Method capacity(). Determine the size of the array taking into account the reserved memory
The capacity() method returns the number of elements allocated for the array.
When adding a new element to the array, you need to allocate additional memory for 1 more element. However, the compiler can increase the memory size by several elements so that when one more element is added later, it does not re-execute such operations as freeing memory for a previously created array, allocating a new fragment, and the like. Thus, performance is accelerated.
The syntax for declaring a method is as follows
size_t vector<T>::capacity<T> const
here T – the type of the array elements.
Example. The example demonstrates the difference between the size() and capacity() methods.
#include <iostream> #include <vector> using namespace std; void main() { // Method capacity() // Create an array of 10 floats vector<float> A1(10); // Print the real number of array elements int capacity = A1.capacity(); int size = A1.size(); cout << "A1.capacity() = " << capacity << endl; cout << "A1.size() = " << size << endl; // Add 1 element to the end of the array A1.push_back(5); cout << "+1 item" << endl; // Print new values of the number of elements in the array capacity = A1.capacity(); size = A1.size(); cout << "A1.capacity() = " << capacity << endl; // 15 cout << "A1.size() = " << size << endl; // 11 }
Program result
A1.capacity() = 10 A1.size() = 10 +1 item A1.capacity() = 15 A1.size() = 11
As you can see from the result, when adding a new element, the number of elements in the array increased by 1 and is 11 (size() method). However, the size of the array itself has increased by 5 elements and is 15 (method capacity()). Now you can add 4 more elements without unnecessary reallocation of memory. If the addition of elements will be performed in a loop, then this approach will speed up the execution of the program.
⇑
4. Method empty(). Determine if the vector is empty
Using the empty() method, you can determine if the array is empty (the number of elements in the array is 0). The general form of the empty() method is as follows:
bool vector<T> empty() const
here T – the type of the elements of the array.
Example.
#include <iostream> #include <vector> using namespace std; void main() { // The empty() method - determine if the vector is empty vector<double> A(5); // Create an array of 5 elements // Checking if the array is empty if (A.empty()) cout << "Vector A is empty." << endl; else cout << "Vector A is not empty." << endl; // Clear the array A.clear(); // Check again if the array is empty if (A.empty()) cout << "Vector A is empty." << endl; else cout << "Vector A is not empty." << endl; }
Program result
Vector A is not empty. Vector A is empty.
⇑
5. Method shrink_to_fit(). Set the size of the array in memory by the number of elements in it without additional memory reservation
The shrink_to_fit() method allows you to align the memory allocated (reserved) for array elements (capacity() method) with the memory occupied by array elements (size() method). The number of reserved elements (for which memory is allocated) is returned by the capacity() method. The number of elements defined in the array is returned by the size() method. The value returned by the capacity() method is always greater than or equal to the value returned by the size() method. If the value obtained by the capacity() method is greater than the value obtained by the size() method, then the shrink_to_fit() method allows these values to be aligned. This reduces the size of the memory returned by the capacity() method.
The method is effective when, as a result of various operations with the array, there is a large excess of reserved elements in the array.
The general form of the method is as follows
void shrink_to_fit()
After calling this method, the size() and capacity() methods will always return the same values.
Example.
#include <iostream> #include <vector> using namespace std; void main() { // Method shrink_to_fit() - change the size of an array // 1. Create array of integers based on initialization list initializer_list<int> L = { 1, 2, 3, 4, 5 }; vector<int> A1(L); // 2. Print the number of elements for which memory is already allocated cout << "Allocated size = " << A1.capacity() << endl; // 5 // 3. Display the current number of elements cout << "Size = " << A1.size() << endl; // 5 // 4. Add 1 element to the array A1.push_back(6); cout << "+1 item" << endl; // 5. Once again, print the allocated number of items in the array // and the current number of elements. cout << "Allocated size = " << A1.capacity() << endl; // 7 cout << "Size = " << A1.size() << endl; // 6 // 6. Align the allocated count of items with the current count A1.shrink_to_fit(); cout << "shrink_to_fit()" << endl; // 7. Re-display the allocated and actual number of items cout << "Allocated size = " << A1.capacity() << endl; // 6 cout << "Size = " << A1.size() << endl; // 6 }
Program result
Allocated size = 5 Size = 5 +1 item Allocated size = 7 Size = 6 shrink_to_fit() Allocated size = 6 Size = 6
As you can see from the result, after adding an element to the array, the size of the array increased from 5 to 6. This is natural and the size() method showed it. But the real size of the array increased from 5 to 7, as shown by the capacity() method. That is, 1 more element was allocated.
The call to the shrink_to_fit() method reallocated memory so that the amount of memory allocated for the elements became equal to the amount of memory occupied by the elements.
⇑
6. Method resize(). Resize array
The resize() method allows you to resize a dynamic array up or down. The method has two overloaded implementations.
The first implementation has the following declaration
void resize(const size_t _NewSize)
here
- _NewSize – the new size of the array. If _NewSize is greater than the current size of the array, then all other elements are padded with zero values.
The second implementation has the following declaration
void resize(const size_t _NewSize, const T& Val)
here
- _NewSize – new size of the array;
- T – type of array elements;
- Val – the values that are added to the elements of the array if the _NewSize value is greater than the current size.
Example.
... // Method resize() - resize the array // 1. Create array of integers based on initialization list initializer_list<int> L = { 1, 2, 3, 4, 5 }; vector<int> A1(L); // 2. Resize array A1 A1.resize(8); // A1 = { 1, 2, 3, 4, 5, 0, 0, 0 } // 3. Decrease the size of array A1 by filling with 10 values A1.resize(6, 10); // A1 = { 1, 2, 3, 4, 5, 0 } // 4. Increase the size of array A1, filling with 15 values A1.resize(9, 15); // A1 = { 1, 2, 3, 4, 5, 0, 15, 15, 15 } ...
⇑
7. Method reserve(). Reserve additional memory for array items
The reserve() method allows you to allocate (reserve) memory for array elements, which is returned by the capacity() method. The actual number of elements in the array, which is returned by the size() method, does not change. This is done by reducing the number of operations associated with memory reallocation.
The method declarations are the following
void reserve(size_t Newcapacity)
here
- Newcapacity – the new size of the array, taking into account the reserved elements.
Example.
#include <iostream> #include <vector> using namespace std; void main() { // Method reserve() - reserve extra memory for the array // 1. Declare an array of 5 floats vector<float> A(5); // 2. Print the number of elements in the array and the reserved amount cout << A.size() << endl; // 5 cout << A.capacity() << endl; // 5 // 3. Reserve extra memory for the array A.reserve(12); // 4. Print the number of array elements and the reserved space for array elements cout << A.size() << endl; // 5 cout << A.capacity() << endl; // 12 }
Program result
5 5 5 12
⇑
Related topics
- General information about the class vector. Methods overview. Creating a dynamic array. Ways to access vector elements
- Methods that modify data in the array. Methods push_back(), pop_back(), clear(), swap(), operator=(), erase(), insert(), assign()
- Methods that provide access to array elements. Methods at(), front(), back(), data(), begin(), end(), cbegin(), cend(), rbegin(), rend(), crbegin(), crend()
⇑