Open In App

array::fill() and array::swap() in C++ STL

Last Updated : 30 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads