使用基于C++11范围的循环验证数组值?[无矢量]

Verifying array values with C++11 range based loop? [Without vectors]

本文关键字:数组 验证 C++11 范围 循环      更新时间:2023-10-16

我有一个赋值,要求程序从用户输入到数组中读取20个数字。条件要求该值在10-100之间且不重复。我也只能使用一个包含20个元素的数组。然而,它不应该提示用户,只是不存储值;最后,程序必须打印出唯一的用户值。正确的结果例如:

input = 9 10 15 15 15 0
output = 10 15
//this is a small example with a 6 element array instead of 20

当我测试我的程序时,我只得到

input: 9 10 15 15 15 0
output: 10 15 15 15
//this is a small example with a 6 element array instead of 20

我使用基于范围的循环来检查值,如果不满足条件,则将值设置为0。所以任何不为零的东西都不会被打印出来。我已经研究了所有关于堆栈溢出的问题,但我找不到特定问题的答案:

  • 如何使用类构造函数将所有数组元素初始化为零
  • 使其"静态",这样当我运行另一个函数时,以前的数组值是全局的,并根据用户输入进行维护
  • 我创建的循环似乎有问题,但看起来很完美。我和同学们核实了一下,他们也同意了

    //arrayinput.h
    #include <array>
    #include <string>
    class arrayelimination
    {
    public:
        const static size_t limit = 20; 
        arrayelimination();
        void inputArray();
        void displayArray();
    private:
        std::array < int , limit > store;
        int userinput;
    };
    
    //arrayinput.cpp
    #include <iostream>
    #include <array>
    #include "arrayinput.h"
    using namespace std;
    arrayelimination::arrayelimination()
    {
    array < int , limit> store = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    }
    
    void arrayelimination::inputArray()
    {
    for ( size_t i = 0; i < store.size(); i++)
    {
        cout << "Enter number between 10-100 for array box [" 
            << i << "]: " ;
        cin >> userinput;
    //check if numbers is between 10-100
            if (userinput >= 10 && userinput <= 100)
        {
            //MOST LIKELY ERROR check if number has previously been used.
                for ( int &check : store) 
                {
                       if ( check != userinput)
                    {
                        store[i] = userinput;
                        break;
                    }
                    else
                        store[i] = 0;
                }
        }
        //output if number isn't between 10-100
    else 
        store[i] = 0;
    }
    }
    void arrayelimination::displayArray()
    {
            cout << "Your unique array numbers stored include...n";
        //output all the unique numbers that the user inputted.  
    for ( size_t j = 0;  j < 20; j++)
    {
    //if the value is NOT 0, output.
        if (store[j] != 0)
        {
            cout << "array[ " << j << " ] = " << store[j] << "n";
        }   
    }
    }
    

当我测试我的程序时,我只得到

input: 10 15 15 15 2 0 0 0 0 0 0 0 ... 0
output: 10 15 15 15

将其设置为零的概念是可行的,但重复的值并不是唯一的。

我必须使用面向对象的设计作为这项任务的要求。我快要死了——我真的不知道这是怎么回事。请帮帮我。

PS:糟糕的是,我忘了提到我只能使用一个数组

问题不在于range-based for loop本身的结构,而在于检查输入值是否唯一的条件。

在此代码块中:

for ( int &check : store) 
{
    if ( check != userinput)
    {
        store[i] = userinput;
        break;
    }
    else
        store[i] = 0;
 }

您正在设置userinput遇到不匹配的ANY元素的值。因此,第一个不匹配的元素将导致您设置userinput,即使userinput稍后将在数组中匹配一个值。您需要确定的是userinput与数组中的NO元素匹配。

例如:

for ( int &check : store) 
{
    if(check == userinput)
    {
        store[i] = 0;
        break;
    } // a match is found so set the value to 0 and stop
    else
    {
        store[i] = check;
    }
}

标准算法有什么问题吗?

int values[20];
// 1) read them in
for (int& v : values) {
    std::cin >> v;
}
// 2) sort/uniq
std::sort(std::begin(values), std::end(values));
auto last = std::unique(std::begin(values), std::end(values));
// 3) print them
std::for_each(values, last, [](const int v){
    std::cout << v << " ";
});

您让这件事变得过于复杂。

  1. 创建空矢量。甚至不要填充/调整它的大小
  2. 使用push_back添加新元素。根据需要添加任意数量
  3. 使用std::Sort对数组进行排序
  4. 遍历数组,并仅在以下情况下打印元素:
    4.1.它是第一个元素[i==0]
    4.2.或者它不等于前一个元素

您也可以对基于范围的循环执行同样的操作。