原始版本:

template<typename T>
void swap(T& a, T& b)
{
T tmp(a);
a = b;
b = tmp;
}

此版本不重视效率,当交换的两个对象比较大时,需要更高效的交换,因此应该提供1)public swap成员函数,让它高效的置换两个对象,并提供nono-member swap,调用之

///////////////////////////////////////////////////////////////////////////////
//
// FileName : swap_item25.h
// Version : 0.10
// Author : Ryan Han
// Date : 2013/07/26 13:13:55
// 2013/10/30 08:27:50
// Comment :
// ½«WidgetÉùÃ÷Ò»¸öswapµÄpublicº¯Êý×öÕæÕýµÄÖû»¹¤×÷£¬È»ºó½«std::swapÌØ»¯
/////////////////////////////////////////////////////////////////////////////// #ifndef _SWAP_ITEM25_H_
#define _SWAP_ITEM25_H_ #include <iostream>
using namespace std; class WidgetImpl {
public:
WidgetImpl(int a = , int b = , int c = );
/*WidgetImpl(int a = 1, int b = 2, int c = 3) : x(a), y(b), z(c){
cout << "WidgetImpl constructor called." << endl;
}*/ ~WidgetImpl(){
cout << "WidgetImpl de-constructor called." << endl;
} void WidgetPrint(){
cout << "x = " << x << " y = " << y << " z = "<< z << endl;
}
private:
int x, y, z;
}; class Widget {
public:
Widget(int a = , int b = , int c = ) : pImpl(new WidgetImpl(a, b, c)){
cout << "Widget constructor called." << endl;
;
} ~Widget(){
cout << "Widget de-constructor called." << endl;
delete pImpl;
} Widget(const Widget& rhs) {
pImpl = new WidgetImpl(*(rhs.pImpl));
} Widget& operator=(const Widget& rhs){
*pImpl = *(rhs.pImpl);
} void WidgetPrint(){
pImpl->WidgetPrint();
//non friend class can't access private data
//cout << (*pImpl).x << endl;
} //has to use because only member function could access private member pImpl
void swap(Widget& other){
using std::swap;
swap(pImpl, other.pImpl);
}
private:
WidgetImpl* pImpl;
}; //inline to avoid duplicate definition
//http://www.cnblogs.com/dracohan/p/3401660.html
namespace std {
template <>
inline void swap<Widget>(Widget& a, Widget& b){
cout << "specialized swap was called" << endl;
a.swap(b);
}
} #endif
///////////////////////////////////////////////////////////////////////////////
//
// FileName : swap_item25.cpp
// Version : 0.10
// Author : Ryan Han
// Date : 2013/07/26 13:13:55
// 2013/10/30 08:27:50
// Comment :
//
/////////////////////////////////////////////////////////////////////////////// #include "swap_item25.h"
#include <iostream>
using namespace std; WidgetImpl::
WidgetImpl(int a, int b, int c) : x(a), y(b), z(c){
cout << "WidgetImpl constructor called." << endl;
}
///////////////////////////////////////////////////////////////////////////////
//
// FileName : swap_item25.cpp
// Version : 0.10
// Author : Ryan Han
// Date : 2013/07/26 13:13:55
// 2013/10/30 08:27:50
// Comment :
//
/////////////////////////////////////////////////////////////////////////////// #include "swap_item25.h"
#include <iostream>
using namespace std; int main()
{
Widget a;
Widget b(,,); a.WidgetPrint();
b.WidgetPrint(); swap(a, b); a.WidgetPrint();
b.WidgetPrint(); int* pinta = new int();
int* pintb = pinta; cout << "*pinta is: " << *pinta << endl;
cout << "*pintb is: " << *pintb << endl; return ;
}

C++-高效的swap的更多相关文章

  1. 【原创】C++之自定义高效的swap(1)

    1 问题背景     当交换两个包含了指针成员的类,我们最想看到的是直接交换其指针.但是当我们调用std::swap标准库这个模板函数时,通常它都会复制3个指针指向的对象作为交换所用,缺乏效率.如下: ...

  2. c++下为使用pimpl方法的类编写高效的swap函数

    swap函数是c++中一个常用的函数,用于交换两对象的值,此外还用于在重载赋值运算符中处理自赋值情况和进行异常安全性编程(见下篇),标准模板库中swap的典型实现如下: namespace stl { ...

  3. 读书笔记 effective c++ Item 25 实现一个不抛出异常的swap

    1. swap如此重要 Swap是一个非常有趣的函数,最初作为STL的一部分来介绍,它已然变成了异常安全编程的中流砥柱(Item 29),也是在拷贝中应对自我赋值的一种普通机制(Item 11).Sw ...

  4. 条款25:考虑写出一个不抛出异常的swap函数

    首先说下标准库的swap算法: namespace std{ template<typename T> void swap(T & a, T & b) { T tmp = ...

  5. swap() 函数实现的方法

    swap()函数总结: 一.利用临时变量 1.引用(交换任意类型) template <typename T> void swap(T& x,T& y) { T tmp; ...

  6. linux kernel 如何处理大小端

    暂时在用MPC8309,不太清楚大小端内核是什么时候给转的. 今天看了关于readl和writel具体实现的文章 今天就主要来分析下readl/writel如何实现高效的数据swap和寄存器读写.我们 ...

  7. linux kernel如何处理大端小端字节序

    (转)http://blog.csdn.net/skyflying2012/article/details/43771179 最近在做将kernel由小端处理器(arm)向大端处理器(ppc)的移植的 ...

  8. C++编码优化之减少冗余拷贝或赋值

    临时变量 目前遇到的一些产生临时变量的情况:函数实参.函数返回值.隐式类型转换.多余的拷贝 1. 函数实参 这点应该比较容易理解,函数参数,如果是实参传递的话,函数体里的修改并不会影响调用时传入的参数 ...

  9. C++ 11的移动语义

    目录 可拷贝和可移动的概念 移动构造函数和移动赋值函数 小结移动构造和移动赋值 std::move() 使用 std::move 实现一个高效的 swap 函数 Move and swap 技巧 参考 ...

随机推荐

  1. hdu 4864 Task

    题目链接:hdu 4864 其实就是个贪心,只是当初我想的有偏差,贪心的思路不对,应该是这样子的: 因为 xi 的权值更重,所以优先按照 x 来排序,而这样的排序方式决定了在满足任务(即 xi > ...

  2. maven各种插件在总结

    http://blog.csdn.net/taiyangdao/article/category/6377863  好文章系列课程

  3. AsyncTask实现异步线程通信

    AsyncTask是Android1.5开始提供的一个封装了Thread与Handler可以实现异步线程的简单方式,不需要再自己实现子线程,然后在主线程处接受数据. 因为AsyncTask是用线程池, ...

  4. 队列 - 从零开始实现by C++

    参考链接:数据结构探险-队列篇 数据结构太重要了,不学好是没法进行软件开发的. C++写数据结构基本套路:一个.h文件写该数据结构类的接口:一个.cpp文件写接口的具体实现:一个main.cpp用于测 ...

  5. js encodeURI方法认识

    很早就知道js中encodeURI方法,也很早就用过,但是每次看到它总感觉有些陌生,因为不知道到底是什么原理,和普通的编码到底什么关系, 今天在查看w3c api时又遇到了她,正好有空就多看了几眼,突 ...

  6. java 多线程8(守护线程)

    比如:后台偷偷运行的那些,qq下载更新包 如果一个进程中只剩下了守护线程,那么守护线程也会死亡.. 一个线程默认都不是守护线程. 判断是否是守护线程:例:d.isDaemon(); 当一个线程随着你的 ...

  7. ogre入门笔记

    ogre作为一款开源的非商业渲染引擎, 除去效率不谈, 其设计结构十分优雅, 值得游戏从业者拥有和学习.本篇笔记基于ogre v1.9. 1.代码模块 ogre的核心代码分布如下图: ogreMain ...

  8. Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤

    #定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...

  9. html5日期转long

    正确:日期,时间均是实时的 var inDate = $("#inDate").val().trim(); if(inDate != "") { inDate ...

  10. 解决点击a标签返回页面顶部的问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...