C++ 如何在数组中找到特定值并返回其索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3909784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find a particular value in an array and return its index?
提问by rectangletangle
Pseudo Code:
伪代码:
int arr[ 5 ] = { 4, 1, 3, 2, 6 }, x;
x = find(3).arr ;
x would then return 2.
x 然后将返回 2。
回答by Peter Alexander
The syntax you have there for your function doesn't make sense (why would the return value have a member called arr
?).
您的函数的语法没有意义(为什么返回值会有一个名为 的成员arr
?)。
To find the index, use std::distance
and std::find
from the <algorithm>
header.
要查找索引,请使用std::distance
和std::find
从<algorithm>
标题。
int x = std::distance(arr, std::find(arr, arr + 5, 3));
Or you can make it into a more generic function:
或者你可以把它变成一个更通用的函数:
template <typename Iter>
size_t index_of(Iter first, Iter last, typename const std::iterator_traits<Iter>::value_type& x)
{
size_t i = 0;
while (first != last && *first != x)
++first, ++i;
return i;
}
Here, I'm returning the length of the sequence if the value is not found (which is consistent with the way the STL algorithms return the last iterator). Depending on your taste, you may wish to use some other form of failure reporting.
在这里,如果未找到值,我将返回序列的长度(这与 STL 算法返回最后一个迭代器的方式一致)。根据您的喜好,您可能希望使用某种其他形式的故障报告。
In your case, you would use it like so:
在你的情况下,你会像这样使用它:
size_t x = index_of(arr, arr + 5, 3);
回答by Brian
Here is a very simple way to do it by hand. You could also use the <algorithm>
, as Peter suggests.
这是一个非常简单的手工方法。您也可以使用<algorithm>
,正如彼得建议的那样。
#include <iostream>
int find(int arr[], int len, int seek)
{
for (int i = 0; i < len; ++i)
{
if (arr[i] == seek) return i;
}
return -1;
}
int main()
{
int arr[ 5 ] = { 4, 1, 3, 2, 6 };
int x = find(arr,5,3);
std::cout << x << std::endl;
}
回答by pm100
the fancy answer. Use std::vector and search with std::find
花哨的答案。使用 std::vector 并使用 std::find 进行搜索
the simple answer
简单的答案
use a for loop
使用 for 循环
回答by virious
#include <vector>
#include <algorithm>
int main()
{
int arr[5] = {4, 1, 3, 2, 6};
int x = -1;
std::vector<int> testVector(arr, arr + sizeof(arr) / sizeof(int) );
std::vector<int>::iterator it = std::find(testVector.begin(), testVector.end(), 3);
if (it != testVector.end())
{
x = it - testVector.begin();
}
return 0;
}
Or you can just build a vector in a normal way, without creating it from an array of ints and then use the same solution as shown in my example.
或者,您可以仅以正常方式构建向量,而无需从整数数组创建它,然后使用与我的示例中所示相同的解决方案。
回答by Nightswatch
int arr[5] = {4, 1, 3, 2, 6};
vector<int> vec;
int i =0;
int no_to_be_found;
cin >> no_to_be_found;
while(i != 4)
{
vec.push_back(arr[i]);
i++;
}
cout << find(vec.begin(),vec.end(),no_to_be_found) - vec.begin();
回答by rashedcs
We here use simply linear search. At first initialize the index equal to -1 . Then search the array , if found the assign the index value in index variable and break. Otherwise, index = -1.
我们在这里使用简单的线性搜索。首先将索引初始化为 -1 。然后搜索数组,如果找到在索引变量中分配索引值并中断。否则,索引 = -1。
int find(int arr[], int n, int key)
{
int index = -1;
for(int i=0; i<n; i++)
{
if(arr[i]==key)
{
index=i;
break;
}
}
return index;
}
int main()
{
int arr[ 5 ] = { 4, 1, 3, 2, 6 };
int n = sizeof(arr)/sizeof(arr[0]);
int x = find(arr ,n, 3);
cout<<x<<endl;
return 0;
}
回答by andand
If the array is unsorted, you will need to use linear search.
如果数组未排序,则需要使用linear search。