C++ <array> swap Function

C++ container class <array>


swapis used in array forSwap the contents of two arrays.The function is a very useful tool.

swapis a member function of the container class, used toswap the contents of two arrays.Both arrays must be of the same type and the same size.

swapIt provides an efficient way to exchange the contents of two containers, and is a common utility in algorithm and data structure implementations.

Word Definitions: swapIt means "swap", i.e., exchanging the contents of two arrays.


Basic Syntax and Parameters

swapIt is a member function of the container class and requires another array as a parameter.

Syntax format

void swap(array& other) noexcept;

Parameter description

  • Parameter: other
    • Type:array<T, N>&(references of the same type)
    • Description: Another array whose contents are to be swapped with. The two arrays must have the same element type and size.

Function description

  • Return Value: void(No return value).
  • EffectSwaps all elements of the two arrays. This is an O(n) operation and requires element-by-element swapping.
  • Note: swapUsually declared asnoexcept, indicating that no exception will be thrown.

Example

Let us thoroughly master this through a series of examples.swapUsage.

Example 1: Basic usage - swapping two arrays

Example

#include <iostream>
#include <array>

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 two arrays
    a.swap(b);

    std::cout << "\n"After 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

交换后:
a: 10 20 30 40 50
b: 1 2 3 4 5

Code analysis:

  1. a.swap(b)Swap the contents of a and b.
  2. After the swap, a contains the values originally in b, and b contains the values originally in a.

Example 2: Using std::swap

You can also usestd::swapfunction to swap two arrays.

Example

#include <iostream>
#include <array>
#include <algorithm>

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
交换后:
arr1: 4 5 6

Code analysis:

  • std::swap(arr1, arr2)You can also swap two arrays in the same way.
  • The effect is the same as calling the member functionswapSame.

Example 3: Reversing an array

You can use swap to reverse an array.

Example

#include <iostream>
#include <array>
#include <algorithm>

int main() {
    std::array<int, 5> numbers = {1, 2, 3, 4, 5};

    std::cout << "Before reversing: ";
    for(int n : numbers) std::cout << n << " ";
    std::cout << std::endl;

    // Use two pointers to reverse by swapping
    auto left = numbers.begin();
    auto right = numbers.end() - 1;

    while(left < right) {
        std::swap(*left, *right);
        ++left;
        --right;
    }

    std::cout << "After reversing: ";
    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:

  • Using head and tail pointers andstd::swapThis can achieve array reversal.
  • This is a manually implemented reversal algorithm.

Example 4: Clearing an array

You can swap with an empty array to achieve a clearing effect (although the array size is fixed, the contents can be swapped).

Example

#include <iostream>
#include <array>

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 the array size is fixed, elements cannot truly be deleted, but you can swap with an all-zero array to "clear" the contents.
  • This may be useful in some scenarios.

C++ container class <array>

other extensions