你可以设置一个整数(C ++)的最大限制吗? [英] Can you set a maximum limit to an integer (C++)?

查看:92
本文介绍了你可以设置一个整数(C ++)的最大限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不想要一个整数超过100,是否有任何简单的方法来确保整数不会超过100,而不管用户增加了多少?



例如,

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

提前感谢

解决方案

这是一个相当简单和相当完整的一个简单的ADT的例子一个通用的BoundedInt。




  • 它使用boost /运算符来避免编写繁琐的(const,非分配)重载。

  • 隐式转换使其可互操作。

  • 我避开了智能优化(因此代码更容易适应模数版本,

  • 我也避免了直接模板的重载在混合实例上转换/操作(例如,比较一个BoundedInt和一个BoundedInt)也是同样的原因:你可以

    注意:



    ul>
  • 您需要c ++ 0x支持以允许Max的默认值生效( constexpr support); 不需要,只要您手动指定最大值



  #include< limits> 
#include< iostream>
#include& lt; 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]



红利资料:


$ b b

使用完全符合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


If I never want an integer to go over 100, is there any simple way to make sure that the integer never exceeds 100, regardless of how much the user adds to it?

For example,

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

Thanks in advance

解决方案

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

  • It uses boost/operators to avoid writing tedious (const, non-assigning) overloads.
  • The implicit conversions make it interoperable.
  • I shunned the smart optimizations (the code therefore stayed easier to adapt to e.g. a modulo version, or a version that has a lower bound as well)
  • I also shunned the direct templated overloads to convert/operate on mixed instantiations (e.g. compare a BoundedInt to a BoundedInt) for the same reason: you can probably rely on the compiler optimizing it to the same effect anyway

Notes:

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

A very simple demonstration follows.

#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 c++11 compliant compiler, you could even define a Userdefined Literal conversion:

typedef BoundedInt<unsigned int, 100> max100;

static max100 operator ""_b(unsigned int i) 
{ 
     return max100(unsigned int i); 
}

So that you could write

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

这篇关于你可以设置一个整数(C ++)的最大限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆