Open In App

How to Find the Minimum Element in a List in C++?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++.

Example:

Input: 
myList = {30, 10, 20, 50, 40};

Output:
The minimum element in the list is: 10

Find the Minimum Element in a List in C++

To find the minimum element in a std::list, we can use the std::min_element() function. This function takes iterators to the first and last of the range and returns an iterator pointing to the minimum element present in the list.

Syntax to Find the Minimum Element in a List

*min_element(first, last);

Here, the first and last are iterators to the begin and end denoting the range of elements in the list.

C++ Program to Find the Minimum Element in a List

The below program demonstrates how we can use the std::min_element function to find the minimum element in a list in C++.

C++
// C++ program to find the minimum element in a list

#include <algorithm>
#include <iostream>
#include <list>
using namespace std;

int main()
{
    // Initializing a list of integers
    list<int> myList = { 30, 10, 20, 50, 40 };

    // Finding the minimum element
    int mini = *min_element(myList.begin(), myList.end());

    // Printing the maximum element of the list
    cout << "The minimum element in the list is : " << mini
         << endl;

    return 0;
}

Output
The minimum element in the list is : 10

Time Complexity: O(N), where N is the size of the list.
Auxiliary Space: O(1)




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads