Swap ()

Discussion in 'C++' started by andrew.smith.cpp@gmail.com, Apr 19, 2008.

  1. Guest

    Hello All :)
    what is the right way to swap two values without using Temp (Or
    Third variable).
    1.) to use "^" operator
    2.) to use this

    a=a+b;
    b=a-b;
    a=a-b;


    Which is the right Way ?????
    , Apr 19, 2008
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hello All :)
    > what is the right way to swap two values without using Temp (Or
    > Third variable).


    std::swap(a, b);
    Rolf Magnus, Apr 19, 2008
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > Hello All :)
    > what is the right way to swap two values without using Temp (Or
    > Third variable).
    > 1.) to use "^" operator
    > 2.) to use this
    >
    > a=a+b;
    > b=a-b;
    > a=a-b;
    >
    >
    > Which is the right Way ?????


    3. None of the above.

    Consider:


    void swap(int& a, int&b)
    {
    a=a+b;
    b=a-b;
    a=a-b;
    }

    int main()
    {
    int x = 7;
    swap(x, x);
    }
    red floyd, Apr 19, 2008
    #3
  4. James Kanze Guest

    On 19 avr, 11:30, wrote:

    > what is the right way to swap two values without using Temp


    There isn't one.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 19, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hadAnet
    Replies:
    2
    Views:
    4,342
    hadAnet
    Dec 28, 2005
  2. Abraham Khalil

    Swap Tree Nodes

    Abraham Khalil, Sep 18, 2003, in forum: Java
    Replies:
    3
    Views:
    3,387
    Abraham Khalil
    Sep 19, 2003
  3. Bill Reyn

    swap method in Java

    Bill Reyn, Jun 22, 2004, in forum: Java
    Replies:
    22
    Views:
    105,828
    yanipot
    Jan 24, 2012
  4. Madhur Ahuja

    implementing swap for integers in java

    Madhur Ahuja, Aug 8, 2004, in forum: Java
    Replies:
    12
    Views:
    1,387
    Michael Borgwardt
    Aug 10, 2004
  5. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    976
    Niels Dekker (no reply address)
    Jul 20, 2006
Loading...

Share This Page