繁体   English   中英

如何在C ++中解析数组以获取重复项

[英]How do I parse arrays in C++ for duplicates

请帮助我使用C ++解析数组,并且仅显示唯一的数字。 我写了一个程序,可以回答下面的大多数问题。

问题:使用一维数组来解决以下问题。 读20个数字,每个数字介于10到100之间(含10和100)。 读取每个数字时,仅当它不是已读取数字的重复项时,才将其验证并存储在数组中。 读取所有值后,仅显示用户输入的唯一值。 提供所有20个数字都不相同的“最坏情况”。 使用最小的数组可以解决此问题。

我做了什么:我的程序创建了一个20项数组。 提示用户输入数据,对其进行验证并显示。 我尝试了几种仅显示唯一数据的方法,但是我没有完成问题的要求。

样本用户输入:

11,12,12,12,13,14,15,15,15,16,17,18,19,20,21,22,23,24,25,26,27

我的程序输出:

{11,12,12,12,13,14,15,15,15,17,18,19,20,21,22,23,24,25,26,27}

正确的程序输出:

{11,12,13,14,15,15,16,17,18,19,20,21,22,23,24,25,26,27}

任何建议去哪里。 参见下面的代码。 谢谢您的帮助!

#include <iostream>  
#include <array> // needed for c++11 arrays
using namespace std;

int main()
{
    const int arraySize = 20; // limit array length to 20
    array  <int, arraySize userArray>  = {}; 

    //Populate array with user input and validate it to be between 10 and 100

    for (int i = 0; i < userArray.size(); i++)
    {
            cout << "Enter a number between 10 and 100" << endl;
            cin >> userArray[i]; // get user input assign it to proper array subscript

            while(userArray[i] > 100 || userArray[i] < 10)//validate user input to be between 10 and 100
            {
                    cout << "Number needs to be between 10 and 100. Enter a new number" << endl;
                    cin >> userArray[i]; //reassign the proper array subscript if needed
            }

    }

    cout << endl;

    //display the information to look like an array
    //result looks like [ v, w, x, y, z ]

    cout << "[ ";
    //display array values
    for (int i = 0; i < userArray.size() - 1; i++)
    {
            cout << userArray[i] << ", ";
    }
    //properly display last array item
    cout << userArray[(userArray.size() - 1)] << " ]" << endl;


    return 0; }

如果可以使用std::vector ,那么可以使用以下解决方案。

template <typename Type>
std::vector<Type> unique_entries (std::vector<Type> vec) { 
    for (auto iter = vec.begin (); iter != vec.end (); ++iter) { 
        auto f = std::find_if (iter+1, vec.end (), [&] (const Type& val) {
           return *iter == val; // (X)
        });

        if (f != vec.end ()) { 
            vec.erase (std::remove (iter+1, vec.end (), *iter), vec.end ());
        }
    }
    return vec;
}

template <typename T>
void show (std::vector<T> vec) {
    for (const auto& v : vec) {
        std::cout << v << " ";
    }
    std::cout << std::endl;
}

例子是这样的:

std::vector<int> vec {11, 12, 12, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
std::vector<short> vec2 {1, 1, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 10};
std::vector<std::string> vec3 {"a", "a", "a", "aa", "b", "b", "bb", "c"};

show (vec);
show (unique_entries (vec));

show (vec2);
show (unique_entries (vec2));

show (vec3);
show (unique_entries (vec3));

并输出:

11 12 12 12 13 14 15 15 16 17 18 19 20 21 22 23 24 25 26 27 
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
1 1 1 2 3 2 1 2 1 2 3 2 1 2 3 2 1 2 10 
1 2 3 10 
a a a aa b b bb c 
a aa b bb c 

看一下行(X),基本上,您可以对所有提供operator==类型使用此unique_entries ,但是必须记住,如果将此函数用于浮点类型,则可能会失败。

希望这就是您要寻找的...........基本上,我使用另一个数组来存储用户已经输入的元素。 因此,当用户输入数字时,它会检查数字是否已经在“输入的元素”数组中。

#include<iostream>
#define MAX 20

using namespace std;


int main()
{  
  int list[MAX],visitedElements[MAX];
  int noElements,element,currentIndex;
  bool flag=false;

  cout<<"Enter total no. of elements(MAX=20):"<<endl;
  cin>>noElements;

  for(int i=0; i<noElements; i++)
  {
       visitedElements[i] = -1;
  }

  for(int i=0;i<noElements;i++)
  { 
     currentIndex=i;
     cout<<"Enter Element:"<<endl;
     cin>>element;
     for(i=0;i<currentIndex;i++)
     {
        if(visitedElements[i] == element)
        {
           flag==true;
           break;
        }
     }
     if(flag == false)
     {
       list[i]=element;
       visitedElements[i] = element; 
     }
     flag = false;
 }
 cout<<"Elements in list are"<<endl;
 for(int i=0 ; i<noElements ;i++)
 cout<<list[i]<<endl;

 return 0;
}
#include <iostream>

using namespace std;

int main(){

    int tab[10];
    int i=0,j;
    int number=0;
    int counter=1;
    bool flag=true;
    int tries=0;

    while(tries<10){

    do{
        cin>>number;
        tries++;
        if((number>100)||(number<10)){
            cout<<"Wrong number, has to be between 10 and 100"<<endl;
        }
    }
    while((number>100)||(number<10));

    flag=true;
    if(i==0){
        tab[i]=number;
    }
    else{  

        for(j=0; j<counter; j++){
            if(tab[j]==number){
                flag=false;
                i--;
                break;
            }
        }
    }

    if(flag==true){
        tab[i]=number;
        counter++;
    }

    i++;
    }

    for(i=0; i<counter-1; i++){
        cout<<tab[i]<<", ";
    }
}

我大概已经意识到了。 我是编程的新手,但这是我的解决方案。 所以。 您说必须输入20位数字,我的必须输入10位数字,输入10次后,它将列出数组。 因此,如果我的输入是“ 10、10、10、12、13、15、15、18、22、22”,则输出为:“ 10、12、13、15、18、22”,我可能会擦除一半代码,但就像我说的我是一个初学者,我很着急地编写它。

编辑:Ups,数组声明中的小错误。 我正在测试5个元素,却忘了做一个更大的数组。

这个问题要求使用一个数组,因此在读取每个数字并检查重复项后,我将遍历数组中已经存在的数字。 它看起来像这样:

#include <iostream>

constexpr int ARRAY_SIZE 20;

int main()
{
    int userArray[ARRAY_SIZE], validNumbers = 0;
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        int num;
        bool isGood = true;
        std::cin >> num;
        //code to check if input is in range here
        for(int j = 0; j < validNumbers; j++)
        {
            if(userArray[j] == num)
            {
                isGood = false;
                break;
            }
        }
        if(isGood)
        {
            userArray[validNumbers] = num;
            validNumbers++;
        }
    }
    //userArray now contains all unique numbers in the order that they were entered
    for(int i = 0; i < validNumbers; i++)
    {
        std::cout << userArray[i];
        if(i < validNumbers - 1)
        {
            std::cout << ' ';
        }
    }
    std::cout << '\n';
}

在for循环内部,而不是使cin直接进入数组,而是添加另一个int变量并将其设置为等于输入

cin >> variable

然后进行一个遍历当前数组的for循环,以确保它不是重复的; 使用下面的行作为for循环

for(int j = 0; j < i; j++)

如果数字已经在数组中,则继续。 否则,将其添加。

另外,我不是在开始时使用for循环,而是使用while循环

while(i < arraysize)

然后,每当您看到重复项时就减小数组大小,而当您添加元素时仅使i增大

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM