zoukankan      html  css  js  c++  java
  • C++-高效的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.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(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;    
    }
  • 相关阅读:
    【转】Web Service单元测试工具实例介绍之SoapUI
    【节选】刘积仁:怎样才是真正的创业者
    xadmin引入django-import-export导入功能
    django使用xadmin
    Mac Docker安装Redis4.0
    JMeter+Maven+CSV数据驱动
    Selenium+TestNG+CSV数据驱动
    JMeter压测时报“内存不足”故障的9个简单解决方案
    requests+unittest+ddt+xlrd+pymysql+BeautifulReport数据驱动
    Mac Docker安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/dracohan/p/3813364.html
Copyright © 2011-2022 走看看