如何在 C++ double 或 int 变量中存储某种“null”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25555015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store some kind of "null" in C++ double or int variable?
提问by jlconlin
I have a class that looks like this
我有一个看起来像这样的课程
struct A {
double a1;
int b1;
double a2;
int b2;
};
I have to read off of a file values for a1
, b1
, a2
, and b2
. Most of the time all four numbers are on the file, but sometimes there are only two numbers.
我有一个文件值的读出a1
,b1
,a2
,和b2
。大多数情况下,所有四个数字都在文件中,但有时只有两个数字。
When there are two numbers, I want to store the values in a1
, and b1
and I want to store "nothing" in a2
and b2
. If a2
and b2
were pointers, I could just assign them to be nullptr
, but they are not pointers.
当有两个数字时,我想将值存储在 中a1
,b1
并且我想在a2
和 中存储“无” b2
。如果a2
和b2
是指针,我可以将它们分配为nullptr
,但它们不是指针。
Is there something I can store in double
and int
variables to indicate that 'nothing' is stored?
有没有我可以存储的东西double
和int
变量来表明“没有”被存储?
I know Boost.Optional
is available, but I'm trying to avoid that library.
我知道Boost.Optional
可用,但我试图避免使用该库。
采纳答案by jonas25007
回答by David Schwartz
Either you have a value that's not legal or you don't. If you have a value that's not legal (like -1), use it as a sentinel. If not, then no. Use Boost.Optionalor roll your own "value plus boolean" class.
要么你有一个不合法的值,要么你不合法。如果您有一个不合法的值(如 -1),请将其用作哨兵。如果没有,那么没有。使用Boost.Optional或推出您自己的“值加布尔值”类。
回答by Peter Pei Guo
You cannot. I can think of two alternative ways:
你不能。我可以想到两种替代方法:
- use int *; or
- Use a value that for sure invalid in your context. For example, if it can never be negative, then use -1 to indicate null. But I still prefer the first way, since its correctness is not depended on requirement or context.
- 使用 int *; 或者
- 使用在您的上下文中肯定无效的值。例如,如果它永远不能为负数,则使用 -1 表示为空。但我仍然更喜欢第一种方式,因为它的正确性不取决于需求或上下文。
回答by Thomas Matthews
Looks like you are going to have problems further down the road. The need to know how many values are valid will be sprinkled through the code base.
看起来您在接下来的道路上会遇到问题。需要知道有多少值是有效的,这将贯穿代码库。
I suggest having a factoryand base class. Essentially, you will have at least two classes:
我建议有一个factory和base class。本质上,您将至少拥有两个类:
struct two_values
{
double a1;
int b1;
};
struct four_values : public two_values
{
double a2;
int b2;
};
When a function explicitly requires the four values, use the four_values
structure in the declaration. Otherwise use the two_values
structure in function declaration.
当函数显式需要四个值时,请four_values
在声明中使用该结构。否则two_values
在函数声明中使用结构。
This relationship states that a four_values
instance can be used in any function requiring a two_values
structure.
这种关系表明,一个four_values
实例可以用在任何需要two_values
结构的函数中。
Alternative
An alternative is to use std::vector
for your items:
另
一种选择是std::vector
用于您的物品:
struct Container
{
std::vector<double> a_values;
std::vector<int> b_values;
};
A benefit with this design is that the vectors can tell you how many items there are and the concept is expandable, in case you need 6 items.
这种设计的一个好处是向量可以告诉你有多少个项目,并且这个概念是可扩展的,以防你需要 6 个项目。
回答by matsjoyce
- You can pick a value that cannot be in the text files (an illegal value), such as
0
,-1
,std::numeric_limits<int>::max()
. When you process the data, only use the value if it does not equal the illegal value (or sentinel). Include a bool indicating how many values there are:
struct A { double a1; int b1; double a2; int b2; bool has_4_nums; };
Use a pointer (
int*
orstd::unique_ptr<int>
as per @Peter Pei Guo), and assignnullptr
when they do not exist.
- 您可以选择一个不能出现在文本文件中的值(非法值),例如
0
、-1
、std::numeric_limits<int>::max()
。当您处理数据时,仅当它不等于非法值(或哨兵)时才使用该值。 包括一个布尔值,指示有多少个值:
struct A { double a1; int b1; double a2; int b2; bool has_4_nums; };
使用指针(
int*
或std::unique_ptr<int>
按照@Peter Pei Guo),并nullptr
在它们不存在时进行分配。
回答by Daniel Frey
You have a two options to avoid boost::optional
and hence the dependency to Boost:
您有两个选择可以避免boost::optional
,因此对 Boost 的依赖:
Use the compiler's
std::experimental::optional
, which is available from GCC 4.9+ (and Clang in recent versions IIRC) with-std=c++14
.Use the "reference implementation" from Andrzej Krzemieński for
std::experimental::optional
from GitHub. It's header only, so you can just copy the header to your project (of course paying attention to the license)
使用编译器的
std::experimental::optional
,这可从GCC 4.9+(及锵在最新版本IIRC)用-std=c++14
。使用 Andrzej Krzemieński for
std::experimental::optional
from GitHub的“参考实现” 。它只是标题,因此您可以将标题复制到您的项目中(当然要注意许可证)
回答by Jarod42
Alternatively, you may add extra member to know if normal members have been affected, something like:
或者,您可以添加额外成员以了解普通成员是否受到影响,例如:
struct A {
double a1;
int b1;
double a2;
int b2;
bool is_assigned[4];
};
or
或者
struct A {
double a1;
int b1;
double a2;
int b2;
double* p_a1; // point to a1 when initialized, else nullptr
int* p_b1; // point to b1 when initialized, else nullptr
double* p_a2; // point to a2 when initialized, else nullptr
int* p_b1; // point to b2 when initialized, else nullptr
};
回答by Robin Halder
struct A {
double a1;
int b1;
double a2;
int b2;
};
for this You can just use any type of variable to indicating whether two or four value are assigned to these variable.
为此,您可以使用任何类型的变量来指示是否为这些变量分配了两个或四个值。
Like take a variable name assigned = -1
就像取一个变量名赋值 = -1
So if Your input value are positive then if two values are inside file then just make
因此,如果您的输入值是正值,那么如果文件中有两个值,则只需执行
a2 = b2 = assigend
a2 = b2 = 赋值
So when You are checking then just check whether the value of a2 and b2 are -1 or something else,
因此,当您检查时,只需检查 a2 和 b2 的值是否为 -1 或其他值,