Std Swap C++ Code Example


Example: 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;

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable