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 ...
随机推荐
- 分享一些 Java 后端的个人干货
学习 Java 也有了不少时间,入 Java 后台的坑也有了一段时日.这段时间里,听过许多前辈的经验与分享,也看过许多大佬的文章和作品.找了个时间整理和总结了一下我个人到目前为止一路以来的听到看到或者 ...
- 【explain】MySQL联表查询中的驱动表
写在前面 1.不要求每个人一定理解 联表查询(join/left join/inner join等)时的mysql运算过程 2.不要求每个人一定知道线上(现在或未来)哪张表数据量大,哪张表数据量小 3 ...
- 0111mysql如何选择Join的顺序
本文通过一个案例来看看MySQL优化器如何选择索引和JOIN顺序.表结构和数据准备参考本文最后部分"测试环境".这里主要介绍MySQL优化器的主要执行流程,而不是介绍一个优化器的各 ...
- C++异常与析构函数及构造函数
析构函数不要抛出异常. 构造函数可以抛出异常,但是要谨慎. 原因下面这篇文章讲的不错,转载如下: http://jarfield.iteye.com/blog/811703 写Java代码的时候,遇到 ...
- thrift 版本不一致导致 @Override 报错
thrift 版本不一致导致 @Override 报错 学习了:http://blog.csdn.net/antony1776/article/details/78920888 版本不一致导致的: 在 ...
- Spring进阶之路(10)-Advice简单介绍以及通过cglib生成AOP代理对象
Advice简单介绍 1. Before:在目标方法运行之前运行织入.假设Before的处理中没有进行特殊的处理.那么目标方法终于会运行,可是假设想要阻止目标方法运行时.能够通过抛出一个异常来实现.B ...
- hdu 4997 Biconnected
这题主要是计算连通子图的个数(c)和不连通子图的个数(dc)还有连通度为1的子图的个数(c1)和连通度为2以上的子图的个数(c2)之间的转化关系 主要思路大概例如以下: 用状态压缩的方法算出状态为x的 ...
- poj--3159--Candies(简单差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 26888 Accepted: 7398 Descrip ...
- 杂项-项目管理:WBS(工作分解结构)
ylbtech-杂项-项目管理:WBS(工作分解结构) WBS:工作分解结构(Work Breakdown Structure) 创建WBS:创建WBS是把项目 交付成果和项目工作分解成较小的,更易于 ...
- HIT Software Construction Lab 3
2019年春季学期 计算机学院<软件构造>课程 Lab 3实验报告 姓名 刘帅 学号 班号 1703008 电子邮件 1609192321@qq.com 手机号码 目录 1 实验目标概 ...