本系列文章由 @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的更多相关文章

  1. Swap Without Extra Variable

    Given two variables, x and y, swap two variables without using a third variable.   Example Given x = ...

  2. memory ordering 内存排序

    Memory ordering - Wikipedia https://en.wikipedia.org/wiki/Memory_ordering https://zh.wikipedia.org/w ...

  3. MlLib--逻辑回归笔记

    批量梯度下降的逻辑回归可以参考这篇文章:http://blog.csdn.net/pakko/article/details/37878837 看了一些Scala语法后,打算看看MlLib的机器学习算 ...

  4. SparkMLlib之 logistic regression源码分析

    最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...

  5. 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 ...

  6. Basic Sort Algorithms

    1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...

  7. 图之单源Dijkstra算法、带负权值最短路径算法

    1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...

  8. Java:foreach实现原理

    第一部分: For-each Loop Purpose The basic for loop was extended in Java5 to make iteration over arrays a ...

  9. MLlib之LR算法源码学习

    /** * :: DeveloperApi :: * GeneralizedLinearModel (GLM) represents a model trained using * Generaliz ...

随机推荐

  1. selenium2+java切换窗口

    package exercises; import java.util.ArrayList; import java.util.List; import java.util.Set; import o ...

  2. Linux常用命令last的使用方法详解

    http://www.jb51.net/article/120140.htm 最近在学习linux命令,学习到了last命令,发现很多同学对last命令不是很熟悉,last命令的功能列出目前与过去登入 ...

  3. TNS-12557: TNS:protocol adapter not loadable TNS-12560: TNS:protocol adapter error

    Description: Oracle 10.2 on hpux 11.23 PA. When i try to start listener i go the next errors: Error ...

  4. BA-协议-BACnet 协议优势简析

    BACnet - Building Automation and Control Network 的简称,为楼宇自控网络制定 的网络和通讯协议 .由美国暖通空调工程师协会主导制定的开放的楼宇自控通讯标 ...

  5. HDU 1788

    必须MARK下:任何时候都要保持清醒头脑,不要被题目绕了.. 其实就是求最小公倍数. #include <iostream> #include <cstdio> #includ ...

  6. ubuntu解压命令全览(rar)

    sudo apt-get install p7zip-full Ubuntu下解压rar文件的方法 2010-05-13 12:47 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了 ...

  7. 使用神经网络-垃圾邮件检测-LSTM或者CNN(一维卷积)效果都不错【代码有问题,pass】

    from sklearn.feature_extraction.text import CountVectorizer import os from sklearn.naive_bayes impor ...

  8. 5.文件I/O

    1 C标准函数与系统函数的区别 文件的结构体: 1.1 I/O缓冲区 每一个FILE文件流都有一个缓冲区buffer,默认大小8192Byte. 1.2 效率 文件缓冲区会降低效率.这里提供缓冲区主要 ...

  9. kubernetes系列:(一)、kubeadm搭建kubernetes(v1.13.1)单节点集群

    kubeadm是Kubernetes官方提供的用于快速部署Kubernetes集群的工具,本篇文章使用kubeadm搭建一个单master节点的k8s集群. 节点部署信息 节点主机名 节点IP 节点角 ...

  10. centos7 usually use

    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.22.103 port port=8 ...