C++ swap() function
swap is used in array toswaps the contents of two arrays, and is a very useful tool.
swap is a member function of the container class, used toswaps the contents of two arrays. The two arrays must be of the same type and same size.
swap provides an efficient way to swap the contents of two containers and is a commonly used tool in algorithm and data structure implementations.
Word meaning: swap means 'swap', i.e., to exchange the contents of two arrays.
Basic syntax and parameters
swap is a member function of the container class and takes another array as a parameter.
Syntax format
void swap(array& other) noexcept;
Parameter Description
- Parameters:
other - Type:
array(reference to the same type)& - Description: Another array whose contents are to be swapped. The two arrays must have the same element type and size.
Function description
- Return Value:
void(no return value). - effect: Swaps all elements of the two arrays. This is an O(n) operation requiring element-by-element swapping.
- Note:
swapis usually declared asnoexcept, indicating it does not throw exceptions.
Example
Let us thoroughly master swap through a series of examples from simple to complex.
Example 1: Basic usage - swapping two arrays
Example
#include
#include
int
main
(
)
{
std
::
array
<
int
,
5
a
=
{
1
,
2
,
3
,
4
,
5
};
std
::
array
<
int
,
5
b
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"Before swap:"
<<
std
::
endl
;
std
::
cout
<<
"a: "
;
for
(
int
n
:
a
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"b: "
;
for
(
int
n
:
b
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Swap the two arrays
a.
swap
(
b
)
;
std
::
cout
<<
"\nAfter swap:"
<<
std
::
endl
;
std
::
cout
<<
"a: "
;
for
(
int
n
:
a
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"b: "
;
for
(
int
n
:
b
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
交换前:
a: 1 2 3 4 5
b: 10 20 30 40 50
After swap:
a: 10 20 30 40 50
b: 1 2 3 4 5
Code analysis:
a.swap(b)swaps the contents of a and b.- After swapping, a contains the original values of b, and b contains the original values of a.
Example 2: Using std::swap
You can also use std::swap to swap two arrays.
Example
#include
#include
#include
int
main
(
)
{
std
::
array
<
int
,
3
arr1
=
{
1
,
2
,
3
};
std
::
array
<
int
,
3
arr2
=
{
4
,
5
,
6
};
std
::
cout
<<
"Before swap: "
<<
std
::
endl
;
std
::
cout
<<
"arr1: "
;
for
(
int
n
:
arr1
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Use std::swap
std
::
swap
(
arr1, arr2
)
;
std
::
cout
<<
"After swap: "
<<
std
::
endl
;
std
::
cout
<<
"arr1: "
;
for
(
int
n
:
arr1
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
交换前:
arr1: 1 2 3
After swap:
arr1: 4 5 6
Code analysis:
std::swap(arr1, arr2)can also swap two arrays.- The effect is the same as calling the member function
swap.
Example 3: Reversing an array
can use swap to reverse an array.
Example
#include
#include
#include
int
main
(
)
{
std
::
array
<
int
,
5
numbers
=
{
1
,
2
,
3
,
4
,
5
};
std
::
cout
<<
"Before reverse: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Use two pointers to swap and reverse
auto
left
=
numbers.
begin
(
)
;
auto
right
=
numbers.
end
(
)
1
;
while
(
left
<
right
)
{
std::
swap
(
left,
right
)
;
++
left
;
--
right
;
}
std
::
cout
<<
"After reverse: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
反转前: 1 2 3 4 5
反转后: 5 4 3 2 1
Code analysis:
- uses head and tail pointers and
std::swapto reverse the array. - This is a manually implemented reversal algorithm.
Example 4: Clearing an array
can be swapped with an empty array to achieve a clearing effect (although array size is fixed, its contents can be swapped).
Example
#include
#include
int
main
(
)
{
std
::
array
<
int
,
5
data
=
{
1
,
2
,
3
,
4
,
5
};
std
::
array
<
int
,
5
empty
=
{
};
std
::
cout
<<
"Before swap - data: "
;
for
(
int
n
:
data
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Swap with an empty array
data.
swap
(
empty
)
;
std
::
cout
<<
"After swap - data: "
;
for
(
int
n
:
data
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
交换前 - data: 1 2 3 4 5
交换后 - data: 0 0 0 0 0
Code analysis:
- Since array size is fixed and elements cannot truly be deleted, you can swap with an all-zero array to 'clear' the contents.
- This may be useful in some scenarios.
The C++ container class