array::fill() and array::swap() 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::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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

myarray1 = 3 5 7 9 
myarray2 = 1 2 3 4 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.