• 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;    
    }
  • 相关阅读:
    NLP——天池新闻文本分类 基于深度学习的文本表示
    NLP——天池新闻文本分类 Task4:fasttext深度学习
    NLP——天池新闻文本分类 TASK3
    Python基础Task3:异常处理
    Python基础TASK2:条件语句与循环语句
    React开发入门:以开发Todo List为例
    [MIT 18.06 线性代数]Intordution to Vectors向量初体验
    [Java Tutorial学习分享]接口与继承
    FutureTask源码分析(JDK7)
    KMP(超详细复杂度分析)
  • 原文地址:https://www.cnblogs.com/dracohan/p/3813364.html
Copyright © 2020-2023  润新知