在用户输入的数组中查找值
find values in user entered array
我试图在用户先前输入的值的数组中找到任何用户输入的值。我做了下面的代码来查找数组中输入的值但是似乎不知道在哪里插入查找用户输入值的循环来搜索
好吧更新
我正在寻找一种方法来找到用户输入的值在数组中用户输入之前如果符合逻辑,就像这样
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指针
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 需要使用模板查找数组的第二个最小和最小值
- 用于查找数组中最大元素的出现次数的代码,给出分段错误
- 查找数组中指示性较大但数组中值较小的元素
- C++ 函数,用于查找数组中四个最小最大元素的总和不起作用
- 查找数组中的最小数字
- 查找数组中重复(重复)数字的索引
- 使用一个内存集数组和单个堆栈在 O(n) 中查找数组的下一个更大元素
- 查找数组中给定数字的出现次数
- 使用 std::累加来查找数组的总和
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 在不进行排序的情况下查找数组中n个最小值的索引
- 当数组在C++中用作参数时,查找数组的元素数
- 查找数组中数组元素的重复出现?
- 查找数组最后 3 个数字的平均值
- 查找数组中是否存在C++元素
- 查找数组中的最小更改数以进行 k 算术级数
- 查找数组的第一个和最后一个索引,其中 from 和 to 元素的顺序总和最大
- 有没有办法在递归中使用指针来查找数组的最小值?
- 在编写代码以查找数组中的对数时获取分段错误