C++ resize function
resize is a function in vector used toresize's function, which can change the number of elements in the container.
resize is a member function of the container class, used toadjusts the container's size to the specified number of elements. If the new size is greater than the current size, new elements are added; if it is smaller, excess elements are removed.
resize provides the ability to flexibly resize the container, and is one of vector's powerful dynamic features.
Word meaning: resize means "resize," i.e., to change the number of elements in the container.
Basic syntax and parameters
resize is a member function of the container class; it can specify the new number of elements.
Syntax format
void resize(size_type count);
void resize(size_type count, const T& value);
Parameter Description
Parameters: count
- Type:
size_type(unsigned integer type, usuallysize_t) - Description: the container's new number of elements.
Parameters(optional): value
- Type: Same as the container element type
- Description: the default value used to fill new elements when elements need to be added. If not specified, value initialization is used by default (0 for built-in types).
Function description
- Return Value:
void(no return value). - effect: Adjusts the container's
size()to count. If elements need to be added, fill with value; if elements need to be removed, discard the excess elements. - Note:
resizechanges the container'ssize(), and may also changecapacity().
Example
Let us thoroughly master resize through a series of examples from simple to complex.
Example 1: Basic usage - enlarge the container
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
};
std
::
cout
<<
"Initial state - size: "
<<
numbers.
size
(
)
<<
", capacity: "
<<
numbers.
capacity
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Enlarge the container to 10 elements
numbers.
resize
(
10
)
;
std
::
cout
<<
"\nAfter resize(10) - size: "
<<
numbers.
size
(
)
<<
", capacity: "
<<
numbers.
capacity
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
初始状态 - size: 3, capacity: 3
元素: 1 2 3
resize(10) 后 - size: 10, capacity: 10
元素: 1 2 3 0 0 0 0 0 0 0
Code analysis:
- Initially has 3 elements: 1, 2, 3.
resize(10)adjusts the size to 10.- The 7 newly added elements are value-initialized to 0.
Example 2: Fill new elements with a specified value
can be done during resize by specifying a fill value.
Example
#include
#include
#include
int
main
(
)
{
std
::
vector
<
std
::
string
names
=
{
"Alice"
,
"Bob"
}
;
std
::
cout
<<
"Initial state - size: "
<<
names.
size
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
const
auto
&
n
:
names
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Fill new elements with "Unknown"
names.
resize
(
5
,
"Unknown"
)
;
std
::
cout
<<
"\nAfter resize(5, "Unknown") - size: "
<<
names.
size
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
const
auto
&
n
:
names
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
初始状态 - size: 2
元素: Alice Bob
resize(5, "Unknown") 后 - size: 5
元素: Alice Bob Unknown Unknown Unknown
Code analysis:
- Use
resize(5, "Unknown")adjusts the size to 5. - The 3 newly added elements are all filled with "Unknown".
Example 3: Use resize to shrink the container
resize can also be used to shrink the container.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
};
std
::
cout
<<
"Initial state - size: "
<<
numbers.
size
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Shrink to 5 elements
numbers.
resize
(
5
)
;
std
::
cout
<<
"\nAfter resize(5) - size: "
<<
numbers.
size
(
)
<<
std
::
endl
;
std
::
cout
<<
"Elements: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
初始状态 - size: 10
元素: 1 2 3 4 5 6 7 8 9 10
resize(5) 后 - size: 5
元素: 1 2 3 4 5
Code analysis:
resize(5)adjusts the size to 5.- The last 5 elements are removed.
Example 4: resize and capacity
resize will change at the same time size and capacity.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
v;
// Pre-allocate space
v.
reserve
(
100
)
;
v.
push_back
(
1
)
;
v.
push_back
(
2
)
;
std
::
cout
<<
"Initial - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
// resize to a larger value
v.
resize
(
50
)
;
std
::
cout
<<
"After resize(50) - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
// resize to a smaller value
v.
resize
(
10
)
;
std
::
cout
<<
"After resize(10) - size: "
<<
v.
size
(
)
<<
", capacity: "
<<
v.
capacity
(
)
<<
std
::
endl
;
return
0
;
}
Expected output:
初始 - size: 2, capacity: 100
resize(50) 后 - size: 50, capacity: 100
resize(10) 后 - size: 10, capacity: 100
Code analysis:
- When
resizeWhen 's value is less than the current capacity,capacityremains unchanged. - When
resizeWhen 's value is greater than the current capacity,capacityincreases accordingly.
Example 5: Use resize to create a fixed-size container
resize can be used to create a fixed-size container.
Example
#include
#include
int
main
(
)
{
// Create a vector containing 100 -1s
std
::
vector
<
int
scores
(
100
,
1
);
std
::
cout
<<
"Size: "
<<
scores.
size
(
)
<<
std
::
endl
;
std
::
cout
<<
"First 10 elements: "
;
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
std
::
cout
<<
scores
[
i
]
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Set the value at a certain position
scores
[
0
]
=
100
;
scores
[
1
]
=
95
;
std
::
cout
<<
"First 10 elements after modification: "
;
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
std
::
cout
<<
scores
[
i
]
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Code analysis:
- Use
resizecan create a container containing a fixed number of elements. - This is useful when a fixed-size container needs to be pre-allocated.
The C++ container class