c++检查范围内值的通用方法
c++ generic way to check a value in a range
数学中有不同的范围:它们可以是开的((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) { }
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- 检查哪个对象调用了另一个对象的对象方法
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 检查类是否在方法中实例化
- 检查类方法中是否(此 == nullptr)
- 检查子类型时的专用方法模板
- 检查两个向量是否并行的最有效方法
- 如何检查在编译时是否调用了模板化方法?
- 尝试编译SFINAE检查中使用的方法体时发生编译错误
- 有没有一种简单的方法来检查C++中的不安全表达式
- 是否有更有效的方法来检查元素是否在给定的区间内
- 检查特定目录中是否存在与文件匹配的 abc* 的最佳方法
- 检查数组是否等于的最快方法?
- 替换枚举以最大化编译时间检查的最佳方法
- 双重检查创建单例问题的方法
- 如果方法不进行类型检查,为什么C++模板匹配?
- C++:检查向量中的元素是否大于另一个具有相同索引的元素的有效方法?
- 检查子类是否执行了方法重写
- 最快/最小方法检查字符串是否包含多个单词
- SFINAE c++方法检查