C++ 如何从向量中找到最小值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13015932/
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 to find minimum value from vector?
提问by Cristi DroPs
How can I find the minimum value from a vector?
如何从向量中找到最小值?
int main()
{
int v[100] = {5,14,2,4,6};
int n = 5;
int mic = v[0];
for(int i=0;i<v[n];i++)
{
if(v[i]<mic)
mic=v[i];
}
cout<<mic;
}
But is not working, what can I do?
但是不起作用,我该怎么办?
采纳答案by alestanis
You have an error in your code. This line:
您的代码中有错误。这一行:
for(int i=0;i<v[n];i++)
should be
应该
for(int i=0;i<n;i++)
because you want to search n
places in your vector, not v[n]
places (which wouldn't mean anything)
因为您想搜索n
向量中的位置,而不是v[n]
位置(这没有任何意义)
回答by zabulus
std::min_element(vec.begin(), vec.end())
- for std::vectorstd::min_element(v, v+n)
- for arraystd::min_element( std::begin(v), std::end(v) )
- added C++11 version from comment by @JamesKanze
std::min_element(vec.begin(), vec.end())
- 对于 std::vector std::min_element(v, v+n)
- 对于数组std::min_element( std::begin(v), std::end(v) )
- 从 @JamesKanze 的评论中添加了 C++11 版本
回答by acrube
You can always use the stl:
您始终可以使用 stl:
auto min_value = *std::min_element(v.begin(),v.end());
回答by bames53
#include <iostream>
#include <vector>
#include <algorithm> // std::min_element
#include <iterator> // std::begin, std::end
int main() {
std::vector<int> v = {5,14,2,4,6};
auto result = std::min_element(std::begin(v), std::end(v));
if (std::end(v)!=result)
std::cout << *result << '\n';
}
The program you show has a few problems, the primary culprit being the for
condition: i<v[n]
. You initialize the array, setting the first 5 elements to various values and the rest to zero. n
is set to the number of elements you explicitly initialized so v[n]
is the first element that was implicitly initialized to zero. Therefore the loop condition is false the first time around and the loop does not run at all; your code simply prints out the first element.
您展示的程序有一些问题,主要的罪魁祸首是for
条件:i<v[n]
. 您初始化数组,将前 5 个元素设置为各种值,其余设置为零。n
设置为您显式初始化的元素数,因此v[n]
是第一个隐式初始化为零的元素。因此循环条件第一次为假,循环根本不运行;您的代码只是打印出第一个元素。
Some minor issues:
一些小问题:
avoid raw arrays; they behave strangely and inconsistently (e.g., implicit conversion to pointer to the array's first element, can't be assigned, can't be passed to/returned from functions by value)
avoid magic numbers.
int v[100]
is an invitation to a bug if you want your array to get input from somewhere and then try to handle more than 100 elements.avoid
using namespace std;
It's not a big deal in implementation files, although IMO it's better to just get used to explicit qualification, but it can cause problems if you blindly use it everywhere because you'll put it in header files and start causing unnecessary name conflicts.
避免原始数组;它们的行为奇怪且不一致(例如,隐式转换为指向数组第一个元素的指针,无法分配,无法按值传递给函数/从函数返回)
避免幻数。
int v[100]
如果您希望数组从某处获取输入,然后尝试处理 100 多个元素,那么这就是一个错误的邀请。避免
using namespace std;
在实现文件中没什么大不了的,虽然 IMO 最好习惯于显式限定,但是如果你盲目地在任何地方使用它会导致问题,因为你会将它放在头文件中并开始导致不必要的名称冲突。
回答by lovaya
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator lowest = first;
if (first == last) return last;
while (++first != last)
if (*first < *lowest)
lowest = first;
return lowest;
}
回答by Rahul Tripathi
Try this with
试试这个
std::min_element(v.begin(),v.end())
回答by lovaya
#include <iostream>
int main()
{
int v[100] = {5,14,2,4,6};
int n = 5;
int mic = v[0];
for(int i = 0; i != n; ++i)
{
if(v[i] < mic)
mic = v[i];
}
std:cout << mic << std::endl;;
}