comparer.h

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

H
43
字号
#ifndef _COMPARER_H
#define _COMPARER_H

// *********************
// for use in sorting, and in searches
// author: Owen Astrachan
// 7/20/98
//
// the struct Comparer, with templated implementation
// here, is an exemplar of the kind of struct/class needed
// for sorting and searching.  The template parameter must
// be the same kind as the objects being sorted/searched for
//
// the Comparer struct can be used for "standard" types, the
// only needed operator is <
//
// **********************

template <class Kind>
struct Comparer
{
    int compare(const Kind & lhs, const Kind & rhs) const;
};


template <class Kind>
int Comparer<Kind>::compare(const Kind & lhs, const Kind & rhs) const
// precondition:  operator < defined for class Kind, and Kind obeys
//                the trichotomy law: a < b OR a > b OR a == b
// postcondition: if lhs < rhs returns value < 0
//                if lhs > rhs returns value > 0
//                if lhs == rhs returns 0
{
    if (lhs < rhs) return -1;
    if (rhs < lhs) return  1;
    
    // else ==
    
    return 0;
}


#endif

⌨️ 快捷键说明

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