带范围检查的整数类型。 [英] An integer type with range checking.
问题描述
我希望能够从流中提取整数而不需要
来编写测试,当我想要在某个范围内的整数时。不幸的是
标准C ++库中没有范围检查的整数类型。考虑
以下函数:
std :: istream& read_range_checked_integer(std :: istream& is,int
& result_value)
{
int value;
if(!(>> value))
{
std :: cout<< 未能提取整数 << std :: endl;
返回;
}
if(!(value> = 0&&)值< 64))
{
std :: cout<< 提取的整数超出范围 << std :: endl;
is.setstate(std :: ios_base :: failbit);
返回;
}
result_value = value;
返回是;
}
我想要一个类型来帮助更简洁地写read_range_checked_integer。对于
示例:
std :: istream& read_range_checked_integer(std :: istream& is,int
& result_value)
{
range_checked_integer< 0,64>价值;
if(!(>> value))
{
std :: cout<< 未能提取range_checked_integer< 0,64> <<
std :: endl;
返回是;
}
result_value = value.get_int();
返回是;
}
有人可以帮我找一个行为类似range_checked_integer的类型?
非常感谢。
Jason Heyes写道:我想成为当我想要在某个范围内的整数时,能够从流中提取整数而无需编写测试。不幸的是,标准C ++库中没有范围检查的整数类型。考虑以下功能:
std :: istream& read_range_checked_integer(std :: istream& is,int
& result_value)
{
int value;
if(!(是>> value))
{
std :: cout<< 未能提取整数 << std :: endl;
返回;
}
if(!(value> = 0&& value< 64))
{
std :: cout<< 提取的整数超出范围 << std :: endl;
is.setstate(std :: ios_base :: failbit);
返回;
}
result_value = value;
返回是;
}
我想要一个类型来帮助更简洁地编写read_range_checked_integer。对于
示例:
std :: istream& read_range_checked_integer(std :: istream& is,int
& result_value)
{
range_checked_integer< ; 0,64>值;
if(!(是>> value))
{
std :: cout<< 未能提取range_checked_integer< 0,64> <<
std :: endl;
返回;
}
result_value = value.get_int();
返回是;
}
有人可以帮我找一个行为类似range_checked_integer的类型吗?
非常感谢。
创建一个类范围整数值和重载流
运算符。例如:
// rint.h
#ifndef _RINT_H_
#define _RINT_H_
#include< iostream>
class RInt
{
public:
RInt(int min,int max);
virtual~RInt();
friend std :: istream&运算符>>(std :: istream& s,RInt& i);
friend std :: ostream& operator<<(std :: ostream& s,const RInt& i);
private:
int value,min,max;
};
// rint.cpp
#include" rint.h"
#include< iostream> ;
使用命名空间std;
RInt :: RInt(int min,int max):value(min),min(min) ,max(max)
{
}
RInt ::〜RInt()
{
}
istream&运算符>>(istream& s,RInt& i)
{
int tmp;
s>> tmp;
if(tmp< i.min || tmp> i.max)
s.clear(s.failbit);
其他
i.value = tmp;
返回s;
}
ostream& operator<<(ostream& s,const RInt& i)
{
s<< i.value;
返回s;
}
#endif // _ RINT_H_
< jason Heyes写道:
我希望能够从流中提取整数,而不需要在需要某个范围内的整数时编写测试。
这可能比你想象的要复杂一些,但它应该是b $ b应该做的工作:
//部分测试的代码。编译并至少在某种程度上工作,
//但仍可能存在错误。
#include< exception>
#include< ; iostream>
#include< functional>
模板<类T,T lower,T upper,class less = std :: less< T> ; >
类有界{
T val;
静态布尔检查(T const& value){
返回less()(值,更低)|| less()(upper,value);
}
public:
bounded():val(lower){}
有界(T const& v){
if(check(v))
throw std :: domain_error( 超出范围);
val = v;
}
有界(有界const& init): val(init.v){}
bounded& operator =(T const& v){
if(check(v))
throw std :: domain_error(" Out of Range");
val = v;
return * this;
}
运算符T()const {return val; }
朋友std :: istream& operator>>(std :: istream& is,bounded& b){
T temp;
是>> temp;
if(check(temp))
is.setstate(std :: ios :: failbit);
b.val = temp;
返回;
}
};
最多明显不同的是,它将底层类型传递为
a模板参数,而不是将其作为名称的一部分。这使得
各种不同类型的范围同样易于使用 -
,例如你可以很容易地做一个有界的< double,0.001,0.1>作为
有界< int,0,512>就像你要的那样。
-
后来,
杰瑞。
宇宙是自己想象的虚构。
" Jerry Coffin" < JC ***** @ taeus.com>在消息中写道
news:11 ********************** @ o13g2000cwo.googlegr oups.com ...这可能比你想象的要复杂得多,但它应该做的工作:
//部分测试的代码。至少在某种程度上编译和工作,
//但仍可能存在错误。
#include< exception>
#include< iostream>
#include< functional>模板<类T,T lower,T upper,class less = std :: less< T> >
类有界{
T val;
静态布尔检查(T const& value){
返回less()(值,更低)|| less()(upper,value);
}
public:
bounded():val(lower){}
有界(T const) & v){
if(check(v))
throw std :: domain_error(" out of range");
val = v;
}
有界(有界const& init):val(init.v){}
有界& operator =(T const& v){
if(check (v))
抛出std :: domain_error(超出范围);
val = v;
返回* this;
}
运算符T()const {return val;朋友std :: istream& operator>>(std :: istream& is,bounded& b){
T temp;
是>> ; temp;
if(check(temp))
is.setstate(std :: ios :: failbit);
b.val = temp;
返回是;
}
};
最明显的区别在于,它将基础类型作为模板参数传递,而不是将其作为名称的一部分。这使得各种不同类型的范围同样易于使用 -
你可以很容易地做一个有界的< double,0.001,0.1>作为
有界< int,0,512>就像你要求的那样。
看起来不错。你允许在有界类型和
类型T之间进行隐式转换。那么,为什么你需要operator =(T const& v)?
I would like to be able to extract an integer from a stream without having
to write a test when I want the integer within some range. Unfortunately
there is no range-checked integer type in the standard C++ library. Consider
the following function:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
int value;
if (!(is >> value))
{
std::cout << "failed to extract integer" << std::endl;
return is;
}
if (!(value >= 0 && value < 64))
{
std::cout << "extracted integer is out of range" << std::endl;
is.setstate(std::ios_base::failbit);
return is;
}
result_value = value;
return is;
}
I want a type to help write read_range_checked_integer more succinctly. For
example:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
range_checked_integer<0, 64> value;
if (!(is >> value))
{
std::cout << "failed to extract range_checked_integer<0, 64>" <<
std::endl;
return is;
}
result_value = value.get_int();
return is;
}
Can somebody help me find a type that behaves like range_checked_integer?
Thanks alot.
Jason Heyes wrote:I would like to be able to extract an integer from a stream without having
to write a test when I want the integer within some range. Unfortunately
there is no range-checked integer type in the standard C++ library. Consider
the following function:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
int value;
if (!(is >> value))
{
std::cout << "failed to extract integer" << std::endl;
return is;
}
if (!(value >= 0 && value < 64))
{
std::cout << "extracted integer is out of range" << std::endl;
is.setstate(std::ios_base::failbit);
return is;
}
result_value = value;
return is;
}
I want a type to help write read_range_checked_integer more succinctly. For
example:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
range_checked_integer<0, 64> value;
if (!(is >> value))
{
std::cout << "failed to extract range_checked_integer<0, 64>" <<
std::endl;
return is;
}
result_value = value.get_int();
return is;
}
Can somebody help me find a type that behaves like range_checked_integer?
Thanks alot.
Create a class for ranged integer values and overload the stream
operators. For example:
// rint.h
#ifndef _RINT_H_
#define _RINT_H_
#include <iostream>
class RInt
{
public:
RInt(int min, int max);
virtual ~RInt();
friend std::istream& operator >>(std::istream& s, RInt& i);
friend std::ostream& operator <<(std::ostream& s, const RInt& i);
private:
int value, min, max;
};
// rint.cpp
#include "rint.h"
#include <iostream>
using namespace std;
RInt::RInt(int min, int max) : value (min), min (min), max (max)
{
}
RInt::~RInt()
{
}
istream& operator >>(istream& s, RInt& i)
{
int tmp;
s >> tmp;
if (tmp < i.min || tmp > i.max)
s.clear(s.failbit);
else
i.value = tmp;
return s;
}
ostream& operator <<(ostream& s, const RInt& i)
{
s << i.value;
return s;
}
#endif //_RINT_H_
Jason Heyes wrote:I would like to be able to extract an integer from a stream without
having to write a test when I want the integer within some range.
This may be a bit more elaborate than you were thinking of, but it
should do the job:
// Partially tested code. Compiles and works to at least some degree,
// but may still harbor bugs.
#include <exception>
#include <iostream>
#include <functional>
template <class T, T lower, T upper, class less=std::less<T> >
class bounded {
T val;
static bool check(T const &value) {
return less()(value, lower) || less()(upper, value);
}
public:
bounded() : val(lower) { }
bounded(T const &v) {
if (check(v))
throw std::domain_error("out of range");
val = v;
}
bounded(bounded const &init) : val(init.v) {}
bounded &operator=(T const &v) {
if (check(v))
throw std::domain_error("Out of Range");
val = v;
return *this;
}
operator T() const { return val; }
friend std::istream &operator>>(std::istream &is, bounded &b) {
T temp;
is >> temp;
if (check(temp))
is.setstate(std::ios::failbit);
b.val = temp;
return is;
}
};
The most obvious difference is that this passes the underlying type as
a template parameter instead of making it part of the name. This makes
ranges of various different types about equally easy to work with --
e.g. you can just about as easily do a bounded<double, 0.001, 0.1> as a
bounded<int, 0, 512> like you asked for.
--
Later,
Jerry.
The universe is a figment of its own imagination.
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...This may be a bit more elaborate than you were thinking of, but it
should do the job:
// Partially tested code. Compiles and works to at least some degree,
// but may still harbor bugs.
#include <exception>
#include <iostream>
#include <functional>
template <class T, T lower, T upper, class less=std::less<T> >
class bounded {
T val;
static bool check(T const &value) {
return less()(value, lower) || less()(upper, value);
}
public:
bounded() : val(lower) { }
bounded(T const &v) {
if (check(v))
throw std::domain_error("out of range");
val = v;
}
bounded(bounded const &init) : val(init.v) {}
bounded &operator=(T const &v) {
if (check(v))
throw std::domain_error("Out of Range");
val = v;
return *this;
}
operator T() const { return val; }
friend std::istream &operator>>(std::istream &is, bounded &b) {
T temp;
is >> temp;
if (check(temp))
is.setstate(std::ios::failbit);
b.val = temp;
return is;
}
};
The most obvious difference is that this passes the underlying type as
a template parameter instead of making it part of the name. This makes
ranges of various different types about equally easy to work with --
e.g. you can just about as easily do a bounded<double, 0.001, 0.1> as a
bounded<int, 0, 512> like you asked for.
Looks good. You allow for implicit conversion between the bounded type and
type T. Why, then, do you need operator=(T const &v)?
这篇关于带范围检查的整数类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!