Cpp Libs Vector Resize
[ C++ Container Class ](#)
* * *
`resize` is a function in vector used for **adjusting the size**, which can change the number of elements in the container.
`resize` is a member function of the container class, used to **adjust the container's size to a specified number of elements**. If the new size is larger than the current size, new elements will be added; if it is smaller, excess elements will be removed.
`resize` provides flexible ability to adjust container size, and is one of the powerful dynamic features of vector.
**Word Meaning**: `resize` means "adjust size", i.e., changing the number of elements in the container.
* * *
## Basic Syntax and Parameters
`resize` is a member function of the container class, which 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` (unsigned integer type, usually `size_t`)
* Description: The new number of elements in the container.
**Parameter** (optional): `value`
* Type: Same as the container element type
* Description: When elements need to be added, used as the default value to fill new elements. If not specified, value initialization is used (0 for built-in types).
### Function Description
* **Return Value**: `void` (no return value).
* **Effect**: Adjust the container's `size()` to count. If elements need to be added, use value to fill; if elements need to be removed, discard excess elements.
* **Note**: `resize` will change the container's `size()`, and may also change `capacity()`.
* * *
## Examples
Let's thoroughly master the usage of `resize` through a series of examples.
### Example 1: Basic Usage - Expanding the Container
## Example
#include
#include
int main(){
std::vector 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;
// Expand container to 10 elements
numbers.resize(10);
std::cout<<"n After 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:**
Initial state - size: 3, capacity: 3
Elements: 1 2 3
After resize(10) - size: 10, capacity: 10
Elements: 1 2 3 0 0 0 0 0 0 0
**Code Analysis:**
1. Initially there are 3 elements: 1, 2, 3.
2. `resize(10)` adjusts the size to 10.
3. The 7 newly added elements are value-initialized to 0.
### Example 2: Using Specified Value to Fill New Elements
You can specify a fill value when using `resize`.
## Example
#include
#include
#include
int main(){
std::vector 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<<"n After 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:**
Initial state - size: 2
Elements: Alice Bob
After resize(5, "Unknown") - size: 5
Elements: Alice Bob Unknown Unknown Unknown
**Code Analysis:**
* Use `resize(5, "Unknown")` to adjust the size to 5.
* The 3 newly added elements are all filled with "Unknown".
### Example 3: Using resize to Shrink the Container
`resize` can also be used to shrink the container.
## Example
#include
#include
int main(){
std::vector 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<<"n After 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:**
Initial state - size: 10
Elements: 1 2 3 4 5 6 7 8 9 10
After resize(5) - size: 5
Elements: 1 2 3 4 5
**Code Analysis:**
* `resize(5)` adjusts the size to 5.
* The last 5 elements are deleted.
### Example 4: resize and capacity
`resize` changes both `size` and `capacity` simultaneously.
## Example
#include
#include
int main(){
std::vector 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:**
Initial - size: 2, capacity: 100
After resize(50) - size: 50, capacity: 100
After resize(10) - size: 10, capacity: 100
**Code Analysis:**
* When `resize` value is less than the current capacity, `capacity` remains unchanged.
* When `resize` value is greater than the current capacity, `capacity` will increase accordingly.
### Example 5: Using resize to Create Fixed-Size Container
`resize` can be used to create fixed-size containers.
## Example
#include
#include
int main(){
// Create a vector containing 100 elements with -1
std::vector 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<<" ";
}
std::cout<< std::endl;
// Set values at certain positions
scores=100;
scores=95;
std::cout<<"First 10 elements after modification: ";
for(int i =0; i <10;++i){
std::cout<< scores<<" ";
}
std::cout<< std::endl;
return 0;
}
**Code Analysis:**
* Using `resize` can create containers containing a fixed number of elements.
* This is useful when you need to pre-allocate a fixed-size container.
* * C++ Container Class ](#)
YouTip