在用户输入的数组中查找值 [英] find values in user entered array
问题描述
好更新
我正在寻找一种在用户之前输入的数组中查找用户输入值的方法如果它符合逻辑
确定第二次更新
这就是我一直在努力的我被感动了找不到输入的搜索值
#include< iostream>#include< conio.h>使用命名空间std;无效main(){int a [10],发现;for(int i = 0; i< 10; i ++){cout<<"enter value:";cin> a [i];}cout<<输入搜索值:";cin>>找到了;for(int i = 0; i< 10; i ++){if(找到== a [10]){cout<<找到的价值";_getch();}否则,如果(找到!= a [10])cout<<未找到值";}_getch();}
工作解决方案
您正在这里解决发现问题.您对输入的值可能没有任何建议
,因此应逐步尝试每个输入.这是一个普遍的问题.
解决它的第一种方法是使用循环.对于现代C ++而言,这不是一个好方法.但是您可能应该
尝试一下.
bool has(int val,int const * arr,size_t size){size_t idx = 0;//迭代数组的每个元素并检查//是否等于"val".在//遇见`val`的情况,循环停止.while(idx< size&& arr [idx]!= val)++ idx;返回idx!= size;}
下面的方法更方便.实际上,具有 has
函数的更通用形式已经在C ++标准库中的< algorithm>
标头中.它称为 find
.它的功能完全相同,但效果更好.实际上,有许多函数可以解决< algorithm>
标头中的常见问题.您必须在任何可能的地方使用它.
bool has_(int val,int const * arr,size_t size){int const * end = arr +大小;//现在,"has_"不再迭代每个元素,并且//检查它.它在范围内找到"val"//在数组的第一个元素和之间//最后.返回std :: find(arr,end,val)!= end;}
我建议您阅读首选算法调用优先于手写循环"小节.在本书的"STL:容器"部分中"C ++编写标准语言"(作者Herb Sutter和Andrei Alexandrescu 获得有关为什么使用< algorithm>
标头的直觉.
此外,您还可以找到对< algorithm>
标头的引用这里.
您的解决方案中的错误
让我们考虑一下您的代码并讨论为什么您最终会出错.实际上,您只是打错字了.
这是使用< algorithm>
标头而不是像您这样的手写循环的原因之一.
#include< iostream>#include< conio.h>使用命名空间std;无效main(){int a [10],发现;对于(int i = 0; i< 10; i ++){cout<<输入值:";cin>>a [i];}cout<<输入搜索值:";cin>>成立;对于(int i = 0; i< 10; i ++){//在这里查看:您将输入的值进行了十次比较//数组最后一个元素之后的值.如果(找到== a [10]){//如果您在数组中找到了输入的值//您只需继续循环即可.你可能应该//此时将其断开.这可以通过以下方式实现//使用`brake`运算符或`while`循环.cout<<发现价值";_getch();}否则,如果(找到!= a [10])cout<<找不到值";}_getch();}
i am trying to find any user entered value in a array which the user has previously entered the values of. I made the following for finding which values were entered in array but cant seem to know where to insert the loop for finding user entered value for searching
ok update
i am looking for a way to find user entered value in array which the user entered before something like this if its logical
Ok 2nd Update
This is what i have been working on i am struck The entered searching value is not been found
#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();
}
Working solution
You are solving a finding problem here. You have no any suggestions where the entered value may
probably be, so you should try each one step by step. This is a common problem.
The first way to solve it is to use a loop. It isn't a good way for modern C++. But you should probably
try it for a practice.
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;
}
The way below is more convinient. Actually, the more general form of has
function is already has in C++ standart library in <algorithm>
header. It is called find
. It do exactly the same, but much better. Actually, there are a lot of functions solving a common problems in <algorithm>
header. You have to use it anywhere you can.
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;
}
I suggest you to read the subsection "Prefer algorithm calls to handwritten loops." in section "STL: Containers" in the book
"C++ Coding Standarts" by Herb Sutter and Andrei Alexandrescu
to gain an intuition about why to use the <algorithm>
header.
Also, you may find the reference to the <algorithm>
header here.
Mistakes in your solution
Lets consider your code and discuss why you end up with error. Actually, you just made a typo.
It is the one of the reasons to use the <algorithm>
header instead of handwritten loops like yours.
#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();
}
这篇关于在用户输入的数组中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!