在用户输入的数组中查找值

find values in user entered array

本文关键字:查找 数组 用户 输入      更新时间:2023-10-16

我试图在用户先前输入的值的数组中找到任何用户输入的值。我做了下面的代码来查找数组中输入的值但是似乎不知道在哪里插入查找用户输入值的循环来搜索

好吧更新

我正在寻找一种方法来找到用户输入的值在数组中用户输入之前如果符合逻辑,就像这样

Ok第二次更新

这就是我一直在做的我很震惊没有找到输入的搜索值

#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
    int a[10], found;
    for(int i=0;i<10;i++)
{
    cout<<"enter value : ";
    cin>>a[i];
}
    cout<<"Enter Searching Value :";
    cin>>found;
    for(int i=0;i<10;i++)
{
    if(found == a[10])
{
    cout<<"Value Found";
    _getch();
}
        else if (found != a[10])
            cout<<"Value Not Found";
}
    _getch();
}

工作方案

你正在解决一个查找问题。您没有任何建议输入的值
可能在哪里,因此您应该一步一步地尝试每个值。这是一个常见的问题。

解决它的第一种方法是使用循环。对于现代c++来说,这不是一个好方法。但是你应该尝试一下。

bool has(int val, int const* arr, size_t size) {
    size_t idx = 0;
    // Iterates each element of array and checks
    // whether the one is equal to the `val`. In
    // case of meeting of `val`, the loop stops.
    while (idx < size && arr[idx] != val) ++idx;
    return idx != size;
}

下面的方式更方便。实际上,has函数的一般形式已经在c++标准库<algorithm>头文件中有了。它被称为find。它做的完全一样,但要好得多。实际上,在<algorithm>头中有很多解决常见问题的函数。你必须随时随地使用它。

bool has_(int val, int const* arr, size_t size) {
    int const* end = arr + size;
    // Now `has_` don't iterate each element and
    // checks it. It finds the `val` in range
    // between the first element of array and
    // the last.
    return std::find(arr, end, val) != end;
}

我建议您阅读本书中"STL:容器"一节中的"更喜欢算法调用而不是手写循环"小节Herb Sutter和Andrei Alexandrescu的《c++编码标准》来直观地了解为什么要使用<algorithm>头。

另外,您可以在这里找到对<algorithm>头的引用。

解决方案中的错误

让我们考虑一下你的代码,并讨论一下为什么会出现错误。实际上,你只是打错字了。
这是使用<algorithm>头而不是像你这样手写循环的原因之一。

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
    int a[10], found;
    for (int i = 0; i<10; i++)
    {
        cout << "enter value : ";
        cin >> a[i];
    }
    cout << "Enter Searching Value :";
    cin >> found;
    for (int i = 0; i<10; i++)
    {
        // Look at here: you compare entered value ten times
        // with the value after the last element of array.
        if (found == a[10])
        {
            // In case that you found an entered value in array
            // you just continue the loop. You should probably
            // break it at this point. This may be achieved by
            // using the `brake` operator or the `while` loop.
            cout << "Value Found";
            _getch();
        }
        else if (found != a[10])
            cout << "Value Not Found";
    }
    _getch();
}

我想这个答案会解决你的问题。:)

#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
    int a[10];
    for(int i=0;i<10;i++)
    {
        cout<<"enter value : ";
        cin>>a[i];
    }
    int random;
    cin>>random;
    int flag=0;
    for(int i=0;i<10;i++)
    {
       if(a[i]==random)
       {
         flag=1;
         cout<<"Found at a["<<i<<"]"<<endl;
         break;
       }
    }
    if(flag==0)
    cout<<"element not found"<<endl;
    return 0;
}

您的问题是整数数组在已分配和未分配之间没有明确的定义。您需要一种方法来"标记"这些整数。您可以使用一个布尔值奇偶校验数组来做到这一点,它将基于std::cin.fail()进行标记,或者使用int*并使用NULL进行标记。有很多方法可以做到这一点。

std::vector<int*> a(10);
for(int i=0;i<10;i++){
  int input;
  cout<<"enter value : ";
  cin >> input;
  if(!cin.fail()) {
    a[i] = new int(input);
  }else{
    cin.clear(std::ios_base::failbit);
  }
}

现在您可以测试向量a的NULL指针