fill() Function in C++
The C++ fill() function is a standard library that provides a particular value to a list of components in a container structure. It offers an easy method for setting all the items inside the range specified to the required value without the need to loop across each one individually. The fill() method contains three parameters, which are the start iterator, the end iterator, and the value.
Syntax:
fill(Iterator start, Iterator end, value);
- The Iterator starts to define the beginning of the range.
- The Iterator defines the end of the range, which is one less than the end value
- The value describes the type of value to be filled
The function is helpful with a variety of sequence containers, including arrays, vectors, lists, and others. It is also compatible with iterators. The fill() method loops over the range that has been provided, assigning a value to each element and removing any existing values in the process. Each element inside the range is changed since it acts in a linear manner.
A key point to remember is that the fill() method does not affect components outside of the range specified or expand the container. While maintaining the rest of the container's contents, it only alters the components that fall inside the specified range.
The fill() method lets you quickly initialize or modify a number of components in a container with a given value. This could be useful when you need to update a subset of items to a certain value or when you need to set all elements to a default or flag value.
The C++ fill() method is a strong tool that makes it easier to give values to a variety of components in a container. You may manage container elements using it without the use of explicit loops or manual element assignments, giving you a clear and effective solution.
Example: 1
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
// Create a vector of size 10
vector<int> Myvector(10);
// Fill all elements of the vector with 3
fill(Myvector.begin(), Myvector.end(), 3);
// Print the vector elements
cout << "Myvector: ";
for (int element: Myvector)
cout << element << " ";
cout << endl;
// Fill the first 5 elements of the vector with 500
fill(Myvector.begin(), Myvector.begin() + 5, 500);
// Print the vector elements
cout << "Myvector: ";
for (int element: Myvector)
cout << element << " ";
cout << endl;
// Fill the remaining elements of the vector with 800
fill(Myvector.begin() + 5, Myvector.end(), 800);
// Print the vector elements
cout << "Myvector: ";
for (int element: Myvector)
cout << element << " ";
cout << endl;
return 0;
}
Output
Myvector: 3 3 3 3 3 3 3 3 3 3
Myvector: 500 500 500 500 500 3 3 3 3 3
Myvector: 500 500 500 500 500 800 800 800 800 800
When the first fill() function is used, all of the members of the vector Myvector are given the value 3. The second application assigns the value 500 to the first five members of Myvector. The third use finally assigns the value 800 to the remaining items, beginning with index 5.
The vector components are printed after each filling operation to confirm the modifications made by the fill() method.
Example: 2
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> numbers(8);
std::fill(numbers.begin(), numbers.begin()+4, 5);
// Fill the first 4 elements with the value 5
std::fill(numbers.begin()+3, numbers.end()-2, 8); // Fill a range from the 4th element to the 6th element with the value 8
std::cout << "numbers contains:";
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output:
numbers contains: 5 5 5 8 8 8 0 0
The first application of fill() uses the range from numbers to give the first 4 items of numbers the value 5. Start () with numbers.begin() + 4. The second application uses numbers to fill up a range from the fourth element (index three) through the sixth element (index five) with the value eight.3 plus digits and begin().end() - 2.
Following the filling procedures, a loop, and Iterator are used to output the vector elements. The numbers vector's elements are sent to the console.