C/C++ Swap without using extra variable
本系列文章由 @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
}
C/C++ Swap without using extra variable的更多相关文章
- Swap Without Extra Variable
Given two variables, x and y, swap two variables without using a third variable. Example Given x = ...
- memory ordering 内存排序
Memory ordering - Wikipedia https://en.wikipedia.org/wiki/Memory_ordering https://zh.wikipedia.org/w ...
- MlLib--逻辑回归笔记
批量梯度下降的逻辑回归可以参考这篇文章:http://blog.csdn.net/pakko/article/details/37878837 看了一些Scala语法后,打算看看MlLib的机器学习算 ...
- SparkMLlib之 logistic regression源码分析
最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- Basic Sort Algorithms
1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...
- 图之单源Dijkstra算法、带负权值最短路径算法
1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...
- Java:foreach实现原理
第一部分: For-each Loop Purpose The basic for loop was extended in Java5 to make iteration over arrays a ...
- MLlib之LR算法源码学习
/** * :: DeveloperApi :: * GeneralizedLinearModel (GLM) represents a model trained using * Generaliz ...
随机推荐
- spring和struts整合
整合准备:导入jar包 如果只是访问action,没有做数据库方面的操作的话 只需要导入下面的jar spring相关jar 以及struts相关jar包 整合过程: 用到了struts所以需要在we ...
- GROUP BY 和 ORDER BY 的一起使用
GROUP BY 和 ORDER BY一起使用 写程序也有很长的一段时间了,有些东西我总不曾去思考,很少去积累一些有用的东西,总喜欢"用要即拿"的心态来对待,这是非常不好的坏习惯. ...
- Django学习笔记(一)——安装,创建项目,配置
疯狂的暑假学习之 Django学习笔记(一) 教材 书<The Django Book> 视频:csvt Django视频 1.创建项目 django‐admin.py startpro ...
- ios OpenCv的配置和人脸识别技术
作为一个好奇心非常重的人,面对未知的世界都想去一探到底. 于是做了个人脸识别的demo. 眼下国内的关于opencv技术文章非常少.都是互相抄袭.关键是抄个一小部分还不全.时间又是非常久之前的了,和如 ...
- mysqli数据库操作简单实例
mysqli数据库操作简单实例 代码 结果
- Oracle的awr和ash
1. 10g之前 用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接断开后,其原来的连接信息在v$sess ...
- linux下解决非本机访问出现的提示
安装完毕之后发现无法用IP访问,出现以下提示 Access forbidden! New XAMPP security concept: Access to the requested directo ...
- Flask-Restful
定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...
- Codeforces 232E - Quick Tortoise bitset+分治
题意: 思路: //By SiriusRen #include <cstdio> #include <bitset> #include <vector> using ...
- 如何解决“因为计算机中丢失php_mbstring.dll”
配置编译环境时,php.exe报系统错误,无法启动此程序,因为计算机中丢失php_mbstring.dll. 在C:\Windows找到php.ini文件,ctrl+f找到extension=php_ ...