[英]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/
另一種選擇是在每次操作后使用它:
if (answer > 100) answer = 100;
這是一個相當簡單且相當完整的示例,它是通用 BoundedInt 的簡單 ADT。
筆記:
下面是一個非常簡單的演示。
#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 的編譯器,您甚至可以定義用戶定義的文字轉換:
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
我知道這是一個舊帖子,但我認為它對某些人仍然有用。 我用它來設置上限和下限:
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);
}
您可以編寫自己的類 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++ 不允許覆蓋(重新定義)原始類型的運算符。
如果制作自定義整數類(如上所述)不屬於您對“簡單方式”的定義,那么您的問題的答案是否定的,沒有簡單的方法可以做您想做的事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.