Can you set a maximum limit for an integer (C ++)?

If I don't want the integer to exceed 100, is there an easy way to make sure that the integer does not exceed 100, no matter how much the user adds to it?

For instance,

50 + 40 = 90 50 + 50 = 100 50 + 60 = 100 50 + 90 = 100 

Thank you in advance

+4
source share
7 answers

Here is a fairly simple and fairly complete example of a simple ADT for a generic BoundedInt.

  • It uses boost / operator to avoid writing tedious (const, non-assigning) overloads.
  • Implicit conversions make it compatible.
  • I avoided smart optimizations (so it’s easier to adapt the code, for example, to a modular version or to a version that also has a lower bound)
  • I also avoided direct template overloads for converting / working with mixed instances (e.g. compare BoundedInt with BoundedInt) for the same reason: you can probably rely on a compiler optimizing it for the same effect anyway

Notes:

  • you need C ++ 0x support to allow the default value for Max to take effect (constexpr support); Not required as long as you specify Max manually

The following is a very simple demonstration.

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

Sample Usage:

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

Demo output:

 10 [hidden: 10] 60 [hidden: 60] 100 [hidden: 120] 60 [hidden: 60] 61 [hidden: 61] 

Bonus Material:

With a fully compatible C ++ 11 compiler, you can even define a Userdefined Literal transformation:

 typedef BoundedInt<unsigned int, 100> max100; static max100 operator ""_b(unsigned int i) { return max100(unsigned int i); } 

So you can write

 max100 x = 123_b; // 100 int y = 2_b*60 - 30; // 70 
+6
source

Try the following:

 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/

Another option would be to use this after each operation:

 if (answer > 100) answer = 100; 
+11
source

Yes.

At a minimum, you can start with this :

 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; }; 
+1
source

You can write your own IntegerRange class, which includes overloaded operator+ , operator- , etc. For an example of operator overloading, see the complex class here .

0
source

The easiest way is to create a class that contains a value, rather than using an integer variable.

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

You may have problems with results that do not meet expectations. What should be the result of this, 70 or 90?

 LimitedInt q = 2*60 - 30; 
0
source

C ++ does not allow overriding (overriding) operators on primitive types.

If creating your own integer class (as suggested above) does not fit your definition of a "simple way", then there is no answer to your question, there is no easy way to do what you want.

0
source

I know this is an old post, but I think it might be useful for some people. I use this to set the upper and lower bounds:

 bounded_value = max(min(your_value,upper_bound),lower_bound); 

You can use it in a function like this:

 float bounded_value(float x, float upper, float under){ return max(min(x,upper),lower); } 
0
source

All Articles