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.
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
Recommended Posts:
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
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.



