Difference between array::fill() and array::swap() in C++
In this article, we will discuss the difference between the array::fill() and array::swap() in C++. But before discussing the differences, we must know about the array::fill() and array::swap().
In C++, a fixed-size array containing several helpful member methods can be obtained by using the std::array class template. Fill() and swap() are two examples of methods that have different uses in manipulating arrays. The fill() function is used to provide each element in the array with a specific value. When the user needs to quickly initialize or reset every element to a common value, this operation comes in helpful. For example, fill() can be used to set all entries of an array to zero or any other desired initial value if the array contains a collection of numerical data. Usually, the syntax entails giving the desired value as a parameter when using the array object's fill() method.
As the name implies, the purpose of the swap() function is to swap the contents of two arrays of the same size and type. It can be useful when the layout of components in two arrays needs to be modified without directly copying individual elements. The swap() function makes switching faster and easier by eliminating the need for temporary variables and explicit element-wise assignments. The second array must be passed as an argument when calling swap() on the first array.
Methods Used Are:
- std::array::fill():
The fill() method in C++ is a member function of the std::array class, and it is used to assign a specified value to every element in the array.
Syntax:
It has the following syntax:
#include <array>
std::array<T, N> myArray; // Replace T with the type and N with the size of the array
// Using fill() to assign a specific value to all elements
myArray.fill(value);
- std::array::swap():
The std::array::swap() method is another member function of the std::array class, and it is used to exchange the contents of two arrays of the same type and size.
Syntax:
It has the following syntax:
#include <array>
std::array<T, N> array1;
std::array<T, N> array2;
// Using swap() to change the contents of two arrays
array1.swap(array2);
Difference between array::fill() and array::swap():
There are several differences between the array::fill() and array::swap() in C++. Some main differences between the array::fill() and array::swap() are as follows:
Name | std::array::fill() | std::array::swap() |
Purpose | Fills all elements of the array with a specified value. | Swaps the contents of two arrays of the same type and size |
Syntax | void fill(const T& value); | void swap(array& other) noexcept; |
Parameters | value: The value to fill the array with. | other: The array to swap content with. |
Mutability | Modifies the content of the array. | Modifies the content of both arrays involved in the swap. |
Return Type | void | Void |
Time Complexity | O(N), where N is the size of the array. | O(N), where N is the size of the array. |
Example 1:
Let's take an example to illustrate the use of array::fill() and array::swap() function in C++.
#include <iostream>
#include <array>
int main() {
// Example using std::array::fill()
std::array<int, 5> myArray1; // Declare an array of integers with size 5
// Use fill() to assign the value 42 to all elements
myArray1.fill(123);
// Display the elements of the array after using fill()
std::cout << "Array after fill(): ";
for (const auto &element : myArray1) {
std::cout << element << " ";
}
std::cout << std::endl;
// Example using std::array::swap()
std::array<int, 5> myArray2 = {12, 25, 37, 49, 56}; // Another array of integers
// Display the contents of both arrays before swapping
std::cout << "Array 1 before swap: ";
for (const auto &element : myArray1) {
std::cout << element << " ";
}
std::cout << std::endl;
std::cout << "Array 2 before swap: ";
for (const auto &element : myArray2) {
std::cout << element << " ";
}
std::cout << std::endl;
// Use swap() to interchange the contents of the two arrays
myArray1.swap(myArray2);
// Display the contents of both arrays after swapping
std::cout << "Array 1 after swap: ";
for (const auto &element : myArray1) {
std::cout << element << " ";
}
std::cout << std::endl;
std::cout << "Array 2 after swap: ";
for (const auto &element : myArray2) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Array after fill(): 123 123 123 123 123
Array 1 before swap: 123 123 123 123 123
Array 2 before swap: 12 25 37 49 56
Array 1 after swap: 12 25 37 49 56
Array 2 after swap: 123 123 123 123 123
Example 2:
Let's take another example to illustrate the use of array::fill() and array::swap() function in C++.
#include <iostream>
#include <array>
int main() {
// Example using std::array::fill()
std::array<double, 3> grades; // Declare an array to store grades
// Use fill() to initialize all grades to 0.0
grades.fill(0.0);
// Display the initial grades
std::cout << "Initial Grades: ";
for (const auto &grade : grades) {
std::cout << grade << " ";
}
std::cout << std::endl;
// Example using std::array::swap()
std::array<double, 3> midtermGrades = {95.5, 87.0, 91.2}; // Another array with midterm grades
// Display the contents of both arrays before swapping
std::cout << "Grades before swap: ";
for (const auto &grade : grades) {
std::cout << grade << " ";
}
std::cout << std::endl;
std::cout << "Midterm Grades before swap: ";
for (const auto &grade : midtermGrades) {
std::cout << grade << " ";
}
std::cout << std::endl;
// Use swap() to interchange the contents of the two arrays
grades.swap(midtermGrades);
// Display the contents of both arrays after swapping
std::cout << "Grades after swap: ";
for (const auto &grade : grades) {
std::cout << grade << " ";
}
std::cout << std::endl;
std::cout << "Midterm Grades after swap: ";
for (const auto &grade : midtermGrades) {
std::cout << grade << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Initial Grades: 0 0 0
Grades before swap: 0 0 0
Midterm Grades before swap: 95.5 87 91.2
Grades after swap: 95.5 87 91.2
Midterm Grades after swap: 0 0 0
Conclusion:
In conclusion, C++ offers two different methods for working with arrays: std::array::fill() and std::array::swap(). The fill() method is used to uniformly assign a specified value to each element within the array to efficiently initialize or reset all of the elements in an array to a common value. It is especially helpful when a constant starting state is needed. Conversely, the swap() function eliminates the need for temporary variables or explicit element-wise assignments by efficiently swapping the contents of two arrays of the same type and size. The swap() facilitates efficient array management, which is frequently used to exchange or rearrange the data held in two arrays.