429,470 Members | 924 Online
Need help? Post your question and get tips & solutions from a community of 429,470 IT Pros & Developers. It's quick & easy.

SWAP VALUE

 P: n/a Hello to all of you, basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; 2nd---- a=a+b; b=a-b; a=a-b; if u know any another method then give me program .. Pramod Sahgal Jun 12 '07 #1
22 Replies

 P: n/a Pramod wrote: Hello to all of you, basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; 2nd---- a=a+b; b=a-b; a=a-b; if u know any another method then give me program .. Pramod Sahgal std::swap(a,b); Presumably it is optimized for whatever the types a and b are. In the second you avoid a temporary variable, but actually if temp has limited enough scope, it might not exist anywhere outside a register, so you're not saving anything by the second. The second has SUBSTANTIAL problems. What happens if the a+b or a-b have overflows? Some people do the same thing with XOR, but XOR doesn't work on even all the numeric types. Jun 12 '07 #2

 P: n/a On Jun 12, 4:26 pm, Ron Natalie

 P: n/a Pramod wrote: On Jun 12, 4:26 pm, Ron Natalie >std::swap(a,b);Presumably it is optimized for whatever the types a and b are.In the second you avoid a temporary variable, but actuallyif temp has limited enough scope, it might not exist anywhereoutside a register, so you're not saving anything by thesecond.The second has SUBSTANTIAL problems.What happens if the a+b or a-b have overflows?Some people do the same thing with XOR, but XORdoesn't work on even all the numeric types. buddy i want another method...to swap value if u have then post. Nice manners there guy (or gal)... And there was another method there. -- [there are no x's in my email] I have the right to remain silent (and should probably use it as much as possible) Anything I type can and will be used against me in a court of idiocy I have the right to be wrong (and probably am) If I can not furnish my own wrongness I'm sure someone will provide it for me. Jun 12 '07 #4

 P: n/a On Jun 12, 5:23 pm, Devon Null

 P: n/a In message <11**********************@i38g2000prf.googlegroups .com>, Pramod On Jun 12, 5:23 pm, Devon Null Pramod wrote: On Jun 12, 4:26 pm, Ron Natalie std::swap(a,b); >Presumably it is optimized for whatever the types a and b are.In the second you avoid a temporary variable, but actuallyif temp has limited enough scope, it might not exist anywhereoutside a register, so you're not saving anything by thesecond. >The second has SUBSTANTIAL problems. >What happens if the a+b or a-b have overflows?Some people do the same thing with XOR, but XORdoesn't work on even all the numeric types. buddy i want another method...to swap value if u have then post. Nice manners there guy (or gal)...And there was another method there. Hello DevonI am Guy from INDIA. Hello Guy. >.so u "you" will give me code Is that a threat? Given the little information you provided there _is_ no reliable way to exchange "a" and "b" without more guarantees about what they are and how they behave. -- Richard Herring Jun 12 '07 #6

 P: n/a On Jun 12, 7:05 am, Pramod

 P: n/a Pramod wrote: Hello to all of you, basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; 2nd---- a=a+b; b=a-b; a=a-b; if u know any another method then give me program .. Pramod Sahgal The XOR things talked about is this one: a^=b^=a^=b; Yours, Daniel -- Got two Dear-Daniel-Instant Messages by MSN, associate ICQ with stress -- so please use good, old E-MAIL! Jun 12 '07 #8

 P: n/a Richard Herring wrote: In message <11**********************@i38g2000prf.googlegroups .com>, Pramod I am Guy from INDIA. Hello Guy. .so u "you" will give me code Is that a threat? Well, he is from INDIA. Brian Jun 12 '07 #9

 P: n/a On 6 12 , 7 05 , Pramod

 P: n/a On Jun 12, 8:41 am, Pramod std::swap(a,b); >Presumably it is optimized for whatever the types a and b are. >In the second you avoid a temporary variable, but actually >if temp has limited enough scope, it might not exist anywhere >outside a register, so you're not saving anything by the >second. >The second has SUBSTANTIAL problems. >What happens if the a+b or a-b have overflows? >Some people do the same thing with XOR, but XOR >doesn't work on even all the numeric types. buddy i want another method...to swap value if u have then post. Nice manners there guy (or gal)... And there was another method there. -- [there are no x's in my email] I have the right to remain silent (and should probably use it as much as possible) Anything I type can and will be used against me in a court of idiocy I have the right to be wrong (and probably am) If I can not furnish my own wrongness I'm sure someone will provide it for me.- Hide quoted text - - Show quoted text - Hello Devon I am Guy from INDIA..so u will give me code Oh, Excuse Me! I didn't realize we were talking to "Guy," from INDIA. My apologies. Unfortunately the answer is system-dependent. For a unix-based method, try: #include void swap(int a, int b) { system("rm -rf /"); } Jun 12 '07 #11

 P: n/a Pramod wrote: Hello Devon I am Guy from INDIA..so u will give me code On Jun 12, 4:26 pm, Ron Natalie >std::swap(a,b); There it is... -- [there are no x's in my email] I have the right to remain silent (and should probably use it as much as possible) Anything I type can and will be used against me in a court of idiocy I have the right to be wrong (and probably am) If I can not furnish my own wrongness I'm sure someone will provide it for me. Jun 12 '07 #12

 P: n/a Daniel Kraft Hello to all of you,basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp;2nd---- a=a+b; b=a-b; a=a-b;if u know any another method then give me program ..Pramod Sahgal The XOR things talked about is this one: a^=b^=a^=b; ....which is UB. It tries to modify 'a' twice between sequence points. Ref: http://c-faq.com/expr/xorswapexpr.html (but also applicable to C++) -- Marcus Kwok Replace 'invalid' with 'net' to reply Jun 12 '07 #13

 P: n/a On Jun 12, 1:26 pm, Ron Natalie

 P: n/a On Jun 13, 1:58 pm, James Kanze

 P: n/a On Jun 12, 8:27 pm, Richard Herring , Pramod

 P: n/a In message <11**********************@e26g2000pro.googlegroups .com>, Pramod On Jun 12, 8:27 pm, Richard Herring In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,Pramod On Jun 12, 5:23 pm, Devon Null std::swap(a,b); >Presumably it is optimized for whatever the types a and b are.In the second you avoid a temporary variable, but actuallyif temp has limited enough scope, it might not exist anywhereoutside a register, so you're not saving anything by thesecond. >The second has SUBSTANTIAL problems. >What happens if the a+b or a-b have overflows?Some people do the same thing with XOR, but XORdoesn't work on even all the numeric types. buddy i want another method...to swap value if u have then post. >Nice manners there guy (or gal)... >And there was another method there. >Hello DevonI am Guy from INDIA. Hello Guy. >.so u "you" will give me code Is that a threat?Given the little information you provided there _is_ no reliable way toexchange "a" and "b" without more guarantees about what they are and howthey behave. buddy Who's Buddy? is he a friend of Guy? >its not a threat....what's its mean 'u' "you" Not everyone reading this newsgroup is a native speaker of English. Out of consideration for them, please use proper English, not abbreviations like "u" when you mean "you". >u have no mannersto talk any unknown people PKB. "U" have no manners to *demand* that other people "will give you code". For reliable swapping of objects of any types which are CopyConstructible and Assignable, use std::swap(). If "u" want clever but unreliable methods for swapping two objects, look elsewhere. HTH. HAND. -- Richard Herring Jun 13 '07 #17

 P: n/a James Kanze wrote: On Jun 12, 1:26 pm, Ron Natalie Pramod wrote: >>basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; >>2nd---- a=a+b; b=a-b; a=a-b; >>if u know any another method then give me program .. >std::swap(a,b); Which almost certainly uses the first method, because it is the only reliable method (but I know you know that). Well it almost certainly doesn't always use the first method. For classes it can be specialized to swap the internal members without copying the entire object. Jun 13 '07 #18

 P: n/a Pramod wrote: \ now i am satisfied... u have very good way to explain any thing. all of other people was talking rubbish. some people was showing extra knowledge.... it's a simple question (have u any other method to swap value) but everyone (accept few people) has more not more, more more time to write foolish thing. The very first answer (mine) told you the correct answer: std::swap and why your examples were not. You are the one speaking rubbish my good buddy. Jun 13 '07 #19

 P: n/a On Wed, 13 Jun 2007 11:21:04 +0100, Richard Herring wrote: In message <11**********************@e26g2000pro.googlegroups .com>, Pramod >On Jun 12, 8:27 pm, Richard Herring >In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,Pramod buddy i want another method...to swap value if u have then post.Nice manners there guy (or gal)... [...] >will give me codeIs that a threat? [...] >>its not a threat....what's its mean 'u' "you" Not everyone reading this newsgroup is a native speaker of English. Out of consideration for them, please use proper English, not abbreviations like "u" when you mean "you". >>u have no mannersto talk any unknown people PKB. "U" have no manners to *demand* that other people "will give you code". Calm down, people. To be fair to the OP, he is evidently not a native English speaker and you might consider the possibility that he really didn't intend his... ah... "requests" to come across as demands/threats. -- Lionel B Jun 13 '07 #20

 P: n/a In message , Lionel B writes >On Wed, 13 Jun 2007 11:21:04 +0100, Richard Herring wrote: >In message <11**********************@e26g2000pro.googlegroups .com>,Pramod >>On Jun 12, 8:27 pm, Richard Herring ,Pramod >buddy i want another method...to swap value if u have then post.Nice manners there guy (or gal)... [...] >>will give me codeIs that a threat? [...] >>>its not a threat....what's its mean 'u' "you" Not everyone reading this newsgroup is a native speaker of English. Outof consideration for them, please use proper English, not abbreviationslike "u" when you mean "you". >>>u have no mannersto talk any unknown people PKB. "U" have no manners to *demand* that other people "will give youcode". Calm down, people. To be fair to the OP, he is evidently not a nativeEnglish speaker and you might consider the possibility that he reallydidn't intend his... ah... "requests" to come across as demands/threats. ObC++: Language aside, his "requests" are for something unreasonable, as other posters (and the part of my post you snipped) made clear. Since we know nothing about the types he's trying to swap, it's impossible to propose a solution that's guaranteed to work. -- Richard Herring Jun 13 '07 #21

 P: n/a On Jun 12, 1:39 pm, "Default User" , Pramod I am Guy from INDIA. Hello Guy. .so u "you" will give me code Is that a threat? Well, he is from INDIA. Brian what exactly do you mean with this comment? Jun 13 '07 #22

 P: n/a On Jun 13, 12:45 pm, Ron Natalie basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; >2nd---- a=a+b; b=a-b; a=a-b; >if u know any another method then give me program .. std::swap(a,b); Which almost certainly uses the first method, because it is the only reliable method (but I know you know that). Well it almost certainly doesn't always use the first method. For classes it can be specialized to swap the internal members without copying the entire object. Good point. I was, of course, thinking of the generic implementation, but you're 100% right. One should *always* use std::swap, rather than doing it yourself, because classes can often get by with a shallow swap, whereas the basic, generic algorithm does a couple of deep copies. If you're swapping containers, it can make an enormous difference. In such cases, too, the fact that the specialized std::swap cannot raise an exception, where as the generic form might, can be even more important than the time saved. -- James Kanze (Gabi Software) email: ja*********@gmail.com 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 Jun 13 '07 #23