C++ <vector> resize function
resizeis used in vector toResizefunction that can change the number of elements in the container.
resizeis a member function of container classes, used toResize the container to the specified number of elements.If the new size is greater than the current size, new elements are added; if it is less than the current size, excess elements are removed.
resizeProvides the ability to flexibly resize the container, one of vector's powerful dynamic features.
Word Definitions: resizeMeans "resize", i.e., change the number of elements in the container.
Basic syntax and parameters
resizeIs a member function of the container class that can specify a new number of elements.
Syntax format
void resize(size_type count); void resize(size_type count, const T& value);
Parameter description
Parameter: count
- Type:
size_type(an unsigned integer type, usuallysize_t) - Description: the new number of elements in the container.
Parameter(Optional):value
- Type: same as the container's 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: the container's
size()Resize to count. If elements need to be added, fill them with value; if elements need to be removed, discard the excess elements. - Note:
resizewill change the container'ssize(), and may also changecapacity()。
Example
Let us thoroughly master through a series of examples.resizeUsage.
Example 1: Basic usage - expanding the container
Example
#include <vector>
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 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 there are 3 elements: 1, 2, 3.
resize(10)Resize to 10.- The 7 newly added elements are value-initialized to 0.
Example 2: Filling new elements with a specified value
You canresizespecify a fill value when.
Example
#include <vector>
#include <string>
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 << "\nresize(5, \"Unknown\"After ) - 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:
- Usage
resize(5, "Unknown")Resize to 5. - The 3 newly added elements are all filled with "Unknown".
Example 3: Using resize to shrink the container
resizeIt can also be used to shrink the container.
Example
#include <vector>
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)Resize to 5.- The last 5 elements are removed.
Example 4: resize and capacity
resizewill change simultaneouslysizeandcapacity。
Example
#include <vector>
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 the value is less than the current capacity,capacityremains unchanged. - When
resizewhen the value is greater than the current capacity,capacitywill increase accordingly.
Example 5: Using resize to create a fixed-size container
resizecan be used to create a fixed-size container.
Example
#include <vector>
int main() {
// create a vector containing 100 copies of -1
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 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:
- Usage
resizeCan create a container containing a fixed number of elements. - This is useful when you need to pre-allocate a fixed-size container.
other extensions