Cpp Libs Array Fill
[ C++ Container Class ](#)
* * *
`fill` is a function in array used to **fill all elements**, which can quickly set all elements to the same value.
`fill` is a member function of the container class, used to **set all elements in the container to a specified value**. This is an O(n) operation that requires traversing all elements.
`fill` is a shortcut for initializing or resetting an array.
**Word Definition**: `fill` means "to fill", i.e., setting all elements to a specified value.
* * *
## Basic Syntax and Parameters
`fill` is a member function of the container class and requires specifying the fill value.
### Syntax Format
void fill(const T& value);
### Parameter Description
* **Parameter**: `value`
* Type: Same as the container element type
* Description: The value to fill into all elements.
### Function Description
* **Return Value**: `void` (no return value).
* **Effect**: Sets each element in the container to `value`.
* **Note**: `fill` modifies all elements in the container.
* * *
## Examples
Let's thoroughly master the usage of `fill` through a series of examples.
### Example 1: Basic Usage - Filling with the Same Value
## Example
#include
#include
int main(){
std::array numbers ={1, 2, 3, 4, 5};
std::cout<<"Before fill: ";
for(int n : numbers){
std::cout<< n <<" ";
}
std::cout<< std::endl;
// Fill all elements with 0
numbers.fill(0);
std::cout<<"After fill: ";
for(int n : numbers){
std::cout<< n <<" ";
}
std::cout<< std::endl;
return 0;
}
**Expected Output:**
Before fill: 1 2 3 4 5After fill: 0 0 0 0 0
**Code Analysis:**
1. `numbers.fill(0)` sets all 5 elements to 0.
2. This is an O(n) operation that traverses all elements and assigns values.
### Example 2: Initializing to Specific Values
`fill` can be used to quickly initialize an array.
## Example
#include
#include
#include
int main(){
// Create and initialize to -1
std::array scores;
scores.fill(-1);
std::cout<<"Initialized to -1: ";
for(int s : scores){
std::cout<< s <<" ";
}
std::cout<< std::endl;
// Create and initialize to empty strings
std::array names;
names.fill("Unknown");
std::cout<<"Initialized to Unknown: ";
for(const auto& n : names){
std::cout<< n <<" ";
}
std::cout<< std::endl;
return 0;
}
**Expected Output:**
Initialized to -1: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1Initialized to Unknown: Unknown Unknown Unknown
**Code Analysis:**
* `fill` can be used to quickly initialize arrays of any type.
* For integers, 0 or -1 are commonly used; for strings, empty strings or specific placeholders are often used.
### Example 3: Resetting an Array
`fill` can be used to reset an array to its initial state.
## Example
#include
#include
int main(){
std::array buffer;
// Initialize to 1-5
for(int i =0; i <5;++i){
buffer= i +1;
}
std::cout<<"Initial data: ";
for(int n : buffer) std::cout<< n <<" ";
std::cout<< std::endl;
// Simulate resetting after using buffer
buffer.fill(0);
std::cout<<"After reset: ";
for(int n : buffer) std::cout<< n <<" ";
std::cout<< std::endl;
return 0;
}
**Expected Output:**
Initial data: 1 2 3 4 5After reset: 0 0 0 0 0
**Code Analysis:**
* `fill` can quickly reset an array to a known state.
* This is useful when the same buffer needs to be used multiple times.
### Example 4: Using fill with std::generate
`fill` used in combination with STL algorithms.
## Example
#include
#include
#include
int main(){
std::array numbers;
// Use fill to fill initial values
numbers.fill(1);
std::cout<<"After filling with 1: ";
for(int n : numbers) std::cout<< n <<" ";
std::cout<< std::endl;
// Increment each element
for(auto& n : numbers){
n *=2;
}
std::cout<<"After doubling: ";
for(int n : numbers) std::cout<< n <<" ";
std::cout<< std::endl;
return 0;
}
**Code Analysis:**
* `fill` is used to set initial values.
* Further processing can be done with loops or algorithms afterward.
* * C++ Container Class ](#)
YouTip