gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:22:35 25 4
gpt4 key购买 nike

在数学中有不同类型的范围:它们可以是开( (a, b) )、闭( [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:好的版本

结构左开{ 模板 static bool compare (const T& value, const T& range) { 返回范围 < 值; } };

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);
}

关于c++检查范围内值的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com