Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

Learn C++ Language

Utility Library swap() Function in C++ Programming Language

Utility Library swap() Function in C++
Exchange values of two objects. Exchanges the values of a and b. C++ Utility swap() function swaps or say interchanges the values of two containers under reference. The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables.
Syntax for Utility swap() Function in C++
#include <utility> //non-array (1) template <class T> void swap (T& a, T& b) noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value); //array (2) template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept (noexcept(swap(*a,*b)));
a, b
Two objects, whose contents are swapped. Type T shall be move-constructible and move-assignable (or have swap defined for it, for version (2)). This function does not return any value. Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version: Custom overloads of swap declared in the same namespace as the type for which they are provided get selected through argument-dependent lookup over this generic version.
Non-array: Constant: Performs exactly one construction and two assignments (although notice that each of these operations works on its own complexity). Array: Linear in N: performs a swap operation per element.
Data races
Both a and b are modified.
Throws if the construction or assignment of type T throws. Never throws if T is nothrow-move-constructible and nothrow-move-assignable. Note that if T does not fulfill the requirements specified above (in parameters), it causes undefined behavior.
/* std::swap() is a built-in function in C++'s Standard Template Library. The function takes two values as input and swaps them. */ /* Exchange values of two objects by swap() function code example */ #include <utility> #include <iostream> using namespace std; int main() { int even[] = {2, 4, 6, 8, 10}; int odd[] = {1, 3, 5, 7, 9}; // before std::cout << "odd: "; for(int x: odd) { std::cout << x << ' '; } std::cout << '\n'; std::cout << "even: "; for(int x: even) { std::cout << x << ' '; } std::cout << "\n\n"; // swapping arrays odd and even swap(odd, even); // after std::cout << "odd: "; for(int x: odd) { std::cout << x << ' '; } std::cout << '\n'; std::cout << "even: "; for(int x: even) { std::cout << x << ' '; } std::cout << '\n'; }

To check whether the input alphabet is vowel or not a vowel in C++, Enter a Character, then check the character for Vowel. The character is vowel, only if it's equal to a, A, e, E, i, I, o, O