valarray swap() function in c++ Last Updated : 23 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The swap() function is defined in valarray header file. This function is used to swap the content of one valarray with another valarray. Syntax: void swap( valarray& valarray2 ); Parameter: This method accepts a parameter valarray2 which represents the another valarray with which we have to swap the old one. Returns: This function doesn't returns anything. Below programs illustrate the above function: Example 1:- CPP // C++ program to demonstrate // example of swap() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing 1st valarray valarray<int> varr1 = { 12, 24, 36, 48 }; // Initializing 2nd valarray valarray<int> varr2 = { 20, 40, 60, 80 }; varr1.swap(varr2); // Displaying valarrays after swapping cout << "The contents of 1st valarray " "after swapping are : "; for (int& x : varr1) cout << x << " "; cout << endl; cout << "The contents of 2nd valarray " "after swapping are : "; for (int& x : varr2) cout << x << " "; cout << endl; return 0; } Output: The contents of 1st valarray after swapping are : 20 40 60 80 The contents of 2nd valarray after swapping are : 12 24 36 48 Example 2:- CPP // C++ program to demonstrate // example of swap() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing 1st valarray valarray<int> varr1 = { -12, -24, -36, -48 }; // Initializing 2nd valarray valarray<int> varr2 = { 20, 40, 60, 80 }; varr1.swap(varr2); // Displaying valarrays after swapping cout << "The contents of 1st valarray " "after swapping are : "; for (int& x : varr1) cout << x << " "; cout << endl; cout << "The contents of 2nd valarray " "after swapping are : "; for (int& x : varr2) cout << x << " "; cout << endl; return 0; } Output: The contents of 1st valarray after swapping are : 20 40 60 80 The contents of 2nd valarray after swapping are : -12 -24 -36 -48 Comment More infoAdvertise with us Next Article valarray swap() function in c++ B bansal_rtk_ Follow Improve Article Tags : Misc C++ Programs C++ CPP-Functions cpp-valarray +1 More Practice Tags : CPPMisc Similar Reads valarray resize() function in C++ The resize() function is defined in valarray header file. This function resizes the valarray to contain n elements and assigns value to each element.Syntax: void resize( size_t n, T value = T() ); Parameter: This method accepts two parameters: n: It represents the new size of valarray.value: It repr 2 min read cshift() function for valarray in C++ The cshift() function is defined in valarray header file. This function returns a new valarray of the same size with elements whose positions are shifted circularly by n elements. If n is negative, right-shift is applied, if n is positive left-shift is applied. Syntax: valarray cshift (int n) const; 2 min read valarray shift() in C++ The shift() function is defined in valarray header file. This function returns a new valarray of the same size with elements whose positions are shifted by n elements. If n is negative, right-shift is applied, if n is positive left-shift is applied. Syntax: valarray shift (int n) const; Parameter: T 2 min read Swap Two Numbers using Function in C++ Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass t 3 min read Swap Two Numbers Without Third Variable in C++ In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two 2 min read Like