c++检查范围内值的通用方法

c++ generic way to check a value in a range

本文关键字:方法 检查 范围内 c++      更新时间:2023-10-16

数学中有不同的范围:它们可以是开的((a,b((、闭的([a,b](、左开的(a,b]。

我想用C++(而不是11(编写一个模板函数,它可以有效地管理这些情况。但我对元编程和模板没有太多信心。

我想要这样的东西:

const int max = MAX, int min = MIN;
int x = value;
// check close range
if ( is_in_range( x, min, max ) ) //...
if ( is_in_range( x, min, max, open) ) //...
if ( is_in_range( x, min, max, left_open) ) //...
if ( is_in_range( x, min, max, right_open) ) //...

有人有什么建议吗?

编辑1

我已经尝试过了,但无法编译

enum { range_open, range_close, range_left_open, range_right_open };
namespace detail {
    template < typename Type >
    inline bool check_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x < max );
    }
    template < typename Type >
    inline bool check_close_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x <= max );
    }
    template < typename Type >
    inline bool check_left_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x <= max );
    }
    template < typename Type >
    inline bool check_right_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x < max );
    }
 }
template < typename Type, int range_open >
inline bool check_range( const Type& x, const Type& max, const Type& min );
template < typename Type, range_open >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_open_range( x, min, max );
}
template < typename Type, range_close >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_close_range( x, min, max );
}
template < typename Type, check_left_open_range >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_left_open_range( x, min, max );
}
template < typename Type, check_right_open_range >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_right_open_range( x, min, max );
}

但实际上更简单的是使用4个过载功能

编辑2

namespace detail {
    template < typename Type >
    inline bool check_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x < max );
    }
    template < typename Type >
    inline bool check_close_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x <= max );
    }
    template < typename Type >
    inline bool check_left_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x <= max );
    }
    template < typename Type >
    inline bool check_right_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x < max );
    }
}
struct range_open {};
struct range_close {};
struct left_open_range {};
struct right_open_range {};
template < typename Type >
inline bool check_range( const Type& x, const Type& max, const Type& min)
{
    return detail::check_open_range( x, min, max );
}
template < typename Type >
inline bool check_range( const Type& x, const Type& max, const Type& min, const range_open&)
{
    return detail::check_open_range( x, min, max );
}
template < typename Type >
inline bool check_range( const Type& x, const Type& max, const Type& min, const range_close& )
{
    return detail::check_close_range( x, min, max );
}
template < typename Type >
inline bool check_range( const Type& x, const Type& max, const Type& min, const left_open_range& )
{
    return detail::check_left_open_range( x, min, max );
}
template < typename Type >
inline bool check_range( const Type& x, const Type& max, const Type& min, const right_open_range& )
{
    return detail::check_right_open_range( x, min, max );
}

我认为编辑2是对的。。。

编辑3:好版本

结构体left_open{样板静态布尔比较(const T&value,const T&mp;range({return range<value;}};

struct left_close {
    template <class T>
    static bool compare (const T& value, const T& range) { return range <= value; }
};
struct right_open  {
    template <class T>
    static bool compare (const T& value, const T& range) { return value < range; }
};
struct right_close {
    template <class T>
    static bool compare (const T& value, const T& range) { return value <= range; }
}; 
template <class L, class R, class T>
bool check_range(const T& value, const T& min, const T& max)
{
    return L::compare <T> (value, min) && R::compare <T> (value, max);
}

这里有一些在VS2010上工作的东西:-

class LeftOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value >= range; }
};
class LeftClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value > range; }
};
class RightOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value <= range; }
};
class RightClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value < range; }
};
template <class L, class R, class T>
bool IsInRange (T value, T min, T max) { return L::Compare <T> (value, min) && R::Compare <T> (value, max); }
int main()
{
  int
    min = 5,
    max = 99;
  bool
    r1 = IsInRange <LeftOpen, RightOpen> (-19, min, max),
    r2 = IsInRange <LeftOpen, RightOpen> (45, min, max),
    r3 = IsInRange <LeftOpen, RightOpen> (149, min, max);
}

一个简单的解决方案是使用重载:

namespace range_policy
{
  struct open {};
  struct left_open {};
  struct right_open {};
  ...
};
template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::open) { .... }
template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::left_open) { .... }

其中相应地实现每个CCD_ 1过载。

在C++11中,您可以考虑使用强类型枚举,而不是伪structs:

enum class range_policy {open, left_open, right_open, ....};

您可以考虑模板编程的标记调度技术。is_in_range函数将有4个重载,每个重载通过一个参数(函数中未使用(来区分,以选择正确的重载。每次过载都可以简单地增加/减少最小值/最大值,以达到您想要的范围。

为此进行枚举。

enum RangeType
{
   close,
   open,
   left_open,
   right_open
}
bool is_in_range(int x, int min, int max, RangeType type) { }