Cristi DroPs
Cristi DroPs

Reputation: 573

How to find minimum value from vector?

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];
enter code here
    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?

Upvotes: 43

Views: 194042

Answers (7)

lovaya
lovaya

Reputation: 465

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;
}

Upvotes: 4

alestanis
alestanis

Reputation: 21863

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)

Upvotes: 11

zabulus
zabulus

Reputation: 2513

See std::min_element.

Examples:

std::min_element(vec.begin(), vec.end()); // STL containers.
std::min_element(v, v + n); // C style arrays where n is the number of elements.
std::min_element(std::begin(v), std::end(v)); // STL containers and C style arrays. 

Note that for the last option, it is better to apply the 'std 2-step' pattern so it works for user-defined types as well as standard library types:

using std::begin, std::end; // Enables argument-dependent lookup: https://en.cppreference.com/w/cpp/language/adl
std::min_element(begin(v), end(v));

Since C++20, we can also use ranges to avoid having to call begin/end manually:

std::ranges::min_element(v);

Upvotes: 129

acrube
acrube

Reputation: 861

You can always use the stl:

auto min_value = *std::min_element(v.begin(),v.end());

Upvotes: 67

lovaya
lovaya

Reputation: 465

#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;;
}

Upvotes: 0

bames53
bames53

Reputation: 88155

#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.

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.

Upvotes: 14

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

Try this with

 std::min_element(v.begin(),v.end())

Upvotes: 1

Related Questions