In this article, we will discuss the difference between the array::fill and array::swap in C++. But before discussing their differences, we must know about the array::fill and array::swap .
Two member functions of the C++ Standard Template Library (STL) that are connected to the std::array template class are array::fill and array::swap . Let's go over each of these roles in more detail:
What is array::fill function?
We can use the array::fill function to give each element of the array a specific value.
Syntax:
It has the following syntax:
array.fill(value);
In this case, value is the value we wish to apply to every element of the array, and an array is an instance of the std::array template.
Example:
Let's take an example to illustrate the working of array::fill in C++.
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> myArray;
myArray.fill(07);
for (const auto& element : myArray) {
cout << element << " ";
}
return 0;
}
Output:
The array::fill is useful when we wish to set the value of each element in the array to a particular one.
What is array::swap?
We can use the swap function to swap the contents of two arrays of the same size and type.
Syntax:
It has the following syntax:
array1.swap(array2);
Example:
Let's take an example to illustrate the working of array::swap in C.
#include <array>
#include <iostream>
using namespace std;
int main() {
array<int, 5> array1 = {7,9,8,1,1};
array<int, 5> array2 = {7,7,9,7,5};
array1.swap(array2);
cout << "Array 1: ";
for (const auto& element : array1) {
cout << element << " ";
}
cout << "\nArray 2: ";
for (const auto& element : array2) {
cout << element << " ";
}
return 0;
}
Output:
Array::swap is useful when we need to switch the contents of two arrays quickly.
Main Differences between the array::fill and array::swap:
There are several differences between the array::fill and array::swap in C++. Some main differences are as follows:
| S.No | array::fill() | array::swap() |
|---|---|---|
1 |
It is employed to assign a single value to each element in an array. | It is employed to switch the contents of two arrays. |
2 |
The way it is written is:fill (const value_type& val); | It reads as follows:swap (array& x) |
3 |
The value we wish to enter into the array is the only parameter required. | The array we wish to swap out is the only parameter required. |
4 |
There is no return value for it. | There is no return value for it. |
5 |
It is linearly complex. | It is linearly complex. |
Benefits of array::fill in C++:
Regarding readability, efficiency, and simplicity, the C++ std::array::fill function is a great choice when we need to initialize or reset the values of every element in an array. The following are some benefits of std::array::fill :
- Readability and Expression: Setting each element of an array to a particular value is made simple and expressive with the fill function. The code may become easier to read and understand as a result.
- Code Conciseness: Less code is required when fill is used instead of loops for initialization. This results in clearer and frequently easier-to-read and updated code.
- Performance: Compared to manually iterating through the array and assigning values, the standard library may implement the fill function more efficiently. Low-level optimizations may be utilized by the standard library implementation to effectively set the values of several elements at once.
- Consistency with Other Containers: When working with various container types, fill function encourages consistency in the code. Use functions similar to std::fill for other containers in your codebase, such as vectors or C-style arrays, to ensure a uniform style.
- Ease of Maintenance: Updating the initialization value only requires changing it once in the argument to fill, which lowers error risk and facilitates maintenance.
Benefits of array::swap in C++:
When we need to swap the contents of two arrays that are the same size and type, C++'s std::array::swap function offers several advantages. The following are some benefits of std::array::swap usage:
- Efficiency: The standard library may have an optimized swap function implementation. Better performance results from its ability to exchange the contents of two arrays effectively without requiring a manual copy of each element.
- No Temporary Storage: The Swap function completes the exchange in place without requiring extra memory, in contrast to manual swapping, which may require a temporary storage variable. It may be crucial when working with sizable arrays.
- Readability: Using swap instead of manually swapping each element makes your code clearer and easier to understand. It makes it very evident that the goal is to swap out all of the arrays' contents.
- Conformity with Additional Containers: Using std::array::swap encourages consistency in code when working with various container types, much like std::array::fill. It is consistent with other standard library swap functions.
- Simplicity of Use: The Swap offers a handy method for exchanging all data in two arrays. It removes the requirement to create a unique loop or algorithm to switch out elements.
Conclusion:
In conclusion, array::swap is used to swap the contents of two arrays of the same size and type, and array::fill is used to set all of an array's elements to a specified value.