首页 > 代码库 > 高效的swap

高效的swap

原始版本:

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 = 1, int b = 2, int c = 3);        /*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 = 1, int b = 2, int c = 3) : 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.htmlnamespace 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(4,5,6);        a.WidgetPrint();    b.WidgetPrint();        swap(a, b);        a.WidgetPrint();    b.WidgetPrint();        int* pinta = new int(5);    int* pintb = pinta;            cout << "*pinta is: " << *pinta << endl;    cout << "*pintb is: " << *pintb << endl;        return 0;    }