int a{}, b{}, temp{};
cin >> a >> b;
//===================== METHOD-1
temp = a;
a = b;
b = temp;
//===================== METHOD-2 ( XOR ^ )
// example: a^b = 5^7
a = a ^ b; // 5^7
b = a ^ b; // 5 ^ 7 ^ 7 //5 ( 7 & 7 dismissed)
a = a ^ b; // 5 ^ 7 ^ 5 //7 ( 5 & 5 dismissed)
//===================== METHOD-3 ( swap() )
swap(a, b);
cout << "a " << a << endl;
cout << "b " << b << endl;
Input : set1 = {1, 2, 3, 4}
set2 = {5, 6, 7, 8}
set1.swap(set2);
Output : set1 = {5, 6, 7, 8}
set2 = {1, 2, 3, 4}
Input : set1 = {'a', 'b', 'c', 'd'}
set2 = {'w', 'x', 'y', 'z'}
set1.swap(set2);
Output : set1 = {'w', 'x', 'y', 'z'}
set2 = {'a', 'b', 'c', 'd'}
int main()
{
int a = 5;
int b = 10;
swap(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int a, b;
cout << "Input first number: ";
cin >> a;
cout << "Input second number: ";
cin >> b;
swap (a, b);
cout << "After swapping the first number: " << a << endl;
cout << "After swapping the second number: " << b << endl;
Code Example |
---|
Cpp :: remove whitespace in cpp |
Cpp :: c++ memset |
Cpp :: unordered map c++ |
Cpp :: size of unordered_set |
Cpp :: C++ Nested if |
Cpp :: copy assignment operator in c++ |
Cpp :: c++ preprocessor commands |
Cpp :: what algorithm does bitcoin use |
Cpp :: stl map remove item |
Cpp :: for_each c++ |
Cpp :: Basic Makefile C++ |
Cpp :: memcpy in cpp |
Cpp :: memset function in c++ |
Cpp :: vector size c++ |
Cpp :: bubble sort function in c++ |
Cpp :: c++ string example |
Cpp :: visual studio code terminal keeps closing c++ |
Cpp :: cpp serial print override always in same place |
Cpp :: string class in c++ |
Cpp :: end vs cend in cpp |
Cpp :: c++ solver online free |
Cpp :: fabs c c++ |
Cpp :: cpp how to add collisions to boxes |
Cpp :: switch cout print with a prameter c++ |
Cpp :: how to make a defaule conrstrocr in c++ classes |
Cpp :: c++ over load oprator to print variable of clas |
Cpp :: case 1 or 2 c++ |
Cpp :: c# unity rendering object |
Cpp :: fishes code in assignment expert |
Cpp :: c++ single comment |