C++ std::array::swap
The std::array::swap
function exchanges the contents of two arrays of the same type and size. It is a simple and efficient way to swap data between arrays without copying elements individually.
Syntax of std::array::swap
</>
Copy
void swap(array& other) noexcept(noexcept(swap(declval<T>(), declval<T>())));
Parameters
Parameter | Description |
---|---|
other | A reference to another array of the same type and size whose contents will be swapped with the current array. |
Return Value
The std::array::swap
function does not return any value. It swaps the contents of the two arrays in place.
Exceptions
The std::array::swap
function does not throw exceptions unless the swap operation for the element type T
throws an exception. If T
‘s swap
is noexcept
, the array swap operation is also noexcept
.
Examples for std::array::swap
Example 1: Swapping Two Arrays
This example demonstrates how to use swap
to exchange the contents of two std::array
objects:
</>
Copy
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr1 = {1, 2, 3, 4, 5};
std::array<int, 5> arr2 = {10, 20, 30, 40, 50};
std::cout << "Before swap:" << std::endl;
std::cout << "arr1: ";
for (const auto& elem : arr1) std::cout << elem << " ";
std::cout << "\narr2: ";
for (const auto& elem : arr2) std::cout << elem << " ";
std::cout << std::endl;
arr1.swap(arr2); // Swap contents of arr1 and arr2
std::cout << "\nAfter swap:" << std::endl;
std::cout << "arr1: ";
for (const auto& elem : arr1) std::cout << elem << " ";
std::cout << "\narr2: ";
for (const auto& elem : arr2) std::cout << elem << " ";
std::cout << std::endl;
return 0;
}
Explanation:
- Two arrays,
arr1
andarr2
, are initialized with different values. - The contents of both arrays are printed before calling
swap
. - The
swap
function exchanges the contents ofarr1
andarr2
. - The contents of both arrays are printed again to confirm the swap operation.
Output:
Before swap:
arr1: 1 2 3 4 5
arr2: 10 20 30 40 50
After swap:
arr1: 10 20 30 40 50
arr2: 1 2 3 4 5
Example 2: Using std::array::swap with Strings
This example demonstrates swapping the contents of two std::array
objects containing strings:
</>
Copy
#include <iostream>
#include <array>
#include <string>
int main() {
std::array<std::string, 3> arr1 = {"Apple", "Banana", "Cherry"};
std::array<std::string, 3> arr2 = {"X", "Y", "Z"};
std::cout << "Before swap:" << std::endl;
std::cout << "arr1: ";
for (const auto& elem : arr1) std::cout << elem << " ";
std::cout << "\narr2: ";
for (const auto& elem : arr2) std::cout << elem << " ";
std::cout << std::endl;
arr1.swap(arr2); // Swap contents of arr1 and arr2
std::cout << "\nAfter swap:" << std::endl;
std::cout << "arr1: ";
for (const auto& elem : arr1) std::cout << elem << " ";
std::cout << "\narr2: ";
for (const auto& elem : arr2) std::cout << elem << " ";
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::array
objects containing strings are initialized with different values. - The contents of both arrays are printed before calling
swap
. - The
swap
function exchanges the contents of the two arrays. - The contents of both arrays are printed again to confirm the swap operation.
Output:
Before swap:
arr1: Apple Banana Cherry
arr2: X Y Z
After swap:
arr1: X Y Z
arr2: Apple Banana Cherry