array::fill() and array::swap() in C++ STL
Last Updated :
30 Jun, 2022
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays.
array::fill()
This function is used to set a common value for all the elements of the array container. Syntax :
arrayname.fill(value)
Parameters :
The value to be set for all the elements of
the container is passed as parameter.
Result :
All the elements of the container are
set to be equal to the parameter passed.
Examples:
Input : myarray = {1, 2, 3, 4}
myarray.fill(5);
Output : myarray = {5, 5, 5, 5}
Input : myarray = {1, 2, 3, 4, 5, 6, 7}
myarray.fill(2);
Output : myarray = {2, 2, 2, 2, 2, 2, 2}
Errors and Exceptions 1. It throws an error if the assignment operation throws some error. 2. It has a basic no exception throw guarantee otherwise.
CPP
// CPP program to illustrate
// Implementation of fill() function
#include <array>
#include <iostream>
using namespace std;
int main()
{
// array container declaration
array<int, 4> myarray{ 1, 2, 3, 4 };
// Using fill() function to
myarray.fill(5);
// printing the array
for(auto it=myarray.begin(); it<myarray.end(); ++it)
cout<<*it<<" ";
return 0;
}
Output:
5 5 5 5
array::swap()
This function is used to swap the contents of one array with another array of same type and size. Syntax :
arrayname1.swap(arrayname2)
Parameters :
The name of the array with which
the contents have to be swapped.
Result :
All the elements of the 2 array are swapped.
Examples:
Input : myarray1 = {1, 2, 3, 4}
myarray2 = {3, 5, 7, 9}
myarray1.swap(myarray2);
Output : myarray1 = {3, 5, 7, 9}
myarray2 = {1, 2, 3, 4}
Input : myarray1 = {1, 3, 5, 7}
myarray2 = {2, 4, 6, 8}
myarray1.swap(myarray2);
Output : myarray1 = {2, 4, 6, 8}
myarray2 = {1, 3, 5, 7}
Errors and Exceptions 1. It throws an error if the array are not of the same type. 2. It throws error if the array are not of the same size. 2. It has a basic no exception throw guarantee otherwise.
CPP
// CPP program to illustrate
// Implementation of swap() function
#include <array>
#include <iostream>
using namespace std;
int main()
{
// array container declaration
array<int, 4> myarray1{ 1, 2, 3, 4 };
array<int, 4> myarray2{ 3, 5, 7, 9 };
// using swap() function to swap elements of arrays
myarray1.swap(myarray2);
// printing the first array
cout<<"myarray1 = ";
for(auto it=myarray1.begin(); it<myarray1.end(); ++it)
cout<<*it<<" ";
// printing the second array
cout<<endl<<"myarray2 = ";
for(auto it=myarray2.begin(); it<myarray2.end(); ++it)
cout<<*it<<" ";
return 0;
}
Output:
myarray1 = 3 5 7 9
myarray2 = 1 2 3 4
Let us see the differences in a tabular form -:
| array::fill() | array::swap() |
1. | It is used to fill all elements of an array with a single value. | It is used to swap the content of one array with another array. |
2. | Its syntax is -: fill (const value_type& val); | Its syntax is -: swap (array& x) |
3. | It only takes one parameter that is the value that we want to fill in the array. | It takes only one parameter that is the array that we want to swap. |
4. | It does not have any return value. | It does not have any return value. |
5. | Its complexity is linear. | Its complexity is Linear. |
Similar Reads
STD::array in C++
The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array containe
3 min read
array::size() in C++ STL
The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++.Example:C++// C++ Program to illustrate the use of array::si
2 min read
array::empty() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::empty() empty() function is used to check if the array container is empty or not. Syntax : arrayname.empty
1 min read
array::front() and array::back() in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be
3 min read
array::at() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par
2 min read
array::operator[ ] in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator.
2 min read
array get() function in C++ STL
The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get(array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the position of the
2 min read
array data() in C++ STL with Examples
The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object. Syntax: array_name.data() Parameters: The function does not accept any parameters. Return Value: The function returns an pointer. Below programs illustrate the above functi
2 min read
array::max_size() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::max_size() This function returns the maximum number of elements that the array container can contain. In c
1 min read