C++ capacity function
capacity is a function in vector used toquery capacity; it returns the size of the currently allocated storage space of the container.
capacity is a member function of the container class, used toreturns the number of elements that the container's currently allocated storage space can hold. This value is usually greater than or equal to size().
capacity reflects the pre-allocated memory space inside the vector; understanding it helps optimize program performance and avoid unnecessary memory reallocation.
Word meaning: capacity means "capacity," i.e., the number of elements the container can hold.
Basic syntax and parameters
capacity is a member function of the container class; calling it requires no parameters.
Syntax format
size_type capacity() const;
Parameter Description
- Parameters: No parameters
capacitytakes no arguments.
Function description
- Return Value: Returns
size_typetype (usuallysize_t), indicates the number of elements that the container's currently allocated storage space can hold. - effect: Returns a non-negative integer representing the allocated memory space of the container.
- Note:
capacity()returns the capacity, not the number of elements. The number of elements is obtained withsize().
Example
Let us thoroughly master capacity through a series of examples from simple to complex.
Example 1: Basic usage - view capacity
Example
#include
#include
int
main
(
)
{
// Empty vector
std
::
vector
<
int
emptyVec;
std
::
cout
<<
"Empty vector - size: "
<<
emptyVec.
size
(
)
<<
", capacity: "
<<
emptyVec.
capacity
(
)
<<
std
::
endl
;
// Vector with elements
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
};
std
::
cout
<<
"5-element vector - size: "
<<
numbers.
size
(
)
<<
", capacity: "
<<
numbers.
capacity
(
)
<<
std
::
endl
;
return
0
;
}
Expected output:
空 vector - size: 0, capacity: 0
5 元素 vector - size: 5, capacity: 5
Code analysis:
- An empty vector's
capacity()is 0. - A vector with 5 elements,
capacity()is at least 5 (may be larger, depending on the implementation).
Example 2: The relationship between size and capacity
Understand size and capacity 's difference is the basis for optimizing vector usage.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
v;
// Gradually add elements and observe capacity changes
std
::
cout
<<
"Changes in size and capacity during element addition:"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
15
;
++
i
)
{
v.
push_back
(
i
)
;
std
::
cout
<<
"Added "
<<
(
i
1
)
<<
" elements - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
}
return
0
;
}
Expected output (typical):
添加元素过程中 size 和 capacity 的变化:
添加 1 个元素后 - size: 1, capacity: 1
添加 2 个元素后 - size: 2, capacity: 2
添加 3 个元素后 - size: 3, capacity: 4
添加 4 个元素后 - size: 4, capacity: 4
添加 5 个元素后 - size: 5, capacity: 8
添加 6 个元素后 - size: 6, capacity: 8
添加 7 个元素后 - size: 7, capacity: 8
添加 8 个元素后 - size: 8, capacity: 8
添加 9 个元素后 - size: 9, capacity: 16
添加 10 个元素后 - size: 10, capacity: 16
添加 11 个元素后 - size: 11, capacity: 16
添加 12 个元素后 - size: 12, capacity: 16
添加 13 个元素后 - size: 13, capacity: 16
添加 14 个元素后 - size: 14, capacity: 16
添加 15 个元素后 - size: 15, capacity: 16
Code analysis:
- The vector's capacity grows exponentially (usually doubling).
- When capacity is insufficient, the vector allocates a larger memory space (usually twice the current capacity).
- This strategy is to reduce the number of memory reallocations and improve performance.
Example 3: Use reserve to pre-allocate capacity
Use reserve can pre-allocate sufficient capacity to avoid frequent memory reallocations.
Example
#include
#include
int
main
(
)
{
// Pre-allocate capacity for 1000 elements
std
::
vector
<
int
v;
v.
reserve
(
1000
)
;
std
::
cout
<<
"After reserve(1000) - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
// Add 100 elements
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
v.
push_back
(
i
)
;
}
std
::
cout
<<
"After adding 100 elements - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
return
0
;
}
Expected output:
reserve(1000) 后 - size: 0, capacity: 1000
添加 100 个元素后 - size: 100, capacity: 1000
Code analysis:
reserve(1000)pre-allocated memory space for 1000 elements.- After adding 100 elements,
capacityis still 1000, and no memory reallocation occurred. - This can significantly improve performance, especially when a large number of elements need to be added.
Example 4: shrink_to_fit releases excess capacity
C++11 introduced the shrink_to_fit method can adjust the capacity to be the same as size.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
v;
// Add 5 elements
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
v.
push_back
(
i
)
;
}
std
::
cout
<<
"Initial state - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
// Reserve more space
v.
reserve
(
100
)
;
std
::
cout
<<
"After reserve(100) - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
// Shrink to the appropriate size
v.
shrink_to_fit
(
)
;
std
::
cout
<<
"After shrink_to_fit() - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
return
0
;
}
Expected output:
初始状态 - size: 5, capacity: 5
reserve(100) 后 - size: 5, capacity: 100
shrink_to_fit() 后 - size: 5, capacity: 5
Code analysis:
shrink_to_fit()requests the container to reduce capacity to the same as size.- This can release no-longer-needed memory.
- Note: this is not mandatory; the actual capacity depends on the implementation.
Example 5: Practical application of capacity
Understanding capacity can help optimize memory usage.
Example
#include
#include
void
printStatus
(
const
std
::
string
&
operation,
const
std
::
vector
<
int
&
v
)
{
std::
cout
<<
operation
<<
" - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
}
int
main
(
)
{
std
::
vector
<
int
data;
printStatus
(
"Initial state"
, data
)
;
// If we know we need to store 1000 elements, we can pre-allocate
const
size_t
estimatedSize
=
1000
;
data.
reserve
(
estimatedSize
)
;
printStatus
(
"After reserve(1000)"
, data
)
;
// Simulate data loading
for
(
size_t
i
=
0
;
i
<
1000
;
++
i
)
{
data.
push_back
(
static_cast
<
int
(
i
)
);
}
printStatus
(
"After adding 1000 elements"
, data
)
;
// Check whether expansion is needed
if
(
data.
capacity
(
)
<
estimatedSize
)
{
std::
cout
<<
"Warning: insufficient capacity!"
<<
std
::
endl
;
}
else
{
std
::
cout
<<
"Sufficient capacity, minimizing memory allocation count"
<<
std
::
endl
;
}
return
0
;
}
Expected output:
初始状态 - size: 0, capacity: 0
reserve(1000) 后 - size: 0, capacity: 1000
添加 1000 个元素后 - size: 1000, capacity: 1000
容量充足,内存分配次数最少化
Code analysis:
- Pre-estimate the required capacity and use
reserveto avoid multiple memory allocations. - This is especially useful for scenarios processing large amounts of data.
The C++ container class