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

10 Jan, 2018
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.
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 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

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 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 

cpp-array
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Friend class and function in C++
Polymorphism in C++
List in C++ Standard Template Library (STL)
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
Queue in C++ Standard Template Library (STL)
new and delete operators in C++ for dynamic memory
Destructors in C++