swap c++ code example
Example 1: what is time complexity of swap function
The Behaviour is Equivalent to:
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
Syntax : Swap(a,b)
Time Complexity: It makes one constructions and one assignments
So, Linear O(n) time.
Example 2: swap in cpp
int a{}, b{}, temp{};
cin >> a >> b;
temp = a;
a = b;
b = temp;
a = a ^ b;
b = a ^ b;
a = a ^ b;
swap(a, b);
cout << "a " << a << endl;
cout << "b " << b << endl;
Example 3: what did swap method return in c++
Return Value: The function does not return anything, it swaps the values of the two variables
Example 4: c++ swap function
int main()
{
int a = 5;
int b = 10;
swap(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}