sortbench.h

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C头文件 代码 · 共 94 行

H
94
字号
#ifndef _SORTWRAPPER_H
#define _SORTWRAPPER_H

template <class Type>
class SortWrapper
{
  public:
    SortWrapper()
    { }
    SortWrapper(const Type& t)
      : myValue(t)
    { }
    SortWrapper(const SortWrapper& sw)
      : myValue(sw.myValue)
    {
        ourCopies++;
    }
    const SortWrapper& operator = (const SortWrapper& sw)
    {
        ourAssigns++;
        myValue = sw.myValue;
        return *this;
    }
    bool less(const SortWrapper<Type>& rhs) const
    {
        ourCompares++;
        return myValue < rhs.myValue;
    }
    bool equal(const SortWrapper<Type>& rhs) const
    {
        ourCompares++;
        return myValue == rhs.myValue;    
    }
    const Type& value() const
    {
        return myValue;
    }
    static unsigned long compareCount()  { return ourCompares;}
    static unsigned long assignCount()   { return ourAssigns;}
    static unsigned long copyCount()     { return ourCopies;}
    static void clear()
    {
        ourCompares = ourCopies = ourAssigns = 0;
    }
    
  private:
    Type myValue;
    static unsigned long ourCompares;
    static unsigned long ourCopies;
    static unsigned long ourAssigns;
};

template <class Type>
unsigned long SortWrapper<Type>::ourCompares = 0;

template <class Type>
unsigned long SortWrapper<Type>::ourAssigns = 0;

template <class Type>
unsigned long SortWrapper<Type>::ourCopies = 0;

template <class Type>
bool operator < (const SortWrapper<Type>& lhs,
                 const SortWrapper<Type>& rhs)
{
    return lhs.less(rhs);
} 

template <class Type>
bool operator == (const SortWrapper<Type>& lhs,
                 const SortWrapper<Type>& rhs)
{
    return lhs.equal(rhs);
}

template <class Type>
bool operator <= (const SortWrapper<Type>& lhs,
                  const SortWrapper<Type>& rhs)
{
    return lhs < rhs || lhs == rhs;
}




template <class Type>
ostream& operator << (ostream& os, const SortWrapper<Type>& sw)
{
    os << sw.value();
    return os;
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?