如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:21:13  来源:igfitidea点击:

How to store some kind of "null" in C++ double or int variable?

c++c++11null

提问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.

我有一个文件值的读出a1b1a2,和b2。大多数情况下,所有四个数字都在文件中,但有时只有两个数字。

When there are two numbers, I want to store the values in a1, and b1and I want to store "nothing" in a2and b2. If a2and b2were pointers, I could just assign them to be nullptr, but they are not pointers.

当有两个数字时,我想将值存储在 中a1b1并且我想在a2和 中存储“无” b2。如果a2b2是指针,我可以将它们分配为nullptr,但它们不是指针。

Is there something I can store in doubleand intvariables to indicate that 'nothing' is stored?

有没有我可以存储的东西doubleint变量来表明“没有”被存储?

I know Boost.Optionalis available, but I'm trying to avoid that library.

我知道Boost.Optional可用,但我试图避免使用该库。

采纳答案by jonas25007

You could assign NAN to the double a2, which would also indicate that the int b2 is invalid.

您可以将 NAN 分配给双精度 a2,这也表明 int b2 无效。

This pagefor NAN usage.

此页面用于 NAN 使用。

回答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:

你不能。我可以想到两种替代方法:

  1. use int *; or
  2. 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.
  1. 使用 int *; 或者
  2. 使用在您的上下文中肯定无效的值。例如,如果它永远不能为负数,则使用 -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:

我建议有一个factorybase 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_valuesstructure in the declaration. Otherwise use the two_valuesstructure in function declaration.

当函数显式需要四个值时,请four_values在声明中使用该结构。否则two_values在函数声明中使用结构。

This relationship states that a four_valuesinstance can be used in any function requiring a two_valuesstructure.

这种关系表明,一个four_values实例可以用在任何需要two_values结构的函数中。

Alternative
An alternative is to use std::vectorfor 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

  1. 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).
  2. Include a bool indicating how many values there are:

    struct A {
        double a1;
        int b1;
        double a2;
        int b2;
        bool has_4_nums;
    };
    
  3. Use a pointer (int*or std::unique_ptr<int>as per @Peter Pei Guo), and assign nullptrwhen they do not exist.

  1. 您可以选择一个不能出现在文本文件中的值(非法值),例如0-1std::numeric_limits<int>::max()。当您处理数据时,仅当它不等于非法值(或哨兵)时才使用该值。
  2. 包括一个布尔值,指示有多少个值:

    struct A {
        double a1;
        int b1;
        double a2;
        int b2;
        bool has_4_nums;
    };
    
  3. 使用指针(int*std::unique_ptr<int>按照@Peter Pei Guo),并nullptr在它们不存在时进行分配。

回答by Daniel Frey

You have a two options to avoid boost::optionaland 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::optionalfrom 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::optionalfrom 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 或其他值,