Metadata

  • ๐Ÿ“… Date :: 09-06-2025
  • ๐Ÿท๏ธ Tags :: cpp

Notes

Detailed Notes on the fill Function in C++

In this tutorial, we learned about the fill function in C++, which is part of the C++ Standard Library. The fill function is used to fill a range of elements within a data structure with a specified value. This function is particularly helpful when you want to initialize an array or any container with the same value efficiently, especially when dealing with large data structures.


Key Points of the fill Function

  1. Purpose:

    • The fill function helps you quickly populate an entire range of elements in a data structure (like an array or vector) with a single value.
    • It eliminates the need to manually assign values to each element, making your code cleaner and more efficient.
  2. Function Signature:

    std::fill(begin_iterator, end_iterator, value);
     
    • begin_iterator: The beginning address (or iterator) of the range.
    • end_iterator: The ending address (or iterator) of the range (non-inclusive).
    • value: The value to assign to each element in the range.

Example 1: Filling an Array with the Same Value

Letโ€™s consider an example where you have an array of strings and want to fill all elements with the same value (e.g., โ€œpizzaโ€).

  1. Initial Array Declaration:

    std::string foods[10];
     
  2. Filling the Array:

    • If we were to manually assign โ€œpizzaโ€ to each element, we would have to do it like this:

      foods[0] = "pizza";
      foods[1] = "pizza";
      // ... repeat for all elements
       
    • However, this is tedious, especially when dealing with large arrays. Instead, we can use the fill function:

      std::fill(foods, foods + 10, "pizza");
       
    • The foods is the starting address of the array, and foods + 10 is the ending address (one past the last element).

  3. Displaying the Filled Array:

    for (const std::string& food : foods) {
        std::cout << food << " ";
    }
    std::cout << std::endl;
     
    • The array now contains 10 elements, all filled with the word "pizza".

Example 2: Using a Larger Array

For an array with more elements (e.g., 100 elements), it would be inefficient to manually type the value 100 times. The fill function simplifies this:

std::string foods[100];
std::fill(foods, foods + 100, "pizza");
 

Here, all 100 elements are filled with "pizza".

Dynamic Array Size with a Variable

If the array size is stored in a variable, you can use that variable to define the range in the fill function. For instance:

const int size = 100;
std::string foods[size];
std::fill(foods, foods + size, "pizza");
 

If the array size changes (e.g., to 150 elements), you can just update the size variable, and the fill function will adapt accordingly.


Filling Part of the Array

The fill function can also be used to fill specific portions of an array. Letโ€™s break it down into practical examples:

  1. Filling the First Half of the Array:
    • To fill the first half of an array with "pizza" and leave the second half empty:

      std::string foods[100];
      std::fill(foods, foods + size / 2, "pizza");
       
    • Here, foods + size / 2 gives the ending address for the first half of the array. The first half will now contain "pizza", and the second half will be uninitialized (empty).

  2. Filling the Second Half with Another Value:
    • To fill the second half of the array with "hamburgers":

      std::fill(foods + size / 2, foods + size, "hamburger");
       
    • The first half of the array contains "pizza", and the second half now contains "hamburgers".


Filling with Multiple Values (Dividing into Thirds)

You can also use the fill function to divide the array into multiple sections and fill each section with a different value. For example, if you want to fill the first third with "pizza", the second third with "hamburgers", and the last third with "hot dogs", hereโ€™s how to do it:

  1. Array Size:

    • Letโ€™s assume the array has 99 elements for simplicity (since 99 divides evenly by 3).
  2. Filling the First Third with "pizza":

    std::fill(foods, foods + size / 3, "pizza");
     
  3. Filling the Second Third with "hamburgers":

    std::fill(foods + size / 3, foods + 2 * size / 3, "hamburger");
     
  4. Filling the Last Third with "hot dogs":

    std::fill(foods + 2 * size / 3, foods + size, "hot dog");
     

After this, the array will be filled in three sections:

  • The first third contains "pizza",
  • The second third contains "hamburgers",
  • The last third contains "hot dogs".

Edge Cases and Considerations

  • Changing Array Size: If you change the size of the array, ensure that the fill functionโ€™s second argument (the ending address) is updated accordingly.
  • Empty Arrays: If you attempt to fill an empty array (e.g., with size 0), the fill function will not make any changes, but no errors will occur.

Advantages of Using fill

  1. Code Simplicity: The fill function makes the code cleaner and more readable, especially when dealing with large arrays.
  2. Efficiency: Instead of manually assigning values to each element, you can use fill to handle the task in one line.
  3. Flexibility: You can fill any portion of the array, not just the entire array, and with any value of any type.

Final Code Example

Hereโ€™s the complete C++ code demonstrating the fill function:

#include <iostream>
#include <string>
#include <algorithm> // for std::fill
 
int main() {
    const int size = 100;
    std::string foods[size];
 
    // Fill the entire array with "pizza"
    std::fill(foods, foods + size, "pizza");
 
    // Display the entire array
    for (const std::string& food : foods) {
        std::cout << food << " ";
    }
    std::cout << std::endl;
 
    // Fill first half with "pizza" and second half with "hamburger"
    std::fill(foods, foods + size / 2, "pizza");
    std::fill(foods + size / 2, foods + size, "hamburger");
 
    // Display updated array
    for (const std::string& food : foods) {
        std::cout << food << " ";
    }
    std::cout << std::endl;
 
    // Fill the array in thirds
    std::fill(foods, foods + size / 3, "pizza");
    std::fill(foods + size / 3, foods + 2 * size / 3, "hamburger");
    std::fill(foods + 2 * size / 3, foods + size, "hot dog");
 
    // Display final array
    for (const std::string& food : foods) {
        std::cout << food << " ";
    }
    std::cout << std::endl;
 
    return 0;
}
 

Output:

pizza pizza pizza pizza pizza pizza pizza pizza pizza pizza ...
pizza pizza pizza pizza pizza pizza pizza pizza pizza pizza ...
pizza pizza pizza hamburger hamburger hamburger hamburger ...


Conclusion

The fill function is a powerful tool in C++ for efficiently populating arrays or containers with a specified value. It saves time and effort, particularly when dealing with large data structures, and enhances code clarity and maintainability. By understanding and using fill, developers can simplify array initialization and partitioning tasks.


References