Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find minimum value from vector? [closed]

Tags:

c++

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?

like image 609
Cristi DroPs Avatar asked Oct 22 '12 16:10

Cristi DroPs


People also ask

How do you find the maximum value of a vector?

You can use max_element to get the maximum value in vector. The max_element returns an iterator to largest value in the range, or last if the range is empty. As an iterator is like pointers (or you can say pointer is a form of iterator), you can use a * before it to get the value.

How do you find the minimum element of a vector using STL?

Min or Minimum element can be found with the help of *min_element() function provided in STL. Max or Maximum element can be found with the help of *max_element() function provided in STL.


Video Answer


1 Answers

std::min_element(vec.begin(), vec.end()) - for std::vector
std::min_element(v, v+n) - for array
std::min_element( std::begin(v), std::end(v) ) - added C++11 version from comment by @JamesKanze

like image 139
zabulus Avatar answered Sep 22 '22 22:09

zabulus