C/C++ Swap without using extra variable
来源:互联网 发布:pdf阅读器源码 编辑:程序博客网 时间:2024/05/16 05:19
本系列文章由 @YhL_Leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/50255379
对于可以线性运算的变量,交换两个变量值的做法,通常我们是这样的:
/*** Swap the parameters with a temp variable.* @param a The first parameter.* @param a The second parameter.*/void swap(int& a, int& b){ int temp = a; a = b; b = temp;}
稍作变化,就可以不通过临时变量实现:
/*** Swap the parameters without a temp variable.* Warning! Susceptible to overflow/underflow.* @param a The first parameter.* @param a The second parameter.*/void swapNoTemp(int& a, int& b){ a -= b; // a = a - b b += a; // b = b + (a - b), b gets the original value of a a = (b - a); // a = a - (a - b), a gets the original value of b}
1 0
- C/C++ Swap without using extra variable
- Exchange the values of two variables without using an extra variable
- How to swap two numbers without using temp or third variable
- swap two values without temporary variable
- Swap values of two variables without extra space
- C - Extra Krunch
- C Variable
- Functions with Variable Argumet lists in C using va_list
- C swap
- load the C runtime library without using a manifest
- load the C runtime library without using a manifest
- C++: Compile error: extra qualification
- C++: Compile error: extra qualification
- c#using
- C#using
- CF#450 C.Remove Extra One
- codeforces 900C. Remove Extra One
- Codeforces 900C-Remove Extra One
- C# Socket与实现
- 数组中只出现一次的数字
- [UVALive 6693]Flow Game计算几何,线代相交
- java解惑--谜题13:畜牧场
- 第四章:UML序列图
- C/C++ Swap without using extra variable
- 看了周忆博客之后的感想
- 乱码注意事项
- 和为S的连续正数序列
- BASE64Decoder小解
- 和为s的两个数字
- 11. food_query.py python数据库查询
- python学习日记_第三天(ex6~8)
- glog的使用