博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swapping variables.
阅读量:4138 次
发布时间:2019-05-25

本文共 2334 字,大约阅读时间需要 7 分钟。

 

 

#define swap(a,b) { /

        (a) ^= (b);     /
        (b) ^= (a);     /
        (a) ^= (b);     /
    }

 

#define swap(a,b) {  /

        (a) += (b);      /
        (b) = (a) - (b); /
        (a) -= (b);      /
    }

 

 

More slick expression

#define swap(a,b) { a ^= b ^= a ^= b; } But here http://c-faq.com/cpp/swapmacro.html. It seems it is not a good way to use a macro . ------------------------------------------------------------------------------------------------------------------------------------------------ There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. (See questions  and .) If the macro is intended to be used on values of arbitrary type (the usual goal), any solution involving a temporary variable is problematical, because:
  • It's hard to give the temporary a name that won't clash with anything. (Any name you pick might be the actual name of one of the variables being swapped. If you tried using ## to concatenate the names of the two actual arguments, to ensure that it won't match either one, it might still not be unique if the concatenated name is longer than 31 characters, and it wouldn't let you swap things like a[i] that aren't simple identifiers. You could probably get away with using a name like _tmp in the ``no man's land'' between the user and implementation namespaces; see question .)
  • Either it can't be declared with the right type (because standard C does not provide a typeof operator), or (if it copies objects byte-by-byte, perhaps with memcpy , to a temporary array sized with sizeof ) the macro can't be used on operands which are declared register .

The best all-around solution is probably to forget about using a macro, unless you're willing to pass in the type as a third argument. (Also, if you're trying to swap entire structures or arrays, you probably want to exchange pointers instead.) If you're worried about the use of an ugly temporary, and know that your machine provides an efficient exchange instruction, convince your compiler vendor to recognize the standard three-assignment swap idiom in the optimization phase.

If you're consumed by a passionate desire to solve this problem once and for all, please reconsider; there are better problems worthier of your energies.

转载地址:http://nhmvi.baihongyu.com/

你可能感兴趣的文章
Catalan 卡特兰数数的分析和应用
查看>>
数组指针 指针数组
查看>>
微软等面试100题系列--(1-20)
查看>>
上排给出十个数,在其下排填出对应的十个数
查看>>
微软等面试100题系列--(21-40)
查看>>
二叉树中节点的最大距离
查看>>
微软等面试100题系列--(41-60)
查看>>
poj 1011 回溯+剪枝 木棒问题
查看>>
深刻理解Linux进程间通信(IPC)
查看>>
2013阿里巴巴实习笔试题 最后两题 明星问题+仓库运货
查看>>
最长递减子序列--动态规划
查看>>
最长公共子序列--动态规划
查看>>
rand7()构造rand10()
查看>>
异或运算 ^ 变量交换及找出现一次的数
查看>>
微软等面试100题系列--(61-80)
查看>>
在字符串中删除特定的字符
查看>>
求质数算法的N种境界 (N > 10)
查看>>
复杂链表的复制
查看>>
主存与Cache的地址映射
查看>>
socket编程
查看>>