Algorithm Fill Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Fill Function

Algorithm Fill Function

BLUF: Mastering Algorithm Fill Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Fill Function

C++ is renowned for its efficiency. Learn how Algorithm Fill Function enables low-level control and high-performance computing in the tutorial below.

C++ Algorithm fill

The fill function in C++ Algorithm is utilized to set a uniform new value to all elements within a defined range [first, end) through the use of the operator=.

Note: Range [first, last) means first is included in the range but last is not included.

Syntax

Example

template <class ForwardIterator, class T>
  void fill (ForwardIterator first, ForwardIterator last, const T& val);

Parameter

A forward iterator indicates the location of the initial element within a defined range.

The last parameter is a forward iterator that points to one position beyond the last element in the specified range that will be iterated over.

The variable "val" represents the value that will be assigned to elements within the range [first, last).

Return value

Complexity

The complexity increases linearly based on the distance between the first and last elements during each assignment.

Data races

The elements within the range [first1, last1) are altered with each element being accessed precisely once.

Exception safety

This function raises an exception if there is an exception during element assignments or operations on an iterator.

Please be aware that using incorrect parameters can result in unpredictable behavior.

Example 1

Let's explore a basic example to showcase the utilization of the fill method:

Example

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> v(5);

  fill(v.begin(), v.end(), 2);

  for_each(v.begin(), v.end(), [](int x) { cout << x << ","; });
  
  return 0;
}

Output:

Output

2,2,2,2,2,

Example 2

Let's see another simple example:

Example

#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vector

using namespace std;

int main () {
  vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0

  fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
  fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0

  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

Output:

Output

myvector contains: 5 5 5 8 8 8 0 0

Example 3

Let's see another simple example:

Example

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   vector <int> v1;  
   vector <int>::iterator Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
   {  
      v1.push_back( 5 * i );  
   }  
  
   cout << "Vector v1 = ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")" << endl;  
  
   // Fill the last 5 positions with a value of 2  
   fill( v1.begin( ) + 5, v1.end( ), 2 );  
  
   cout << "Modified v1 = ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")" << endl;  
   
   return 0;
}

Output:

Output

Vector v1 = ( 0 5 10 15 20 25 30 35 40 45 )
Modified v1 = ( 0 5 10 15 20 2 2 2 2 2 )

Example 4

Let's see another simple example:

Example

#include <algorithm>
#include <vector>
#include <iostream>
#include <iomanip>

using namespace std;
 
void print(const vector <int>& v)
{
    vector <int>::const_iterator i;
    for(i = v.begin(); i != v.end(); i++)
    {
        cout << setw(2) <<  *i << " ";
    }
    cout << endl;
}
 
int main()
{
    int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    vector <int> v(arr, arr + sizeof(arr) / sizeof(int));
 
    cout << "Vector before fill" << endl;
    print(v);
    fill(v.begin() + 4, v.end() - 3, -1);
    cout << "Vector after fill" << endl;
    print(v);
}

Output:

Output

Vector before fill
 0  1  2  3  4  5  6  7  8  9 
Vector after fill
 0  1  2  3 -1 -1 -1  7  8  9

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience