可以将最大限制设置为整数(c++)吗?
Can you set a maximum limit to an integer (C++)?
如果我不希望一个整数超过100,有没有什么简单的方法来确保这个整数永远不会超过100,不管用户给它加了多少?
例如,50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
试试这个:
std::min(50 + 40, 100);
std::min(50 + 50, 100);
std::min(50 + 60, 100);
std::min(50 + 90, 100);
http://www.cplusplus.com/reference/algorithm/min/另一个选项是在每个操作之后使用this:
if (answer > 100) answer = 100;
这是一个相当简单且相当完整的通用BoundedInt的简单ADT示例。
- 它使用boost/操作符来避免编写繁琐的(const,非赋值)重载。
- 隐式转换使其可互操作。
- 我避开了智能优化(因此代码更容易适应,例如模版本,或者有下界的版本)
- 我也避免了直接的模板重载转换/操作混合实例(例如比较一个BoundedInt到一个BoundedInt)出于同样的原因:你可能可以依赖于编译器优化它到相同的效果无论如何
指出:
- 你需要c++0x支持来允许Max的默认值生效(constexpr support);不需要,只要你手动指定Max
下面是一个非常简单的演示。
#include <limits>
#include <iostream>
#include <boost/operators.hpp>
template <
typename Int=unsigned int,
Int Max=std::numeric_limits<Int>::max()>
struct BoundedInt : boost::operators<BoundedInt<Int, Max> >
{
BoundedInt(const Int& value) : _value(value) {}
Int get() const { return std::min(Max, _value); }
operator Int() const { return get(); }
friend std::ostream& operator<<(std::ostream& os, const BoundedInt& bi)
{ return std::cout << bi.get() << " [hidden: " << bi._value << "]"; }
bool operator<(const BoundedInt& x) const { return get()<x.get(); }
bool operator==(const BoundedInt& x) const { return get()==x.get(); }
BoundedInt& operator+=(const BoundedInt& x) { _value = get() + x.get(); return *this; }
BoundedInt& operator-=(const BoundedInt& x) { _value = get() - x.get(); return *this; }
BoundedInt& operator*=(const BoundedInt& x) { _value = get() * x.get(); return *this; }
BoundedInt& operator/=(const BoundedInt& x) { _value = get() / x.get(); return *this; }
BoundedInt& operator%=(const BoundedInt& x) { _value = get() % x.get(); return *this; }
BoundedInt& operator|=(const BoundedInt& x) { _value = get() | x.get(); return *this; }
BoundedInt& operator&=(const BoundedInt& x) { _value = get() & x.get(); return *this; }
BoundedInt& operator^=(const BoundedInt& x) { _value = get() ^ x.get(); return *this; }
BoundedInt& operator++() { _value = get()+1; return *this; }
BoundedInt& operator--() { _value = get()-1; return *this; }
private:
Int _value;
};
示例用法:typedef BoundedInt<unsigned int, 100> max100;
int main()
{
max100 i = 1;
std::cout << (i *= 10) << std::endl;
std::cout << (i *= 6 ) << std::endl;
std::cout << (i *= 2 ) << std::endl;
std::cout << (i -= 40) << std::endl;
std::cout << (i += 1 ) << std::endl;
}
演示输出:
10 [hidden: 10]
60 [hidden: 60]
100 [hidden: 120]
60 [hidden: 60]
61 [hidden: 61]
附加材料:
对于完全兼容c++11的编译器,您甚至可以定义一个Userdefined Literal转换:
typedef BoundedInt<unsigned int, 100> max100;
static max100 operator ""_b(unsigned int i)
{
return max100(unsigned int i);
}
所以你可以写
max100 x = 123_b; // 100
int y = 2_b*60 - 30; // 70
可以。
作为最低限度,您可以这样开始:
template <int T>
class BoundedInt
{
public:
explicit BoundedInt(int startValue = 0) : m_value(startValue) {}
operator int() { return m_value; }
BoundedInt operator+(int rhs)
{ return BoundedInt(std::min((int)BoundedInt(m_value + rhs), T)); }
private:
int m_value;
};
您可以编写自己的类IntegerRange,其中包括重载的operator+
, operator-
等。有关操作符重载的示例,请参阅此处的复杂类。
最简单的方法是创建一个类来保存该值,而不是使用整数变量。
class LimitedInt
{
int value;
public:
LimitedInt() : value(0) {}
LimitedInt(int i) : value(i) { if (value > 100) value = 100; }
operator int() const { return value; }
LimitedInt & operator=(int i) { value = i; if (value > 100) value = 100; return *this; }
};
如果结果不符合预期,你可能会陷入麻烦。结果应该是什么,70还是90?
LimitedInt q = 2*60 - 30;
c++不允许在基本类型上重写(重新定义)操作符。
如果创建自定义整数类(如上所述)不属于您定义的"简单方法",那么您的问题的答案是no,没有简单的方法来做您想要的。
我知道这是一个旧的帖子,但我认为它仍然可以对一些人有用。我用它来设置上下边界:
bounded_value = max(min(your_value,upper_bound),lower_bound);
可以在如下函数中使用:
float bounded_value(float x, float upper, float under){
return max(min(x,upper),lower);
}
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- std::当在256字节边界上写入整数时,流的奇怪行为
- 内联程序集printf将整数解释为地址
- 是否基于数组B整数打印数组A中的整数
- 如何在C++中将整数转换为其数字数组