Search
 
SCRIPT & CODE EXAMPLE
 

CPP

swap in cpp

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;
Comment

c++ swap function

int main()
{
	int a = 5;
	int b = 10;
  
	swap(a, b);
  
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}
Comment

C++ Swap Function

    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;
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ remove chars from string 
Cpp :: passing structure to function in c++ example 
Cpp :: c++ average vector 
Cpp :: toupper c++ 
Cpp :: how to replace part of string with new string c++ 
Cpp :: find function in c++ 
Cpp :: polymorphism in c++ 
Cpp :: bfs to detect cycle in undirected graph 
Cpp :: kmp algorithm c++ 
Cpp :: visual studio cpp compiler 
Cpp :: linked list cycle c++ 
Cpp :: qt make widget ignore mouse events 
Cpp :: c++ loop trhought object 
Cpp :: friend function in c++ 
Cpp :: use of strstr in c++ 
Cpp :: c++ get active thread count 
Cpp :: full implementation of binary search tree in C++ 
Cpp :: creating node in c++ 
Cpp :: c++ pass ofstream as argument 
Cpp :: opencv(4.5.1) c:usersappveyorappdatalocal emp1pip-req-build-kh7iq4w7opencvmodulesimgprocsrc esize.cpp:4051: error: (-215:assertion failed) !ssize.empty() in function 
Cpp :: if else in c++ 
Cpp :: Accessing C++ Array Elements 
Cpp :: erase range vector c++ 
Cpp :: why return 0 in int main 
Cpp :: How to generate all the possible subsets of a set ? 
Cpp :: Maximum element in a map c++ 
Cpp :: convert uppercase to lowercase 
Cpp :: problem category codechef solution in c++ 
Cpp :: c++ void poiinter 
Cpp :: taking integer input from file in c++ 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =