程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 C++ 数组中包含缺失数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 C++ 数组中包含缺失数据?

开发过程中遇到在 C++ 数组中包含缺失数据的问题如何解决?下面主要结合日常开发的经验,给出你关于在 C++ 数组中包含缺失数据的解决方法建议,希望对你解决在 C++ 数组中包含缺失数据有所启发或帮助;

我是 C++ 新手。

以下代码可让用户在 elements 中输入五个 array,然后将这些值相加,并获得平均值和预测的未来值。

如果用户输入 5 个 elements,代码工作正常,但我如何处理缺少一个或多个值的情况?

我在下面编写了似乎解决这个问题的代码,方法是将缺失值定义为负数。该代码似乎也工作正常。但是,在 C++ array 中是否有更好的、公认的处理缺失值的方法?

如果我尝试在 Microsoft Visual Studio 2019 中运行第一个代码,我什至不知道要为缺失值输入什么。如果我不输入任何内容,只需按 Enter 键,则不会发生任何事情。

这是适用于五个元素的原始代码。此代码由 Saldina Nurak 编写的代码略有修改:

#include <iostream>
using namespace std;
    
int nmonths = 6;
int totalmonths = 24;
    
int main()
{
    // {100,220,300,200,250}
    // This line works in the command window
    // float monthArray[nmonths];
    
    // for Microsoft Visual Studio 2019
    float monthArray[6];
    
    float total = 0;
    for(int i = 0; i <= (nmonths-1); i++)
    {
        cout << "Enter Amount " << i+1 << ": ";
        cin >> monthArray[i];
        total += monthArray[i];
    }
    
    float average = total / nmonths;
    float inTwoYears = average * totalmonths;
    
    cout << "total = " << total << endl;
    cout << "average = " << average << endl;
    cout << "inTwoYears = " << inTwoYears << endl;
}
@H_502_25@
Enter Amount 1: 100
Enter Amount 2: 220
Enter Amount 3: 300
Enter Amount 4: 0
Enter Amount 5: 200
Enter Amount 6: 250
total = 1070
average = 178.333
inTwoYears = 4280
@H_502_25@

这是我写的修改后的代码,它似乎处理缺失值,将它们定义为负数:

#include <iostream>
using namespace std;
    
int nmonths = 6;
int totalmonths = 24;
int emptycounter = 0;
    
int main()
{
    // This works from the command window
    // float monthArray[nmonths];  // {100,-99,250};
    
    // for Microsoft Visual Studio I have to use
    float monthArray[6];
    
    float total = 0;
    
    for(int i = 0; i <= (nmonths-1); i++)
    {
        cout << "Enter Amount " << i+1 << ": ";
        cin >> monthArray[i];
    
        if (monthArray[i] >= 0) emptycounter++;
        else (emptycounter = emptycounter);
    
        if (monthArray[i] >= 0) total += monthArray[i];
        else total = total;
    }
    
    float average = total / emptycounter;
    float inTwoYears = average * (totalmonths - (nmonths - emptycounter));
    
    cout << "total = " << total << endl;
    cout << "average = " << average << endl;
    cout << "inTwoYears = " << inTwoYears << endl;
}
@H_502_25@
C:\Users\mark_>cd C:\Users\mark_\myCppprograms
C:\Users\mark_\myCppprograms>c++ MissingDataInArray2.cpp -o MissingDataInArray2.exe -std=gnu++11
C:\Users\mark_\myCppprograms>MissingDataInArray2
Enter Amount 1: 100
Enter Amount 2: 220
Enter Amount 3: 300
Enter Amount 4: 0
Enter Amount 5: -99
Enter Amount 6: 250
total = 870
average = 174
inTwoYears = 4002
@H_502_25@

处理 C++ 中缺失值的标准方法是什么?用户如何从键盘输入缺失值?

解决方法

在 C++ 中处理缺失值的标准方法是什么?

std::optional 是标准的,可以满足需求。

用户如何从键盘输入缺失值?

对于 operator>>istream 没有 std::optional<float> 的定义,但您可以编写一个按照您想要的方式运行的函数。

例如,您可以使用 std::getline 始终读取整行,然后如果该行为空,则返回一个空的 std::optional<float>,如果不是,则解析该数字并返回一个 std::optional<float>包含它。

,

如果您尝试读取数字,则必须定义应该是缺失值的内容。您可以阅读该行并尝试将其解析为 int,如果无法解析,那么它将是您的缺失值?

此外,您使用的不是 C++ 数组,而是 C 数组。

C++ 有一个 array 容器,但 vector 为您提供了更大的灵活性。

你可以这样做:

vector<int> monthArray;
int value;
for(int i = 0; i < nmonths; i++) // See the change done in the test
{
    cin >> value;
    if(value > 0)
       monthArray.push_back(value); // This would insert at the end and take care of resizing the container as needed.
}
monthArray.size(); // This returns how many elements you have in the container

您的两个 else 子句都为自身分配了一个变量。如果:

,您可以删除两者并将两个语句放在同一个语句中:
if (monthArray[i] >= 0) 
{
    emptycounter++;
    total += monthArray[i];
}

但如果您使用矢量,则不需要 emptycounter。向量的大小将包含有效元素的数量。

for(int i = 0; i < nmonths; i++)
{
    cout << "Enter Amount " << i+1 << ": ";
    cin >> value;
    if(value > 0)
    {
       monthArray.push_back(value);
       total += value;
    }
}

毕竟......有一个问题:Do you really need an array?你似乎只是累积了有效值,并且在保存元素之后从不引用数组。

P.S:要使用vector,您需要#include<vector>

,

当缺失的观察被定义为 NA(如在 R 中并由 @Yksisarvinen 在评论中建议)时,这里是通过使用 stoi 来实现来自 @vmp 的答案的代码。我还没有想出如何实现@Ben Voigt 的答案。

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int nmonths = 6 ;
int totalmonths = 24 ;

int main()
{
     float total = 0;
     vector<int> monthArray;
     string value;

     for(int i = 0; i < nmonths; i++)
     {
          cout << "Enter Amount " << i+1 << ": ";
          cin >> value;
          if(value != "NA")
          {
               monthArray.push_back(stoi(value));
               total += stoi(value);
          }
     }

     float average = total / monthArray.size() ;
     float inTwoYears = average * (totalmonths - (nmonths - monthArray.size())) ;

     cout << "total = " << total << endl;
     cout << "average = " << average << endl;
     cout << "inTwoYears = " << inTwoYears << endl;

}

// C:\Users\mark_>cd C:\Users\mark_\myCppprograms
// C:\Users\mark_\myCppprograms>c++ vector2.cpp -o vector2.exe -std=gnu++11
// C:\Users\mark_\myCppprograms>vector2
// Enter Amount 1: 100
// Enter Amount 2: 220
// Enter Amount 3: 300
// Enter Amount 4: 0
// Enter Amount 5: NA
// Enter Amount 6: 250
// total = 870
// average = 174
// inTwoYears = 4002
 

大佬总结

以上是大佬教程为你收集整理的在 C++ 数组中包含缺失数据全部内容,希望文章能够帮你解决在 C++ 数组中包含缺失数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: