min_element() Function in C++

This C++ program demonstrates the min_element algorithm on a container. The min_element() function has two overloaded forms – one with a compare predicate as the third parameter and another without it. The program demonstrates both of the overloaded forms where one prints the element with minimum value in a vector and another using a compare function to find the pair element with minimum value in a map. The compare function compares the two pairs on the basis of their second arguments.

Here is the source code of the C++ program which demonstrates the min_element algorithm on a container. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to demonstrate the min_element() algorithm on a container
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <string>
  9.  
  10. bool compare(std::pair<std::string, int> p, std::pair<std::string, int> q )
  11. {
  12.     return p.second < q.second;
  13. }
  14.  
  15. void print(const std::vector <int>& v)
  16. {
  17.     std::vector <int>::const_iterator i;
  18.     for(i = v.begin(); i != v.end(); i++)
  19.     {
  20.         std::cout << *i << " ";
  21.     }
  22.     std::cout << std::endl;
  23. }
  24.  
  25. void fun(std::pair<std::string, int> p)
  26. {
  27.     std::cout << p.first << "\t" << p.second << std::endl;
  28. }
  29.  
  30. int main()
  31. {
  32.     int arr[] = {40, 80, 50, 30, 10, 70, 60, 100, 20, 90};
  33.     std::vector <int> v(arr, arr + sizeof(arr)/sizeof(int));
  34.     std::map <std::string, int> rank;
  35.     std::pair <std::string, int> min;
  36.     rank["John"] = 10;
  37.     rank["Chris"] = 5;
  38.     rank["Mark"] = 2;
  39.     rank["Henry"] = 7;
  40.  
  41.     std::cout << "min_element() without predicate " << std::endl;
  42.     std::cout << "Vector : ";
  43.     print(v);
  44.     std::cout << "Minimum element = "
  45.               << *std::min_element(v.begin(), v.end())
  46.               << std::endl;
  47.     std::cout << "\nmin_element() with predicate" << std::endl;
  48.     std::cout << "Name\tMarks" << std::endl; 
  49.     for_each(rank.begin(), rank.end(), fun);
  50.     min = (*std::min_element(rank.begin(), rank.end(), compare));
  51.     std::cout << "Person with best rank = " << min.first
  52.               << ", Rank = " << min.second << std::endl;
  53. }

$ a.out
min_element() without predicate 
Vector : 40 80 50 30 10 70 60 100 20 90 
Minimum element = 10
 
min_element() with predicate
Name    Marks
Chris   5
Henry   7
John    10
Mark    2
Person with best rank = Mark, Rank = 2

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.