YouTip LogoYouTip

Cpp Libs Array Swap

[![Image 1: C++ Container Class ](#) C++ Container Class ](#) * * * `swap` is a function in array used to **swap the contents of two arrays**, and is a very useful tool. `swap` is a member function of the container class, used to **swap 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 "exchange", i.e., exchanging the contents of two arrays. * * * ## Basic Syntax and Parameters `swap` is a member function of the container class, requiring another array as a parameter. ### Syntax Format void swap(array& other) noexcept; ### Parameter Description * **Parameter**: `other` * Type: `array&` (reference of the same type) * Description: Another array to swap contents with. 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**: `swap` is usually declared as `noexcept`, meaning it will not throw exceptions. * * * ## Examples Let's thoroughly master the usage of `swap` through a series of examples. ### Example 1: Basic Usage - Swapping Two Arrays ## Example #include #include int main(){ std::array a ={1, 2, 3, 4, 5}; std::array 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:** Before swap: a: 1 2 3 4 5 b: 10 20 30 40 50After swap: a: 10 20 30 40 50 b: 1 2 3 4 5 **Code Analysis:** 1. `a.swap(b)` swaps the contents of a and b. 2. 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 the `std::swap` function to swap two arrays. ## Example #include #include #include int main(){ std::array arr1 ={1, 2, 3}; std::array 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:** Before swap: arr1: 1 2 3After 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 You can use swap to implement array reversal. ## Example #include #include #include int main(){ std::array numbers ={1, 2, 3, 4, 5}; std::cout<<"Before reversal: "; 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 reversal: "; for(int n : numbers) std::cout<< n <<" "; std::cout<< std::endl; return 0; } **Expected Output:** Before reversal: 1 2 3 4 5After reversal: 5 4 3 2 1 **Code Analysis:** * Using head and tail pointers with `std::swap` 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 array size is fixed, contents can be swapped). ## Example #include #include int main(){ std::array data ={1, 2, 3, 4, 5}; std::array empty ={}; std::cout<<"Before swap - data: "; for(int n : data) std::cout<< n <<" "; std::cout<< std::endl; // Swap with 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:** Before swap - data: 1 2 3 4 5After swap - data: 0 0 0 0 0 **Code Analysis:** * Since array size is fixed, elements cannot be truly deleted, but swapping with a zero-initialized array can "clear" the contents. * This may be useful in certain scenarios. * * C++ Container Class ](#)
← Cpp Libs List Push_BackCpp Libs Array Operator β†’